#!/bin/sh # backup.sh # # This script is a template for a backup script which could be installed to # /usr/local/bin as a per-machine system backup script. You will have to tweak # it to your specific needs. # # The following dependencies have to be installed: # # - borg for the backup itself # - mariabackup to get a consistent snapshot of all MariaDB databases # - go-sendxmpp to notify in case of errors / success HOST='org.example.host' ADMIN='admin@example.org' REPO='/srv/borg' MARIADB_BACKUP_DIR='/var/local/mariadb_export' # create full backup for mariadb to ensure consistent state (optional) rm -rf ${MARIADB_BACKUP_DIR} mkdir -p ${MARIADB_BACKUP_DIR} chmod 700 ${MARIADB_BACKUP_DIR} if ! mariabackup --backup --target-dir=${MARIADB_BACKUP_DIR}/ --user=root then echo "❌ Backup: MariaDB backup failed on ${HOST}." | go-sendxmpp "${ADMIN}" exit 1 fi # create a BorgBackup archive in the given repository if ! borg create \ --verbose \ --stats \ --compression zstd \ --exclude 'dev/*' \ --exclude 'lost+found/*' \ --exclude 'media/*' \ --exclude 'mnt/*' \ --exclude 'proc/*' \ --exclude 'run/*' \ --exclude 'sys/*' \ --exclude 'tmp/*' \ --exclude 'srv/borg/*' \ # change / remove me based on REPO variable "${REPO}::${HOST}_{now:%Y-%m-%d_%H:%M:%S}" \ / then echo "❌ Backup: borg create failed on ${HOST}." | go-sendxmpp "${ADMIN}" exit 1 fi # prune existing archives in the repository to reduce disk usage if ! borg prune \ --glob-archives "${HOST}*" \ --keep-daily 7 \ --keep-weekly 4 \ --keep-monthly 6 \ "${REPO}" then echo "❌ Backup: borg prune failed on ${HOST}." | go-sendxmpp "${ADMIN}" exit 1 fi # inform admin about success if not exited before echo "✅ System backup succeeded on ${HOST}." | go-sendxmpp "${ADMIN}"