|
@@ -0,0 +1,84 @@
|
|
|
+#!/bin/sh
|
|
|
+# Requirements : borgbackup
|
|
|
+
|
|
|
+# Read configuration
|
|
|
+. /etc/fcn-toolbox/backup.cfg
|
|
|
+
|
|
|
+if [ -z "${LOCAL_DIR}" ]; then
|
|
|
+ echo >&2 "LOCAL_DIR is not set. Aborting.";
|
|
|
+ exit 1;
|
|
|
+fi
|
|
|
+
|
|
|
+if [ -z "${REMOTE_USER}" ]; then
|
|
|
+ echo >&2 "REMOTE_USER is not set. Aborting.";
|
|
|
+ exit 1;
|
|
|
+fi
|
|
|
+
|
|
|
+if [ -z "${REMOTE_HOSTNAME}" ]; then
|
|
|
+ echo >&2 "REMOTE_HOSTNAME is not set. Aborting.";
|
|
|
+ exit 1;
|
|
|
+fi
|
|
|
+
|
|
|
+HOSTNAME=`hostname`
|
|
|
+BACKUP_DEST=${LOCAL_DIR}/${HOSTNAME}.borg
|
|
|
+
|
|
|
+DATE=`date +%Y-%m-%d`
|
|
|
+BACKUP_NAME="${HOSTNAME}_${DATE}"
|
|
|
+
|
|
|
+# Temporary directory for backup
|
|
|
+BACKUP_TEMP="${LOCAL_DIR}/output-daily"
|
|
|
+
|
|
|
+if ! type "borg" > /dev/null; then
|
|
|
+ echo >&2 "borgbackup is not installed. Aborting.";
|
|
|
+ exit 1;
|
|
|
+fi
|
|
|
+
|
|
|
+if [ -d "${BACKUP_TEMP}" ]; then
|
|
|
+ echo >&2 "Directory ${BACKUP_TEMP} already exists. Aborting.";
|
|
|
+ exit 1;
|
|
|
+else
|
|
|
+ mkdir "${BACKUP_TEMP}"
|
|
|
+fi
|
|
|
+
|
|
|
+# Backup ejabberd data if ejabberd_ctl is available
|
|
|
+if type "ejabberdctl" > /dev/null; then
|
|
|
+ EJABBERD_BACKUP=`mktemp -p ~ejabberd/`
|
|
|
+ ejabberdctl backup "$EJABBERD_BACKUP"
|
|
|
+ mv "$EJABBERD_BACKUP" $BACKUP_TEMP/ejabberd.backup
|
|
|
+fi
|
|
|
+
|
|
|
+# Backup PostgreSQL data if pg_dump is available
|
|
|
+if type "pg_dump" > /dev/null; then
|
|
|
+ PG_DBS=`sudo -i -u postgres psql template1 -t -c "SELECT datname FROM pg_database WHERE datname NOT LIKE 'template_'"`
|
|
|
+ for PG_DB in $PG_DBS
|
|
|
+ do
|
|
|
+ PG_BACKUP=`sudo -u postgres mktemp -t pg_dump_XXXX`
|
|
|
+ sudo -u postgres -i pg_dump -Z3 -Fc "${PG_DB}" > "${PG_BACKUP}"
|
|
|
+ mv "${PG_BACKUP}" "${BACKUP_TEMP}/pgsql_${PG_DB}_Fc.dump"
|
|
|
+ done
|
|
|
+fi
|
|
|
+
|
|
|
+if [ ! -d "${BACKUP_DEST}" ]; then
|
|
|
+ # Initialize local borg repository
|
|
|
+ borg init --encryption=none "${BACKUP_DEST}"
|
|
|
+fi
|
|
|
+
|
|
|
+# Do local backup
|
|
|
+borg create --exclude-from /etc/fcn-toolbox/backup.exclude ${BACKUP_DEST}::${BACKUP_NAME} "/etc" "${BACKUP_TEMP}"
|
|
|
+
|
|
|
+# Prune local archives to keep only 7 daily, and 4 weekly, and 3 monthly ones
|
|
|
+borg prune --keep-daily=7 --keep-weekly=4 --keep-monthly=3 ${BACKUP_DEST}
|
|
|
+
|
|
|
+# Quick check of the remote repository before doing a remote backup
|
|
|
+if borg check --repository-only ${REMOTE_USER}@${REMOTE_HOSTNAME}:${BACKUP_DEST}; then
|
|
|
+ # Do remote backup
|
|
|
+ borg create --exclude-from /etc/fcn-toolbox/backup.exclude ${REMOTE_USER}@${REMOTE_HOSTNAME}:${BACKUP_DEST}::${BACKUP_NAME} "/etc" "${BACKUP_TEMP}"
|
|
|
+ # Prune remote archives to keep only 7 daily, and 4 weekly, and 3 monthly ones
|
|
|
+ borg prune --keep-daily=7 --keep-weekly=4 --keep-monthly=3 ${REMOTE_USER}@${REMOTE_HOSTNAME}:${BACKUP_DEST}
|
|
|
+else
|
|
|
+ echo >&2 "Remote repository is missing of corrupted. Try running: "
|
|
|
+ echo >&2 "borg init --encryption=none ${REMOTE_USER}@${REMOTE_HOSTNAME}:${BACKUP_DEST}"
|
|
|
+fi
|
|
|
+
|
|
|
+# Delete temporary directory
|
|
|
+rm -rf "${BACKUP_TEMP}"
|