Last active
September 1, 2021 20:33
-
-
Save DepthDeluxe/7771ed78789e8b05677b219938d1ea9a to your computer and use it in GitHub Desktop.
Restic Cron Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Adapted from https://github.com/erikw/restic-systemd-automatic-backup/blob/master/usr/local/sbin/restic_backup.sh | |
set -e -o pipefail | |
export PATH="/bin:/usr/bin:/usr/local/bin" | |
RESTIC="/usr/local/bin/restic" | |
RESTIC_ENV="${HOME}/.restic.env" | |
BACKUP_TAG='cron' | |
BACKUP_PATH="${HOME}/Documents" | |
# How many backups to keep. | |
RETENTION_HOURS=72 | |
RETENTION_DAYS=14 | |
RETENTION_WEEKS=4 | |
RETENTION_YEARS=1 | |
source "$RESTIC_ENV" | |
exit_hook() { | |
echo 'In exit_hook(), being killed' >&2 | |
jobs -p | xargs kill | |
restic unlock | |
} | |
trap exit_hook INT TERM | |
# NOTE start all commands in background and wait for them to finish. | |
# Reason: bash ignores any signals while child process is executing and thus my trap exit hook is not triggered. | |
# However if put in subprocesses, wait(1) waits until the process finishes OR signal is received. | |
# Reference: https://unix.stackexchange.com/questions/146756/forward-sigterm-to-child-in-bash | |
echo "Starting backup at $(date)" | |
$RESTIC unlock & | |
wait $! | |
$RESTIC backup --verbose --tag "$BACKUP_TAG" "$BACKUP_PATH" & | |
wait $! | |
$RESTIC forget --verbose --tag "$BACKUP_TAG" --prune --keep-hourly $RETENTION_HOURS --keep-daily $RETENTION_DAYS --keep-weekly $RETENTION_WEEKS --keep-yearly $RETENTION_YEARS & | |
wait $! | |
echo "Backup and cleanup is done at $(date)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment