Last active
June 9, 2025 05:56
-
-
Save fredrike/bbc32e0e51ace094f8b16c0c73fdfc46 to your computer and use it in GitHub Desktop.
Script to make backups inspired from https://nerdyarticles.com/backup-strategy-with-restic-and-healthchecks-io/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -euo pipefail | |
# Define variables | |
RESTIC_BIN="/usr/bin/restic" | |
RESTIC_PASSWD=".passwd" | |
BACKUP_SOURCE=(Photos Immich) | |
BACKUP_REPO="sftp://localhost:2220//backup_path --limit-upload 8545" | |
WEBHOOK_URL="https://hc-ping.com/<UUID>" | |
KEEP_OPTIONS=(--keep-daily 7 --keep-weekly 6 --keep-monthly 12 --keep-yearly 99) | |
RUN_UUID=$(cat /proc/sys/kernel/random/uuid) | |
CURL_CMD=(curl -fsS -m 10 --retry 5 -o /dev/null -A "$($RESTIC_BIN version)") | |
# Start ping | |
"${CURL_CMD[@]}" "$WEBHOOK_URL/start?rid=$RUN_UUID" | |
# Function to run a command and capture its output | |
run_command() { | |
local CMD=("$@") | |
local TMP_FILE | |
TMP_FILE=$(mktemp) | |
echo "Running: ${CMD[*]}" | tee "$TMP_FILE" | |
{ | |
"${CMD[@]}" | |
} > >(tee -a "$TMP_FILE") 2> >(tee -a "$TMP_FILE" >&2) | |
local STATUS=${PIPESTATUS[0]} | |
if [ "$STATUS" -eq 0 ]; then | |
"${CURL_CMD[@]}" --data-binary @"$TMP_FILE" "$WEBHOOK_URL/log?rid=$RUN_UUID" || \ | |
echo "Warning: failed to send success log" >&2 | |
else | |
"${CURL_CMD[@]}" --data-binary @"$TMP_FILE" "$WEBHOOK_URL/$STATUS?rid=$RUN_UUID" || \ | |
echo "Warning: failed to send error log" >&2 | |
fi | |
rm -f "$TMP_FILE" | |
return "$STATUS" | |
} | |
# Run restic commands | |
run_command "$RESTIC_BIN" -p "$RESTIC_PASSWD" -r "$BACKUP_REPO" unlock || exit $? | |
run_command "$RESTIC_BIN" -p "$RESTIC_PASSWD" -r "$BACKUP_REPO" backup "${BACKUP_SOURCE[@]}" || exit $? | |
run_command "$RESTIC_BIN" -p "$RESTIC_PASSWD" -r "$BACKUP_REPO" forget "${KEEP_OPTIONS[@]}" --prune --cleanup-cache || exit $? | |
# Final success ping | |
"${CURL_CMD[@]}" "$WEBHOOK_URL?rid=$RUN_UUID" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment