fcn-backup 3.1 KB

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