fcn-backup 3.0 KB

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