Last active
June 9, 2025 05:56
-
-
Save fredrike/bbc32e0e51ace094f8b16c0c73fdfc46 to your computer and use it in GitHub Desktop.
Revisions
-
fredrike revised this gist
Jun 9, 2025 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -9,7 +9,7 @@ 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 -
fredrike revised this gist
Jun 9, 2025 . 1 changed file with 34 additions and 29 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,45 +1,50 @@ #!/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" # 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" -
fredrike revised this gist
Jun 4, 2025 . No changes.There are no files selected for viewing
-
fredrike revised this gist
Jun 4, 2025 . 1 changed file with 18 additions and 17 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,44 +1,45 @@ #!/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" -
fredrike revised this gist
Jun 1, 2025 . 1 changed file with 5 additions and 4 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -6,9 +6,10 @@ BACKUP_SOURCE="/<LOCATION_TO_BACKUP>" BACKUP_REPO="<PATH_TO_STORE_BACKUP>/<BACKUP_NAME>" WEBHOOK_URL="https://hc-ping.com/<uuid>" KEEP_OPTIONS="--keep-hourly 2 --keep-daily 6 --keep-weekly 3 --keep-monthly 1" RUN_UUID=$(cat /proc/sys/kernel/random/uuid) # Sending start ping to measure runtime curl -fsS -m 10 --retry 5 "$WEBHOOK_URL/start?rid=$RUN_UUID" # Function to run a command and capture its output to a temp file run_command() { @@ -19,13 +20,13 @@ run_command() { STATUS = $? # Checks the exit status of the last command OUTPUT=$(cat "$TMP_FILE") # Log output to Heathchecks curl -fsS -m 10 --retry 5 --data-raw "$OUTPUT" "$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 -m 10 --retry 5 "$WEBHOOK_URL/$STATUS?rid=$RUN_UUID" exit $STATUS fi } @@ -40,4 +41,4 @@ run_command "restic -p $RESTIC_PASSWD -r $BACKUP_REPO backup $BACKUP_SOURCE" run_command "restic -p $RESTIC_PASSWD -r $BACKUP_REPO forget $KEEP_OPTIONS --prune --cleanup-cache" # Send success curl -fsS -m 10 --retry 5 -o /dev/null "$WEBHOOK_URL?rid=$RUN_UUID" -
fredrike created this gist
Jun 1, 2025 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,43 @@ #!/bin/bash # Define variables RESTIC_PASSWD="/home/<USERNAME>/.restic_password" BACKUP_SOURCE="/<LOCATION_TO_BACKUP>" BACKUP_REPO="<PATH_TO_STORE_BACKUP>/<BACKUP_NAME>" WEBHOOK_URL="https://hc-ping.com/<uuid>" KEEP_OPTIONS="--keep-hourly 2 --keep-daily 6 --keep-weekly 3 --keep-monthly 1" # Sending start ping to measure runtime curl --retry 3 --retry-max-time 120 $WEBHOOK_URL/start # 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" >> "$TMP_FILE" # Logs the command being executed to $TMP_FILE $CMD >> "$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 OUTPUT=$(cat "$TMP_FILE") # Log output to Heathchecks curl -fsS -m 10 --retry 3 --retry-max-time 120 --data-raw "$OUTPUT" $WEBHOOK_URL/log 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 -m 10 --retry 5 $WEBHOOK_URL/$STATUS exit $STATUS fi } # Perform Restic unlock and capture output run_command "restic -p $RESTIC_PASSWD -r $BACKUP_REPO unlock" # Perform Restic backup run_command "restic -p $RESTIC_PASSWD -r $BACKUP_REPO backup $BACKUP_SOURCE" # Perform Restic forget run_command "restic -p $RESTIC_PASSWD -r $BACKUP_REPO forget $KEEP_OPTIONS --prune --cleanup-cache" # Send success curl -fsS -m 10 --retry 5 -o /dev/null $WEBHOOK_URL