diff --git a/Dockerfile b/Dockerfile index 00ab17e..551999d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,10 +6,10 @@ ARG SUPERCRONIC_URL=https://github.com/aptible/supercronic/releases/download/v0. OVERMIND_URL=https://github.com/DarthSim/overmind/releases/download/v2.4.0/overmind-v2.4.0-linux-amd64.gz ENV TZ="Asia/Shanghai" \ - + # OVERMIND_CAN_DIE=crontab \ OVERMIND_PROCFILE=/Procfile \ - + # ROCKET_PORT=8080 COPY config/crontab \ @@ -32,14 +32,14 @@ RUN apk add --no-cache \ sqlite \ msmtp \ mailx \ - + # && rm -rf /var/cache/apk/* \ && curl -fsSL "$SUPERCRONIC_URL" -o /usr/local/bin/supercronic \ && curl -fsSL "$OVERMIND_URL" | gunzip -c - > /usr/local/bin/overmind \ - + # && ln -sf /usr/bin/msmtp /usr/bin/sendmail \ && ln -sf /usr/bin/msmtp /usr/sbin/sendmail \ - + # && chmod +x /usr/local/bin/supercronic \ && chmod +x /usr/local/bin/overmind \ && chmod +x /restic.sh diff --git a/scripts/restic.sh b/scripts/restic.sh index baa17d7..a3828be 100644 --- a/scripts/restic.sh +++ b/scripts/restic.sh @@ -1,8 +1,14 @@ #!/bin/sh -if [ -z "$RESTIC_PASSWORD" ]; then - exit 1 -fi +# catch the error in case first pipe command fails (but second succeeds) +set -o pipefail +# turn on traces, useful while debugging but commented out by default +# set -o xtrace + +EMAIL_SUBJECT_PREFIX="[Restic]" +LOG="/var/log/restic/$(date +\%Y\%m\%d_\%H\%M\%S).log" + +mkdir -p /var/log/restic/ if [ -n "$SMTP_TO" ]; then cat << EOF > /etc/msmtprc @@ -21,19 +27,8 @@ password $SMTP_PASSWORD EOF fi -# catch the error in case first pipe command fails (but second succeeds) -set -o pipefail -# turn on traces, useful while debugging but commented out by default -# set -o xtrace - -EMAIL_SUBJECT_PREFIX="[Restic]" -LOG="/var/log/restic/$(date +\%Y\%m\%d_\%H\%M\%S).log" - -# create log dir -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 @@ -43,10 +38,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 # @@ -76,12 +67,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 } @@ -89,32 +79,33 @@ function notify_and_exit_on_error() { # ############## # backup steps # # ############## -restic unlock - -running "checking restic config" -run_silently restic cat config +restic unlock +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