.dots/scripts/restic/restic.sh

49 lines
1.5 KiB
Bash
Raw Normal View History

2024-08-29 15:53:24 +02:00
#!/usr/bin/env zsh
2024-08-28 15:22:02 +02:00
# author: Daniel Sommer <daniel.sommer@velvettear.de>
# license: MIT
# exit on error
2024-08-28 15:35:53 +02:00
set -e
2024-08-28 15:22:02 +02:00
# check permissions
[[ "$EUID" != 0 ]] && printf "error: permission denied!\n" >&2 && exit 1
2024-08-29 15:53:24 +02:00
# check which action to execute
[[ -n "$1" ]] && action="$1" || action="backup"
2024-08-28 15:22:02 +02:00
# check if script exists
2024-08-29 15:53:24 +02:00
script="$(dirname $(realpath "$0"))/$action.sh"
2024-08-28 15:22:02 +02:00
[[ ! -x "$script" ]] && printf "error: script '"$script"' does not exist or is not executable!\n" >&2 && exit 1
2024-08-29 15:53:24 +02:00
# get the "home" directory
home="$(dirname $(realpath "$0"))"
2024-08-28 15:22:02 +02:00
# setup restic
2024-08-30 10:21:15 +02:00
export RESTIC_USER="$(whoami)"
2024-08-28 15:22:02 +02:00
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"
2024-08-29 15:53:24 +02:00
export RESTIC_LOG="/var/log/restic-$action.log"
# check if .env file exists and source it
env="$home/$(hostname).env"
[[ -r "$env" ]] && source "$env"
2024-08-28 15:22:02 +02:00
2024-08-29 15:53:24 +02:00
# 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"
2024-08-28 15:22:02 +02:00
# source / execute restic script
2024-08-29 15:53:24 +02:00
source "$script" > >(tee "$RESTIC_LOG") 2>&1
# execute "post" action if defined
[[ -n "$RESTIC_ACTION_POST" ]] && eval "$RESTIC_ACTION_POST"
2024-08-28 15:22:02 +02:00
2024-08-29 15:53:24 +02:00
# send a notification
/home/velvettear/.dots/scripts/notify.sh "restic" "$RESTIC_LOG" --title "$RESTIC_NTFY_TITLE" --icon "$RESTIC_NTFY_ICON" > /dev/null