.dots/scripts/restic/backup.sh
2024-08-30 10:21:15 +02:00

35 lines
1.3 KiB
Bash
Executable file

#!/usr/bin/env zsh
# author: Daniel Sommer <daniel.sommer@velvettear.de>
# license: MIT
# check if we're running in a subshell, forbid direct execution
[[ "$ZSH_EVAL_CONTEXT" == "toplevel" ]] && printf "error: direct execution of this script is forbidden!\n" >&2 && exit 1
# get the "home" directory
home="$(dirname $(realpath "$0"))"
# check if .env file exists and source it
env="$home/$(hostname).env"
[[ ! -r "$env" ]] && printf "error: config file '"$env"' does not exist!\n" >&2 && exit 1
source "$env"
# check if backup directories are defined
[[ -z "$RESTIC_DIRECTORIES" ]] && printf "error: config file '"$env"' does not define any backup directories!\n" >&2 && exit 1
printf ">> starting restic backup to repository '"$RESTIC_REPOSITORY"'...\n"
# execute "pre" action if defined
[[ -n "$RESTIC_BACKUP_PRE" ]] && eval "$RESTIC_BACKUP_PRE"
for directory in ${RESTIC_DIRECTORIES[@]}; do
seconds="$SECONDS"
printf "\n> backing up '"$directory"'...\n"
sudo --preserve-env -u "$RESTIC_USER" restic backup --verbose --no-scan --retry-lock 3h "$directory"
printf "> backup of '"$directory"' finished after "$(( $SECONDS - $seconds ))" seconds!\n"
done
# execute "post" action if defined
[[ -n "$RESTIC_BACKUP_POST" ]] && eval "$RESTIC_BACKUP_POST"
printf "\n>> restic backup finished after "$SECONDS" seconds!\n"