mirror of https://github.com/erikw/restic-systemd-automatic-backup without B2
This commit is contained in:
79
usr/local/sbin/restic_backup.sh
Normal file
79
usr/local/sbin/restic_backup.sh
Normal file
@ -0,0 +1,79 @@
|
||||
#!/usr/bin/env bash
|
||||
# Make backup my system with restic to Backblaze B2.
|
||||
# This script is typically run by: /etc/systemd/system/restic-backup.{service,timer}
|
||||
|
||||
# Exit on failure, pipe failure
|
||||
set -e -o pipefail
|
||||
|
||||
export RESTIC_PASSWORD_FILE="/etc/restic/pw.txt"
|
||||
export RESTIC_REPOSITORY="sftp:lerentis@asgard.lan:/srv/dev-disk-by-id-md-name-asgard-Data/Backup/Laptop/Huginn"
|
||||
|
||||
# Clean up lock if we are killed.
|
||||
# If killed by systemd, like $(systemctl stop restic), then it kills the whole cgroup and all it's subprocesses.
|
||||
# However if we kill this script ourselves, we need this trap that kills all subprocesses manually.
|
||||
exit_hook() {
|
||||
echo "In exit_hook(), being killed" >&2
|
||||
jobs -p | xargs kill
|
||||
restic unlock
|
||||
}
|
||||
trap exit_hook INT TERM
|
||||
|
||||
# How many backups to keep.
|
||||
RETENTION_DAYS=14
|
||||
RETENTION_WEEKS=16
|
||||
RETENTION_MONTHS=18
|
||||
RETENTION_YEARS=3
|
||||
|
||||
# What to backup, and what to not
|
||||
BACKUP_PATHS="/home/lerentis/"
|
||||
BACKUP_EXCLUDES="--exclude-file /home/lerentis/.backup_exclude"
|
||||
BACKUP_TAG=systemd.timer
|
||||
|
||||
|
||||
# Set all environment variables like
|
||||
# B2_ACCOUNT_ID, B2_ACCOUNT_KEY, RESTIC_REPOSITORY etc.
|
||||
#source /etc/restic/b2_env.sh
|
||||
|
||||
# How many network connections to set up to B2. Default is 5.
|
||||
B2_CONNECTIONS=50
|
||||
|
||||
# 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
|
||||
|
||||
# Remove locks from other stale processes to keep the automated backup running.
|
||||
restic unlock &
|
||||
wait $!
|
||||
|
||||
# Do the backup!
|
||||
# See restic-backup(1) or http://restic.readthedocs.io/en/latest/040_backup.html
|
||||
# --one-file-system makes sure we only backup exactly those mounted file systems specified in $BACKUP_PATHS, and thus not directories like /dev, /sys etc.
|
||||
# --tag lets us reference these backups later when doing restic-forget.
|
||||
restic backup --verbose --tag $BACKUP_TAG $BACKUP_EXCLUDES $BACKUP_PATHS &
|
||||
wait $!
|
||||
|
||||
# Dereference old backups.
|
||||
# See restic-forget(1) or http://restic.readthedocs.io/en/latest/060_forget.html
|
||||
# --group-by only the tag and path, and not by hostname. This is because I create a B2 Bucket per host, and if this hostname accidentially change some time, there would now be multiple backup sets.
|
||||
restic forget \
|
||||
--verbose \
|
||||
--tag $BACKUP_TAG \
|
||||
--group-by "paths,tags" \
|
||||
--keep-daily $RETENTION_DAYS \
|
||||
--keep-weekly $RETENTION_WEEKS \
|
||||
--keep-monthly $RETENTION_MONTHS \
|
||||
--keep-yearly $RETENTION_YEARS &
|
||||
wait $!
|
||||
|
||||
# Remove old data not linked anymore.
|
||||
# See restic-prune(1) or http://restic.readthedocs.io/en/latest/060_forget.html
|
||||
restic prune --verbose &
|
||||
wait $!
|
||||
|
||||
# Check repository for errors.
|
||||
# NOTE this takes much time (and data transfer from remote repo?), do this in a separate systemd.timer which is run less often.
|
||||
#restic check &
|
||||
#wait $!
|
||||
|
||||
echo "Backup & cleaning is done."
|
34
usr/local/sbin/restic_check.sh
Normal file
34
usr/local/sbin/restic_check.sh
Normal file
@ -0,0 +1,34 @@
|
||||
#!/usr/bin/env bash
|
||||
# Check my backup with restic to Backblaze B2 for errors.
|
||||
# This script is typically run by: /etc/systemd/system/restic-check.{service,timer}
|
||||
|
||||
# Exit on failure, pipe failure
|
||||
set -e -o pipefail
|
||||
|
||||
export RESTIC_PASSWORD_FILE="/etc/restic/pw.txt"
|
||||
export RESTIC_REPOSITORY="sftp:lerentis@asgard.lan:/srv/dev-disk-by-id-md-name-asgard-Data/Backup/Laptop/Huginn"
|
||||
|
||||
# Clean up lock if we are killed.
|
||||
# If killed by systemd, like $(systemctl stop restic), then it kills the whole cgroup and all it's subprocesses.
|
||||
# However if we kill this script ourselves, we need this trap that kills all subprocesses manually.
|
||||
exit_hook() {
|
||||
echo "In exit_hook(), being killed" >&2
|
||||
jobs -p | xargs kill
|
||||
restic unlock
|
||||
}
|
||||
trap exit_hook INT TERM
|
||||
|
||||
|
||||
#source /etc/restic/b2_env.sh
|
||||
|
||||
# How many network connections to set up to B2. Default is 5.
|
||||
B2_CONNECTIONS=50
|
||||
|
||||
# Remove locks from other stale processes to keep the automated backup running.
|
||||
# NOTE nope, dont' unlock liek restic_backup.sh. restic_backup.sh should take preceedance over this script.
|
||||
#restic unlock &
|
||||
#wait $!
|
||||
|
||||
# Check repository for errors.
|
||||
restic check --verbose &
|
||||
wait $!
|
Reference in New Issue
Block a user