restic-cron/restic-cron

68 lines
1.8 KiB
Plaintext
Raw Permalink Normal View History

2016-05-20 22:45:05 +02:00
#!/bin/bash
#
# restic-cron - Runs a Restic backup on the homedir
#
# by Paul van Tilburg <paul@luon.net>
# Nofication function
notify() {
test -z "$RESTIC_QUIET" && echo "I: $1"
notify-send -t 10000 -i drive-harddisk "Restic Backup" "$1"
2016-05-20 22:45:05 +02:00
}
# Check if a --verbose argument is passed, otherwise use quiet
RESTIC_QUIET="--quiet"
if [ "$1" = "--verbose" ]; then
RESTIC_QUIET=""
fi
# Restic settings
2020-02-12 20:09:22 +01:00
RESTIC_BACKUP_OPTIONS="--exclude-file=$HOME/.config/restic/exclude --cleanup-cache"
RESTIC_FORGET_OPTIONS="--keep-weekly=4 --keep-monthly=12 --keep-yearly=60"
RESTIC_DIR="$HOME"
2020-02-12 20:50:09 +01:00
export RESTIC_REPOSITORY="sftp://void//media/Backup/paul/restic-laptops"
2016-05-20 22:45:05 +02:00
# Start the backup by asking for the passphrase
notify "Starting backup…"
export RESTIC_PASSWORD=$(zenity --title="Restic backup passphrase" --password)
if [ -z "$RESTIC_PASSWORD" ]; then
notify "Backup aborted due to empty passphrase or cancellation!"
exit 1
fi
# Create a new backup
test -z "$RESTIC_QUIET" && echo "I: Backup step…"
restic $RESTIC_QUIET backup $RESTIC_DIR $RESTIC_BACKUP_OPTIONS
status=$?
if [ $status -ne 0 ]; then
2016-05-20 22:45:05 +02:00
notify "Unable to finish the backup!"
exit 2
fi
2017-07-26 11:46:12 +02:00
# Forget old snapshots and prune the repository
2016-08-21 21:46:52 +02:00
test -z "$RESTIC_QUIET" && echo "I: Forget step…"
2017-07-26 11:46:12 +02:00
restic $RESTIC_QUIET forget --prune $RESTIC_FORGET_OPTIONS
2016-08-21 21:46:52 +02:00
status=$?
if [ $status -ne 0 ]; then
echo "E: Exit code was $status"
2017-07-26 11:46:12 +02:00
notify "Was not able to forget old snapshots and/or prune the repository!"
2016-08-21 21:46:52 +02:00
exit 3
fi
2020-08-11 14:17:53 +02:00
# Verify the backup every 4 weeks
if [ $(($(date +%-W) % 4)) = 0 ]; then
2020-08-11 14:17:53 +02:00
test -z "$RESTIC_QUIET" && echo "I: Check step…"
restic $RESTIC_QUIET check
status=$?
if [ $status -ne 0 ]; then
echo "E: Exit code was $status"
notify "Repository check of the backup failed!"
exit 5
fi
else
echo "I: Skipping check step this time!"
2016-05-20 22:45:05 +02:00
fi
notify "Backup finished!"