Skip to content

Instantly share code, notes, and snippets.

@mstump
Created December 28, 2020 01:21
Show Gist options
  • Save mstump/daa7d25496582b907599a8c626e6866c to your computer and use it in GitHub Desktop.
Save mstump/daa7d25496582b907599a8c626e6866c to your computer and use it in GitHub Desktop.

Revisions

  1. Matt Stump created this gist Dec 28, 2020.
    47 changes: 47 additions & 0 deletions backup.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    #!/bin/bash
    source /root/.restic_keys

    telegramSend() {
    message=$1
    telegramStatus=$?
    if [[ "$telegramStatus" != 0 ]]; then
    echo "Telegram failed to send message: $message"
    fi

    curl -q -X POST \
    -H 'Content-Type: application/json' \
    -d "{\"chat_id\": \"${TELEGRAM_CHAT_ID}\", \"text\": \"${message}\"}" \
    https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage

    telegramStatus=$?
    if [[ "$telegramStatus" != 0 ]]; then
    echo "Telegram failed to send message: $message"
    fi
    }

    checkOutput() {
    output=$($@ 2>&1)
    exitCode=$?

    if [[ $exitCode != 0 ]]; then
    telegramSend "RESTIC BACKUP ERROR:\n${output}"
    exit 1
    else
    echo -e "${output}"
    fi
    }

    echo "$(date) - Starting backup..."
    checkOutput restic \
    backup \
    --verbose \
    --files-from=/root/.restic_files \
    --exclude-file=/root/.restic_exclude \
    --tag automated_cron

    echo "$(date) - Running forget and prune..."
    checkOutput restic forget --prune --keep-daily 7 --keep-weekly 4 --keep-monthly 12

    success="$(date) - Backup finished"
    telegramSend "${success}"
    echo -e "\n${success}"