2020-05-17 15:25:51 +01:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
# Use setcap'd binary in home directory
|
2021-03-14 21:04:54 +00:00
|
|
|
#PATH="/home/restic/bin/:$PATH"
|
2022-04-18 10:22:53 +01:00
|
|
|
#Not necessary anymore : binary in /usr/bin setcap'd
|
2020-05-17 15:25:51 +01:00
|
|
|
|
|
|
|
backup_origin="/backups/restic"
|
|
|
|
backup_container="central-backup"
|
|
|
|
|
2022-04-18 10:27:49 +01:00
|
|
|
mail_sender='"Name""<sender@example.com>"'
|
|
|
|
mail_recipients="first@example.com,second@example.com"
|
|
|
|
|
2020-05-17 15:25:51 +01:00
|
|
|
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"
|
|
|
|
|
2022-05-08 20:17:17 +01:00
|
|
|
systemctl stop tandoor
|
|
|
|
|
2020-05-17 15:25:51 +01:00
|
|
|
# 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
|
|
|
|
|
2022-04-18 10:22:53 +01:00
|
|
|
# Sleep to allow time for propagation
|
|
|
|
sleep 60
|
|
|
|
|
2022-04-18 10:44:21 +01:00
|
|
|
# Remove old and stale data
|
2020-05-17 15:25:51 +01:00
|
|
|
if [ -z "$error_message" ]; then
|
2022-04-18 10:44:21 +01:00
|
|
|
restic forget --verbose --prune --group-by tag --tag "$1" --keep-last "$backup_count" > forget_output.log 2>&1
|
2020-05-17 15:25:51 +01:00
|
|
|
fi
|
|
|
|
if [ $? -ne 0 ]; then
|
2022-04-18 10:44:21 +01:00
|
|
|
error_message="Restic forget failed ! See log bellow.\n\n"
|
|
|
|
error_message="$error_message""$(cat forget_output.log)"
|
2020-05-17 15:25:51 +01:00
|
|
|
fi
|
|
|
|
|
2022-04-18 10:44:21 +01:00
|
|
|
# Check repository status
|
2020-05-17 15:25:51 +01:00
|
|
|
if [ -z "$error_message" ]; then
|
2022-04-18 10:44:21 +01:00
|
|
|
restic check --verbose > check_output.log 2>&1
|
2020-05-17 15:25:51 +01:00
|
|
|
fi
|
|
|
|
if [ $? -ne 0 ]; then
|
2022-04-18 10:44:21 +01:00
|
|
|
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)"
|
2020-05-17 15:25:51 +01:00
|
|
|
fi
|
|
|
|
|
2022-04-18 10:44:21 +01:00
|
|
|
# Clean up and remove old backups
|
|
|
|
rm -rf temp/*
|
|
|
|
|
2020-05-17 15:25:51 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2022-05-08 20:17:17 +01:00
|
|
|
systemctl start tandoor
|
|
|
|
|
2020-05-17 15:25:51 +01:00
|
|
|
exit 0
|