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 c0fc5d9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 42 deletions.
10 changes: 5 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -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
Expand Down
65 changes: 28 additions & 37 deletions scripts/restic.sh
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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 #
Expand Down Expand Up @@ -76,45 +67,45 @@ 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
}

# ##############
# 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

0 comments on commit c0fc5d9

Please sign in to comment.