Skip to content

Commit

Permalink
🍔
Browse files Browse the repository at this point in the history
  • Loading branch information
webees committed Jan 29, 2025
1 parent e9df8a8 commit fc538b8
Showing 1 changed file with 18 additions and 27 deletions.
45 changes: 18 additions & 27 deletions scripts/restic.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
#!/bin/sh

if [ -z "$RESTIC_PASSWORD" ]; then
exit 1
fi

if [ -n "$SMTP_TO" ]; then
cat << EOF > /etc/msmtprc
defaults
Expand Down Expand Up @@ -33,7 +29,7 @@ LOG="/var/log/restic/$(date +\%Y\%m\%d_\%H\%M\%S).log"
mkdir -p /var/log/restic/

# e-mail notification
function notify() {
function email() {
if [ -n "$SMTP_TO" ]; then
sed -e 's/\x1b\[[0-9;]*m//g' "${LOG}" | mail -s "${EMAIL_SUBJECT_PREFIX} ${1}" ${SMTP_TO}
fi
Expand All @@ -43,10 +39,6 @@ function log() {
"$@" 2>&1 | tee -a "$LOG"
}

function run_silently() {
"$@" >/dev/null 2>&1
}

# ###############################################################################
# colorized echo helpers #
# taken from: https://github.com/atomantic/dotfiles/blob/master/lib_sh/echos.sh #
Expand Down Expand Up @@ -76,12 +68,11 @@ function error() {
log echo -e "$2"
}

function notify_and_exit_on_error() {
function email_and_exit_on_error() {
output=$(eval $1 2>&1)

if [ $? -ne 0 ]; then
error "$2" "$output"
notify "$2"
email "$2"
exit 2
fi
}
Expand All @@ -90,31 +81,31 @@ function notify_and_exit_on_error() {
# backup steps #
# ##############
restic unlock

running "checking restic config"

run_silently restic cat config
restic cat config

if [ $? -ne 0 ]; then
warn "restic repo either not initialized or erroring out"
running "trying to initialize it"
notify_and_exit_on_error "restic init" "Repo init failed"
warn "Restic repo not ready"
running "Trying to initialize it"
email_and_exit_on_error "restic init" "Repo init failed"
ok
fi

running "Backup SQLite"
email_and_exit_on_error "sqlite3 /data/db.sqlite3 '.backup /data/backup.bak'" "SQLite backup failed"
ok

running "backing up sqlite database"
notify_and_exit_on_error "sqlite3 /data/db.sqlite3 '.backup /data/backup.bak'" "SQLite backup failed"
running "SQLite Check"
notify_and_exit_on_error "sqlite3 /data/backup.bak 'PRAGMA integrity_check'" "SQLite check failed"
ok

running "restic backup"
notify_and_exit_on_error "restic backup --verbose --exclude='db.*' /data" "Restic backup failed"
running "Restic Backup"
email_and_exit_on_error "restic backup --verbose --exclude='db.*' /data" "Restic backup failed"
ok

running "checking consistency of restic repository"
notify_and_exit_on_error "restic check" "Restic check failed"
running "Restic Check"
email_and_exit_on_error "restic check" "Restic check failed"
ok

running "removing outdated snapshots"
notify_and_exit_on_error "restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 3 --keep-yearly 3 --prune" "Restic forget failed"
running "Restic Forget"
email_and_exit_on_error "restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 3 --keep-yearly 3 --prune" "Restic forget failed"
ok

0 comments on commit fc538b8

Please sign in to comment.