Last active
June 4, 2025 14:14
-
-
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 | |
# Define variables | |
RESTIC_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" | |
# Sending start ping to measure runtime | |
$CURL_CMD "$WEBHOOK_URL/start?rid=$RUN_UUID" | |
# Function to run a command and capture its output to a temp file | |
run_command() { | |
local CMD="$1" # Takes the first argument as the command to be executed | |
TMP_FILE=$(mktemp) | |
echo "Running: $CMD" | tee "$TMP_FILE" # Logs the command being executed to $TMP_FILE | |
$CMD | tee -a "$TMP_FILE" 2>&1 # Executes the command and redirects both stdout and stderr to $TMP_FILE | |
STATUS=$? # Checks the exit status of the last command | |
# Log output to Heathchecks | |
$CURL_CMD --data-raw "$(cat $TMP_FILE)" "$WEBHOOK_URL/log?rid=$RUN_UUID" | |
rm "$TMP_FILE" | |
if [ $STATUS -ne 0 ]; then | |
# Sends ping to Heathchecks with the failure code | |
# Deletes the TMP_FILE | |
# Exits the bash script | |
$CURL_CMD "$WEBHOOK_URL/$STATUS?rid=$RUN_UUID" | |
exit $STATUS | |
fi | |
} | |
# Perform Restic unlock and capture output | |
run_command "$RESTIC_BIN -p $RESTIC_PASSWD -r $BACKUP_REPO unlock" | |
# Perform Restic backup | |
run_command "$RESTIC_BIN -p $RESTIC_PASSWD -r $BACKUP_REPO backup $BACKUP_SOURCE" | |
# Perform Restic forget | |
run_command "$RESTIC_BIN -p $RESTIC_PASSWD -r $BACKUP_REPO forget $KEEP_OPTIONS --prune --cleanup-cache" | |
# Send success | |
$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