restructure restic scripts

This commit is contained in:
Daniel Sommer 2024-08-29 15:53:24 +02:00
parent 7b735afaca
commit f4ec62e83e
9 changed files with 145 additions and 62 deletions

View file

@ -55,4 +55,4 @@ echo "pw: $password"
echo "url: $url"
echo "args: ${args[@]}"
curl -u $user:$password "${args[@]}" $url
curl -s -u $user:$password "${args[@]}" $url

35
scripts/restic/backup.sh Executable file
View file

@ -0,0 +1,35 @@
#!/usr/bin/env zsh
# author: Daniel Sommer <daniel.sommer@velvettear.de>
# license: MIT
# check permissions
[[ "$EUID" != 0 ]] && printf "error: permission denied!\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"
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"

15
scripts/restic/j5040.env Normal file
View file

@ -0,0 +1,15 @@
#!/usr/bin/env zsh
# author: Daniel Sommer <daniel.sommer@velvettear.de>
# license: MIT
# define title and icon for the notification
export RESTIC_NTFY_TITLE="$(hostname)"
export RESTIC_NTFY_ICON="green_circle"
# define directories to backup
export RESTIC_DIRECTORIES=(
"/etc"
"/root"
"/home/velvettear"
)

View file

@ -1,21 +0,0 @@
#!/usr/bin/env bash
# author: Daniel Sommer <daniel.sommer@velvettear.de>
# license: MIT
[[ "$EUID" != 0 ]] && printf "error: permission denied!\n" >&2 && exit 1
directories=(
"/etc"
)
printf ">> starting restic backup...\n"
for directory in ${directories[@]}; do
seconds="$SECONDS"
printf "> backing up '"$directory"'...\n"
restic backup --verbose --no-scan --retry-lock 3h "$directory"
printf "> backup of '"$directory"' finished after "$(( $SECONDS - $seconds ))" seconds!\n"
done
printf ">> restic backup finished after "$SECONDS" seconds!\n"

15
scripts/restic/n100.env Normal file
View file

@ -0,0 +1,15 @@
#!/usr/bin/env zsh
# author: Daniel Sommer <daniel.sommer@velvettear.de>
# license: MIT
# define title and icon for the notification
export RESTIC_NTFY_TITLE="$(hostname)"
export RESTIC_NTFY_ICON="green_circle"
# define directories to backup
export RESTIC_DIRECTORIES=(
"/etc"
"/root"
"/home/velvettear"
)

View file

@ -1,30 +0,0 @@
#!/usr/bin/env bash
# author: Daniel Sommer <daniel.sommer@velvettear.de>
# license: MIT
[[ "$EUID" != 0 ]] && printf "error: permission denied!\n" >&2 && exit 1
directories=(
"/etc"
"/mnt/storage/audiobooks"
"/mnt/storage/comics"
"/mnt/storage/documents"
"/mnt/storage/ebooks"
"/mnt/storage/images"
"/mnt/storage/music"
"/mnt/storage/software"
"/mnt/storage/syncthing"
"/mnt/storage/videos"
)
printf ">> starting restic backup...\n"
for directory in ${directories[@]}; do
seconds="$SECONDS"
printf "\n> backing up '"$directory"'...\n"
restic backup --verbose --no-scan --retry-lock 3h "$directory"
printf "> backup of '"$directory"' finished after "$(( $SECONDS - $seconds ))" seconds!\n"
done
printf ">> restic backup finished after "$SECONDS" seconds!\n"

23
scripts/restic/prune.sh Executable file
View file

@ -0,0 +1,23 @@
#!/usr/bin/env zsh
# author: Daniel Sommer <daniel.sommer@velvettear.de>
# license: MIT
# check permissions
[[ "$EUID" != 0 ]] && printf "error: permission denied!\n" >&2 && exit 1
# define title and icon for the notification
export RESTIC_NTFY_TITLE="prune"
export RESTIC_NTFY_ICON="red_circle"
printf ">> pruning repository '"$RESTIC_REPOSITORY"'...\n\n"
# execute "pre" action if defined
[[ -n "$RESTIC_PRUNE_PRE" ]] && eval "$RESTIC_PRUNE_PRE"
restic forget --keep-daily 7 --keep-weekly 1 --keep-monthly 1 --prune --retry-lock 3h
# execute "post" action if defined
[[ -n "$RESTIC_PRUNE_POST" ]] && eval "$RESTIC_PRUNE_POST"
printf "\n>> repository pruned after "$SECONDS" seconds!\n"

