fcn-backup 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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
  40. if type "pg_dump" > /dev/null; then
  41. PG_DBS=`sudo -i -u postgres psql template1 -t -c "SELECT datname FROM pg_database WHERE datname NOT LIKE 'template_'"`
  42. for PG_DB in $PG_DBS
  43. do
  44. PG_BACKUP=`sudo -u postgres mktemp -t pg_dump_XXXX`
  45. sudo -u postgres -i pg_dump -Z3 -Fc "${PG_DB}" > "${PG_BACKUP}"
  46. mv "${PG_BACKUP}" "${BACKUP_TEMP}/pgsql_${PG_DB}_Fc.dump"
  47. done
  48. fi
  49. if [ ! -d "${BACKUP_DEST}" ]; then
  50. # Initialize local borg repository
  51. borg init --encryption=none "${BACKUP_DEST}"
  52. fi
  53. # Do local backup
  54. borg create --exclude-from /etc/fcn-toolbox/backup.exclude ${BACKUP_DEST}::${BACKUP_NAME} "/etc" "${BACKUP_TEMP}"
  55. # Prune local archives to keep only 7 daily, and 4 weekly, and 3 monthly ones
  56. borg prune --keep-daily=7 --keep-weekly=4 --keep-monthly=3 ${BACKUP_DEST}
  57. # Quick check of the remote repository before doing a remote backup
  58. if borg check --repository-only ${REMOTE_USER}@${REMOTE_HOSTNAME}:${BACKUP_DEST}; then
  59. # Do remote backup
  60. borg create --exclude-from /etc/fcn-toolbox/backup.exclude ${REMOTE_USER}@${REMOTE_HOSTNAME}:${BACKUP_DEST}::${BACKUP_NAME} "/etc" "${BACKUP_TEMP}"
  61. # Prune remote archives to keep only 7 daily, and 4 weekly, and 3 monthly ones
  62. borg prune --keep-daily=7 --keep-weekly=4 --keep-monthly=3 ${REMOTE_USER}@${REMOTE_HOSTNAME}:${BACKUP_DEST}
  63. else
  64. echo >&2 "Remote repository is missing of corrupted. Try running: "
  65. echo >&2 "borg init --encryption=none ${REMOTE_USER}@${REMOTE_HOSTNAME}:${BACKUP_DEST}"
  66. fi
  67. # Delete temporary directory
  68. rm -rf "${BACKUP_TEMP}"