fcn-backup 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/bin/sh
  2. # Requirements : borgbackup
  3. # Read configuration
  4. . /etc/fcn-toolbox/backup.cfg
  5. if [ -z "${LOCAL_DIR}" ]; then
  6. echo >&2 "LOCAL_DIR is not set. Aborting.";
  7. exit 1;
  8. fi
  9. if [ -z "${REMOTE_USER}" ]; then
  10. echo >&2 "REMOTE_USER is not set. Aborting.";
  11. exit 1;
  12. fi
  13. if [ -z "${REMOTE_HOSTNAME}" ]; then
  14. echo >&2 "REMOTE_HOSTNAME is not set. Aborting.";
  15. exit 1;
  16. fi
  17. HOSTNAME=`hostname`
  18. BACKUP_DEST=${LOCAL_DIR}/${HOSTNAME}.borg
  19. DATE=`date +%Y-%m-%d`
  20. BACKUP_NAME="${HOSTNAME}_${DATE}"
  21. # Temporary directory for backup
  22. BACKUP_TEMP="${LOCAL_DIR}/output-daily"
  23. if ! type "borg" > /dev/null; then
  24. echo >&2 "borgbackup is not installed. Aborting.";
  25. exit 1;
  26. fi
  27. if [ -d "${BACKUP_TEMP}" ]; then
  28. echo >&2 "Directory ${BACKUP_TEMP} already exists. Aborting.";
  29. exit 1;
  30. else
  31. mkdir "${BACKUP_TEMP}"
  32. fi
  33. # Backup ejabberd data if ejabberd_ctl is available
  34. if type "ejabberdctl" > /dev/null; then
  35. EJABBERD_BACKUP=`mktemp -p ~ejabberd/`
  36. ejabberdctl backup "$EJABBERD_BACKUP"
  37. mv "$EJABBERD_BACKUP" $BACKUP_TEMP/ejabberd.backup
  38. fi
  39. # Backup PostgreSQL data if pg_dump is available, and user postgres exists
  40. if type "pg_dump" > /dev/null; then
  41. if id -u "postgres" > /dev/null 2>&1; then
  42. PG_DBS=`sudo -i -u postgres psql template1 -t -c "SELECT datname FROM pg_database WHERE datname NOT LIKE 'template_'"`
  43. for PG_DB in $PG_DBS
  44. do
  45. PG_BACKUP=`sudo -u postgres mktemp -t pg_dump_XXXX`
  46. sudo -u postgres -i pg_dump -Z3 -Fc "${PG_DB}" > "${PG_BACKUP}"
  47. mv "${PG_BACKUP}" "${BACKUP_TEMP}/pgsql_${PG_DB}_Fc.dump"
  48. done
  49. fi
  50. fi
  51. if [ ! -d "${BACKUP_DEST}" ]; then
  52. # Initialize local borg repository
  53. borg init --encryption=none "${BACKUP_DEST}"
  54. fi
  55. # Do local backup
  56. borg create --exclude-from /etc/fcn-toolbox/backup.exclude ${BACKUP_DEST}::${BACKUP_NAME} "/etc" "${BACKUP_TEMP}"
  57. # Prune local archives to keep only 7 daily, and 4 weekly, and 3 monthly ones
  58. borg prune --keep-daily=7 --keep-weekly=4 --keep-monthly=3 ${BACKUP_DEST}
  59. # Quick check of the remote repository before doing a remote backup
  60. if borg check --repository-only ${REMOTE_USER}@${REMOTE_HOSTNAME}:${BACKUP_DEST}; then
  61. # Do remote backup
  62. borg create --exclude-from /etc/fcn-toolbox/backup.exclude ${REMOTE_USER}@${REMOTE_HOSTNAME}:${BACKUP_DEST}::${BACKUP_NAME} "/etc" "${BACKUP_TEMP}"
  63. # Prune remote archives to keep only 7 daily, and 4 weekly, and 3 monthly ones
  64. borg prune --keep-daily=7 --keep-weekly=4 --keep-monthly=3 ${REMOTE_USER}@${REMOTE_HOSTNAME}:${BACKUP_DEST}
  65. else
  66. echo >&2 "Remote repository is missing of corrupted. Try running: "
  67. echo >&2 "borg init --encryption=none ${REMOTE_USER}@${REMOTE_HOSTNAME}:${BACKUP_DEST}"
  68. fi
  69. # Delete temporary directory
  70. rm -rf "${BACKUP_TEMP}"