#!/bin/bash ## ## Save backup to directory named after hostname ## DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )" DIR_DATA=$DIR"/data" # load config . $DIR/config LOG=$DIR"/backup.log" export BORG_REPO=$REPOSITORY export BORG_PASSPHRASE=$PASSPHRASE export BORG_RSH="ssh -i $DIR/id_backup" ## ## Write output to logfile ## exec > >(tee -i ${LOG}) exec 2>&1 # some helpers and error handling: info() { printf "\n%s %s\n\n" "$( date )" "$*" >&2; } trap 'echo $( date ) Backup interrupted >&2; exit 2' INT TERM echo "###### Starting backup on $(date) ######" . $DIR/before.sh ## ## Sync backup data ## echo "Syncing backup files ..." borg create \ --verbose \ --filter AME \ --list \ --stats \ --show-rc \ --compression lz4 \ --exclude-caches \ --exclude '/home/*/.cache/*' \ --exclude '/var/cache/*' \ --exclude '/var/tmp/*' \ \ ::'{hostname}-{now}' \ /etc \ /home \ /root \ /var \ $DIR_DATA \ /media/data/live backup_exit=$? echo "###### Finished backup on $(date) ######" echo "###### Pruning repository on $(date) ######" # Use the `prune` subcommand to maintain 7 daily, 4 weekly and 6 monthly # archives of THIS machine. The '{hostname}-' prefix is very important to # limit prune's operation to this machine's archives and not apply to # other machines' archives also: borg prune \ --list \ --prefix '{hostname}-' \ --show-rc \ --keep-daily 7 \ --keep-weekly 4 \ --keep-monthly 6 \ prune_exit=$? echo "###### Finished pruning on $(date) ######" # use highest exit code as global exit code global_exit=$(( backup_exit > prune_exit ? backup_exit : prune_exit )) if [ ${global_exit} -eq 1 ]; then echo "Backup and/or Prune finished with a warning" fi if [ ${global_exit} -gt 1 ]; then echo "Backup and/or Prune finished with an error" fi . $DIR/after.sh exit ${global_exit}