68 lines
1.8 KiB
Bash
Executable File
68 lines
1.8 KiB
Bash
Executable File
#!/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"
|
|
}
|
|
|
|
# Check if a --verbose argument is passed, otherwise use quiet
|
|
RESTIC_QUIET="--quiet"
|
|
if [ "$1" = "--verbose" ]; then
|
|
RESTIC_QUIET=""
|
|
fi
|
|
|
|
# Restic settings
|
|
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"
|
|
export RESTIC_REPOSITORY="sftp://void//media/Backup/paul/restic-laptops"
|
|
|
|
# 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
|
|
notify "Unable to finish the backup!"
|
|
exit 2
|
|
fi
|
|
|
|
# Forget old snapshots and prune the repository
|
|
test -z "$RESTIC_QUIET" && echo "I: Forget step…"
|
|
restic $RESTIC_QUIET forget --prune $RESTIC_FORGET_OPTIONS
|
|
status=$?
|
|
if [ $status -ne 0 ]; then
|
|
echo "E: Exit code was $status"
|
|
notify "Was not able to forget old snapshots and/or prune the repository!"
|
|
exit 3
|
|
fi
|
|
|
|
# Verify the backup every 4 weeks
|
|
if [ $(($(date +%-W) % 4)) = 0 ]; then
|
|
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!"
|
|
fi
|
|
|
|
notify "Backup finished!"
|