Skip to content

Instantly share code, notes, and snippets.

@fredrike
Last active June 9, 2025 05:56
Show Gist options
  • Select an option

  • Save fredrike/bbc32e0e51ace094f8b16c0c73fdfc46 to your computer and use it in GitHub Desktop.

Select an option

Save fredrike/bbc32e0e51ace094f8b16c0c73fdfc46 to your computer and use it in GitHub Desktop.

Revisions

  1. fredrike revised this gist Jun 9, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion restic-backup.sh
    Original 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"
    CURL_CMD=(curl -fsS -m 10 --retry 5 -o /dev/null -A "$($RESTIC_BIN version)")


    # Start ping
  2. fredrike revised this gist Jun 9, 2025. 1 changed file with 34 additions and 29 deletions.
    63 changes: 34 additions & 29 deletions restic-backup.sh
    Original file line number Diff line number Diff line change
    @@ -1,45 +1,50 @@
    #!/bin/bash
    set -euo pipefail

    # Define variables
    RESTIC_BIN="restic"
    RESTIC_BIN="/usr/bin/restic"
    RESTIC_PASSWD=".passwd"
    BACKUP_SOURCE="Photos Immich"
    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"
    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
    # Start ping
    "${CURL_CMD[@]}" "$WEBHOOK_URL/start?rid=$RUN_UUID"

    # Function to run a command and capture its output
    run_command() {
    local CMD="$1" # Takes the first argument as the command to be executed
    local CMD=("$@")
    local TMP_FILE
    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"
    echo "Running: ${CMD[*]}" | tee "$TMP_FILE"

    {
    "${CMD[@]}"
    } > >(tee -a "$TMP_FILE") 2> >(tee -a "$TMP_FILE" >&2)

    # Perform Restic backup
    run_command "$RESTIC_BIN -p $RESTIC_PASSWD -r $BACKUP_REPO backup $BACKUP_SOURCE"
    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"
    }

    # Perform Restic forget
    run_command "$RESTIC_BIN -p $RESTIC_PASSWD -r $BACKUP_REPO forget $KEEP_OPTIONS --prune --cleanup-cache"
    # 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 $?

    # Send success
    $CURL_CMD "$WEBHOOK_URL?rid=$RUN_UUID"
    # Final success ping
    "${CURL_CMD[@]}" "$WEBHOOK_URL?rid=$RUN_UUID"
  3. fredrike revised this gist Jun 4, 2025. No changes.
  4. fredrike revised this gist Jun 4, 2025. 1 changed file with 18 additions and 17 deletions.
    35 changes: 18 additions & 17 deletions restic-backup.sh
    Original file line number Diff line number Diff line change
    @@ -1,44 +1,45 @@
    #!/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"
    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 -fsS -m 10 --retry 5 "$WEBHOOK_URL/start?rid=$RUN_UUID"
    $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
    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")
    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 -fsS -m 10 --retry 5 --data-raw "$OUTPUT" "$WEBHOOK_URL/log?rid=$RUN_UUID"
    $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 -m 10 --retry 5 "$WEBHOOK_URL/$STATUS?rid=$RUN_UUID"
    $CURL_CMD "$WEBHOOK_URL/$STATUS?rid=$RUN_UUID"
    exit $STATUS
    fi
    }

    # Perform Restic unlock and capture output
    run_command "restic -p $RESTIC_PASSWD -r $BACKUP_REPO unlock"
    run_command "$RESTIC_BIN -p $RESTIC_PASSWD -r $BACKUP_REPO unlock"

    # Perform Restic backup
    run_command "restic -p $RESTIC_PASSWD -r $BACKUP_REPO backup $BACKUP_SOURCE"
    run_command "$RESTIC_BIN -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"
    run_command "$RESTIC_BIN -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"
    $CURL_CMD "$WEBHOOK_URL?rid=$RUN_UUID"
  5. fredrike revised this gist Jun 1, 2025. 1 changed file with 5 additions and 4 deletions.
    9 changes: 5 additions & 4 deletions restic-backup.sh
    Original 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 --retry 3 --retry-max-time 120 $WEBHOOK_URL/start
    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 3 --retry-max-time 120 --data-raw "$OUTPUT" $WEBHOOK_URL/log
    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
    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
    curl -fsS -m 10 --retry 5 -o /dev/null "$WEBHOOK_URL?rid=$RUN_UUID"
  6. fredrike created this gist Jun 1, 2025.
    43 changes: 43 additions & 0 deletions restic-backup.sh
    Original 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