32
scripts/restic/restic.env Normal file
View file

@ -0,0 +1,32 @@
#!/usr/bin/env zsh
# author: Daniel Sommer <daniel.sommer@velvettear.de>
# license: MIT
# override default repository
export RESTIC_REPOSITORY="/mnt/restic"
# define pre and post backup actions
export RESTIC_BACKUP_PRE="printf '\n> mounting remote storage...\n' && mount -t nfs -o vers=4,soft,noatime,nolock,retrans=1,timeo=10 192.168.100.1:/mnt/storage /mnt/n100"
export RESTIC_BACKUP_POST="printf '\n> unmounting remote storage...\n' && umount /mnt/n100"
# define pre and post prune actions
export RESTIC_PRUNE_PRE=""
export RESTIC_PRUNE_POST="printf '\n> fixing ownership of repository...\n' && chown -R restic:restic $RESTIC_REPOSITORY"
# define title and icon for the notification
export RESTIC_NTFY_TITLE="storage"
export RESTIC_NTFY_ICON="yellow_circle"
# define backup directories
export RESTIC_DIRECTORIES=(
"/mnt/n100/audiobooks"
"/mnt/n100/comics"
"/mnt/n100/documents"
"/mnt/n100/ebooks"
"/mnt/n100/images"
"/mnt/n100/music"
"/mnt/n100/software"
"/mnt/n100/syncthing"
"/mnt/n100/videos"
)

View file

@ -1,4 +1,4 @@
#!/usr/bin/env bash
#!/usr/bin/env zsh
# author: Daniel Sommer <daniel.sommer@velvettear.de>
# license: MIT
@ -9,25 +9,39 @@ set -e
# check permissions
[[ "$EUID" != 0 ]] && printf "error: permission denied!\n" >&2 && exit 1
# check which action to execute
[[ -n "$1" ]] && action="$1" || action="backup"
# check if script exists
script="$(dirname $(realpath "$0"))/$(hostname).sh"
script="$(dirname $(realpath "$0"))/$action.sh"
[[ ! -x "$script" ]] && printf "error: script '"$script"' does not exist or is not executable!\n" >&2 && exit 1
# get hostname
host="$(hostname)"
[[ -z "$host" ]] && host="$HOST"
# get the "home" directory
home="$(dirname $(realpath "$0"))"
# setup restic
export RESTIC_REPOSITORY="rest:http://192.168.100.101:8000"
export RESTIC_PASSWORD="\$Velvet90"
export RESTIC_COMPRESSION="max"
export RESTIC_CACHE_DIR="/var/cache/restic"
export RESTIC_LOG="/var/log/restic-$action.log"
# setup log file
log="/var/log/restic-"$(hostname)".log"
# check if .env file exists and source it
env="$home/$(hostname).env"
[[ -r "$env" ]] && source "$env"
# set default values for the notification if undefined
[[ -z "$RESTIC_NTFY_TITLE" ]] && RESTIC_NTFY_TITLE="$(hostname)"
[[ -z "$RESTIC_NTFY_ICON" ]] && RESTIC_NTFY_ICON="white_circle"
# execute "pre" action if defined
[[ -n "$RESTIC_ACTION_PRE" ]] && eval "$RESTIC_ACTION_PRE"
# source / execute restic script
source "$script" 2>&1 | tee "$log"
source "$script" > >(tee "$RESTIC_LOG") 2>&1
# send a notification when finished
/home/velvettear/.dots/scripts/notify.sh "restic" "$log" --title "$host" --icon "green_circle"
# execute "post" action if defined
[[ -n "$RESTIC_ACTION_POST" ]] && eval "$RESTIC_ACTION_POST"
# send a notification
/home/velvettear/.dots/scripts/notify.sh "restic" "$RESTIC_LOG" --title "$RESTIC_NTFY_TITLE" --icon "$RESTIC_NTFY_ICON" > /dev/null