#!/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_ENABLE}" ]; then echo >&2 "REMOTE_ENABLE is not set. Aborting."; exit 1; elif [ ${REMOTE_ENABLE} = true ]; then 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 if [ -z "${REMOTE_DIR}" ]; then echo >&2 "REMOTE_DIR is not set. Aborting."; exit 1; fi fi if [ -z "${PGSQL_EXCLUDE_DATNAME}" ]; then PGSQL_EXCLUDE_DATNAME="['template_']" fi HOSTNAME=`hostname` LOCAL_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, and user postgres exists if type "pg_dump" > /dev/null; then if id -u "postgres" > /dev/null 2>&1; then PG_DBS=`sudo -i -u postgres psql template1 -t -c "SELECT datname FROM pg_database WHERE NOT datname LIKE ANY(ARRAY${PGSQL_EXCLUDE_DATNAME})"` 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 fi if [ ! -d "${LOCAL_DEST}" ]; then # Initialize local borg repository borg init --encryption=none "${LOCAL_DEST}" fi # Do local backup borg create --exclude-from /etc/fcn-toolbox/backup.exclude ${LOCAL_DEST}::${BACKUP_NAME} "${BACKUP_TEMP}" ${BACKUP_DIR_INCL} # 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 ${LOCAL_DEST} if [ ${REMOTE_ENABLE} = true ]; then REMOTE_DEST=${REMOTE_DIR}/${HOSTNAME}.borg # Quick check of the remote repository before doing a remote backup if borg check --repository-only ${REMOTE_USER}@${REMOTE_HOSTNAME}:${REMOTE_DEST}; then # Do remote backup borg create --exclude-from /etc/fcn-toolbox/backup.exclude ${REMOTE_USER}@${REMOTE_HOSTNAME}:${REMOTE_DEST}::${BACKUP_NAME} "${BACKUP_TEMP}" ${BACKUP_DIR_INCL} # 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}:${REMOTE_DEST} else echo >&2 "Remote repository is missing of corrupted. Try running: " echo >&2 "borg init --encryption=none ${REMOTE_USER}@${REMOTE_HOSTNAME}:${REMOTE_DEST}" fi fi # Delete temporary directory rm -rf "${BACKUP_TEMP}"