#!/bin/bash # Use setcap'd binary in home directory #PATH="/home/restic/bin/:$PATH" # Now uses binary in /usr/bin, which is also setcap'd backup_origin="/backups/restic" backup_container="central-backup" if [[ -z "$1" || "$1" != "daily" && "$1" != "weekly" && "$1" != "monthly" ]]; then echo "Usage : backup [daily|weekly|monthly]" exit 1 fi include_files="" # Use case fallthrough to include each subset of files # Set the number of backups to keep at the same time using default values case "$1" in "monthly") include_files="$include_files"" --files-from targets/monthly" backup_count=${backup_count:-6} ;& "weekly") include_files="$include_files"" --files-from targets/weekly" backup_count=${backup_count:-8} ;& "daily") include_files="$include_files"" --files-from targets/daily" backup_count=${backup_count:-14} ;; esac cd "$backup_origin" # Run database backups echo "Backing up BookStack db..." mysqldump -u restic BookStack > temp/BookStack_db.mysql echo "Backing up BitWarden db..." sqlite3 /home/bitwarden/data/db.sqlite3 ".backup $backup_origin/temp/BitWarden_db.sqlite" echo "Backing up Firefly db..." sqlite3 /var/www/firefly-iii/storage/database/database.sqlite ".backup $backup_origin/temp/Firefly_db.sqlite" # Backup apt package list and keys ( cd temp echo "Backing up apt keys,sources and packages..." ../package_save.sh here ) # Use OVH credentials and backup source openstack_creds.sh export RESTIC_PASSWORD_FILE="$backup_container".pass export RESTIC_REPOSITORY="swift:$backup_container:/restic" restic backup $include_files --verbose --tag "$1" > backup_output.log 2>&1 if [ $? -ne 0 ]; then error_message="Restic backup failed ! See log bellow.\n" error_message="$error_message""Backup check and purge will not be run.\n\n" error_message="$error_message""$(cat backup_output.log)" fi if [ -z "$error_message" ]; then restic check --verbose > check_output.log 2>&1 fi if [ $? -ne 0 ]; then error_message="Restic check failed ! See log below.\n" error_message="$error_message""Backup purge will not be run.\n\n" error_message="$error_message""$(cat check_output.log)" fi # Clean up and remove old backups rm -rf temp/* if [ -z "$error_message" ]; then restic forget --verbose --prune --group-by tag --tag "$1" --keep-last "$backup_count" > forget_output.log 2>&1 fi if [ $? -ne 0 ]; then error_message="Restic forget failed ! See log bellow.\n\n" error_message="$error_message""$(cat forget_output.log)" fi if [ -n "$error_message" ]; then echo -e "$error_message" | mail -aFrom:"$mail_sender" -s "[$(uname -n)] ⚠️ Backup error $(date +%D-%Hh%M)" "$mail_recipients" exit 1 fi exit 0