Backup Notifications to Telegram: restic, BorgBackup and rsync

Backup Notifications to Telegram: restic, BorgBackup and rsync

push.tg

A backup that silently stopped working is worse than no backup, because you only find out on the day you need it. The fix is cheap: make every backup run send one line to Telegram, OK or FAILED. Then a missing morning message is your alarm too.

Step 1: get your endpoint (30 seconds)

Open @trafficRobot in Telegram and press Start. You get an address like k3x9q@push.tg. Your endpoint is the part before the @: https://push.tg/k3x9q. Every example below uses k3x9q, so swap in your own key.

There's no bot to create, no chat_id to look up and no token to keep safe. Add @trafficRobot to a group and the group gets its own address, so everyone who looks after the machines sees the messages.

restic

Wrap the run in a small script so you get the result either way:

#!/bin/sh
# /usr/local/bin/restic-run.sh
if out=$(restic backup /home /etc 2>&1); then
  curl -s -d "restic OK on $(hostname): $(echo "$out" | tail -n 1)" https://push.tg/k3x9q
else
  curl -s -d "restic FAILED on $(hostname): $(echo "$out" | tail -n 5)" https://push.tg/k3x9q
fi

The last line of a successful restic run names the snapshot it saved, so the OK message doubles as a receipt. Keep your usual RESTIC_REPOSITORY and RESTIC_PASSWORD_FILE environment in the script or the cron line.

BorgBackup with borgmatic

borgmatic can run commands after a backup or when something goes wrong. In borgmatic 2.x use the commands: section:

commands:
    - after: error
      run:
          - curl -s -d "borgmatic FAILED on $(hostname)" https://push.tg/k3x9q

Older borgmatic versions use the on_error: hook for the same thing:

on_error:
    - curl -s -d "borgmatic FAILED on {repository}: {error}" https://push.tg/k3x9q

If you call plain borg create from a script, use the same if/else pattern as the restic example.

rsync

rsync -a --delete /srv/data/ backup-host:/backups/data/ \
  && curl -s -d "rsync OK on $(hostname)" https://push.tg/k3x9q \
  || curl -s -d "rsync FAILED on $(hostname) (exit $?)" https://push.tg/k3x9q

Schedule it

# crontab -e
30 3 * * * /usr/local/bin/restic-run.sh

Cron runs with a minimal environment, so use absolute paths. If you'd rather only hear about failures, drop the OK branch, but then add a weekly "still alive" message so you notice if the job stops running at all.

Send the disk usage along

df -h /backups | curl -s --data-binary @- https://push.tg/k3x9q

A full backup disk is the most common reason backups start failing, so it's worth a weekly look.

Check that the message really arrived. push.tg answers HTTP 200 even when the key is wrong; the body then says {"error":"no such user"}. Run each command by hand once and look at Telegram before you trust it.

Open @trafficRobot · push.tg

More copy-paste guides

Report Page