diff --git a/scripts/notify.sh b/scripts/notify.sh index f3a251c..7efb9e6 100755 --- a/scripts/notify.sh +++ b/scripts/notify.sh @@ -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 diff --git a/scripts/restic/backup.sh b/scripts/restic/backup.sh new file mode 100755 index 0000000..451a66e --- /dev/null +++ b/scripts/restic/backup.sh @@ -0,0 +1,35 @@ +#!/usr/bin/env zsh + +# author: Daniel Sommer +# 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" diff --git a/scripts/restic/j5040.env b/scripts/restic/j5040.env new file mode 100644 index 0000000..b8479d8 --- /dev/null +++ b/scripts/restic/j5040.env @@ -0,0 +1,15 @@ +#!/usr/bin/env zsh + +# author: Daniel Sommer +# 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" +) diff --git a/scripts/restic/j5040.sh b/scripts/restic/j5040.sh deleted file mode 100755 index 424ddbd..0000000 --- a/scripts/restic/j5040.sh +++ /dev/null @@ -1,21 +0,0 @@ -#!/usr/bin/env bash - -# author: Daniel Sommer -# 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" diff --git a/scripts/restic/n100.env b/scripts/restic/n100.env new file mode 100644 index 0000000..b8479d8 --- /dev/null +++ b/scripts/restic/n100.env @@ -0,0 +1,15 @@ +#!/usr/bin/env zsh + +# author: Daniel Sommer +# 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" +) diff --git a/scripts/restic/n100.sh b/scripts/restic/n100.sh deleted file mode 100755 index d59ec3e..0000000 --- a/scripts/restic/n100.sh +++ /dev/null @@ -1,30 +0,0 @@ -#!/usr/bin/env bash - -# author: Daniel Sommer -# 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" diff --git a/scripts/restic/prune.sh b/scripts/restic/prune.sh new file mode 100755 index 0000000..15e7686 --- /dev/null +++ b/scripts/restic/prune.sh @@ -0,0 +1,23 @@ +#!/usr/bin/env zsh + +# author: Daniel Sommer +# 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" diff --git a/scripts/restic/restic.env b/scripts/restic/restic.env new file mode 100644 index 0000000..7a89e5e --- /dev/null +++ b/scripts/restic/restic.env @@ -0,0 +1,32 @@ +#!/usr/bin/env zsh + +# author: Daniel Sommer +# 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" +) diff --git a/scripts/restic/restic.sh b/scripts/restic/restic.sh index f6629dd..19166fc 100755 --- a/scripts/restic/restic.sh +++ b/scripts/restic/restic.sh @@ -1,4 +1,4 @@ -#!/usr/bin/env bash +#!/usr/bin/env zsh # author: Daniel Sommer # 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