2021-05-08 14:23:21 +01:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
set -e
|
|
|
|
|
|
|
|
export B2_ACCOUNT_ID="{{ restic_b2_account_id }}"
|
|
|
|
export B2_ACCOUNT_KEY="{{ restic_b2_account_key }}"
|
|
|
|
export RESTIC_PASSWORD="{{ restic_key }}"
|
|
|
|
export RESTIC_REPOSITORY="b2:{{ restic_b2_bucket }}"
|
2021-10-15 12:39:16 +01:00
|
|
|
export GOGC=20 # HACK: Work around for restic's high memory usage https://github.com/restic/restic/issues/1988
|
2021-05-08 14:23:21 +01:00
|
|
|
|
|
|
|
export RESTIC_LOG_DIR="$HOME/log"
|
2021-05-16 15:34:37 +01:00
|
|
|
export RESTIC_LOG_FILE="$RESTIC_LOG_DIR/$1-$(date -Iseconds).log"
|
|
|
|
|
2022-05-09 22:55:22 +01:00
|
|
|
export FORGET_OPTIONS="--keep-daily 60 --keep-monthly 6 --group-by host"
|
2021-05-08 14:23:21 +01:00
|
|
|
|
|
|
|
mkdir -p "$RESTIC_LOG_DIR"
|
|
|
|
|
|
|
|
# Run backup, and capture logs to file
|
|
|
|
cron_backup() {
|
2021-05-16 15:34:37 +01:00
|
|
|
curl -fsS -m 10 --retry 5 -o /dev/null {{ healthchecks_host }}/{{ restic_healthchecks_id }}/start
|
2021-08-30 21:49:58 +01:00
|
|
|
restic --verbose backup --files-from=$HOME/restic-include.txt --exclude-file=$HOME/restic-excludes.txt | tee -a $RESTIC_LOG_FILE
|
2021-05-08 14:23:21 +01:00
|
|
|
exit_code=${PIPESTATUS[0]}
|
2021-05-16 15:34:37 +01:00
|
|
|
curl -fsS -m 10 --retry 5 -o /dev/null {{ healthchecks_host }}/{{ restic_healthchecks_id }}/$exit_code --data-binary "@$RESTIC_LOG_FILE"
|
2021-05-08 14:23:21 +01:00
|
|
|
echo "Exit code: $exit_code"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Run backup, but show all the progress
|
|
|
|
backup() {
|
2021-08-30 21:49:58 +01:00
|
|
|
restic --verbose backup --files-from=$HOME/restic-include.txt --exclude-file=$HOME/restic-excludes.txt
|
2021-05-08 14:23:21 +01:00
|
|
|
}
|
|
|
|
|
2021-05-16 15:34:37 +01:00
|
|
|
{% if restic_forget %}
|
|
|
|
# Run forget and prune, and capture logs to file
|
|
|
|
cron_forget() {
|
|
|
|
curl -fsS -m 10 --retry 5 -o /dev/null {{ healthchecks_host }}/{{ restic_forget_healthchecks_id }}/start
|
2021-06-28 08:33:08 +01:00
|
|
|
restic forget --prune $FORGET_OPTIONS | tee -a $RESTIC_LOG_FILE
|
2021-05-16 15:34:37 +01:00
|
|
|
exit_code=${PIPESTATUS[0]}
|
|
|
|
curl -fsS -m 10 --retry 5 -o /dev/null {{ healthchecks_host }}/{{ restic_forget_healthchecks_id }}/$exit_code --data-binary "@$RESTIC_LOG_FILE"
|
|
|
|
echo "Exit code: $exit_code"
|
|
|
|
}
|
|
|
|
{% endif %}
|
|
|
|
|
2021-05-16 14:39:44 +01:00
|
|
|
# Forget legacy snapshots
|
|
|
|
forget() {
|
|
|
|
set -x
|
2021-09-07 22:04:23 +01:00
|
|
|
restic forget $FORGET_OPTIONS $@
|
2021-05-16 14:39:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Prune orphaned files
|
|
|
|
prune() {
|
|
|
|
set -x
|
|
|
|
restic --verbose prune $@
|
|
|
|
}
|
|
|
|
|
2021-05-08 14:23:21 +01:00
|
|
|
# Run restic, but with environment variables set
|
|
|
|
exec () {
|
|
|
|
set -x
|
|
|
|
restic $@
|
|
|
|
}
|
|
|
|
|
|
|
|
# Run the things
|
|
|
|
"$@"
|