Skip to content

Instantly share code, notes, and snippets.

@clonejo
Last active October 25, 2021 21:40
Show Gist options
  • Save clonejo/da27d3fe50fd511b588a2b5bdecbbf4c to your computer and use it in GitHub Desktop.
Save clonejo/da27d3fe50fd511b588a2b5bdecbbf4c to your computer and use it in GitHub Desktop.
My scripts for automated restic backups
#!/bin/bash
set -eu
if [[ ${1:-} != "-f" ]]; then
if ! ip route | head -n 1 | grep --quiet 'dev en' ; then
echo "Not connected to Ethernet, skipping backup." >&2
exit 0
fi
ac=$(cat /sys/class/power_supply/AC/online)
if [[ $ac = 0 ]]; then
echo "Running on battery, skipping backup." >&2
exit 0
fi
fi
headerBackupPath=/var/lib/luks-header-backup-nvme0n1p2
rm "$headerBackupPath"
cryptsetup luksHeaderBackup /dev/nvme0n1p2 --header-backup-file "$headerBackupPath"
/usr/bin/pacman -Qe > /etc/pacman.d/installed-packages
/usr/bin/restic \
-r sftp:restic-target:/backups/restic-repo \
--password-file=/etc/restic-key \
backup /etc /home /root /usr/local/bin \
--tag=full \
--exclude-caches \
--exclude=/home/clonejo/.cache \
--exclude=/home/clonejo/.cargo/registry \
--exclude=/home/clonejo/.local/share \
--exclude=/home/clonejo/.rustup/toolchains \
--exclude=/home/clonejo/Downloads \
--exclude=target/classes \
--exclude=target/debug \
--exclude=target/doc \
--exclude=target/release
date --iso-8601=seconds >/var/lib/restic-last-successfull-backup
# That file can be used to trigger warnings about your backups not working.
[Unit]
Description=restic backups
[Service]
Type=oneshot
Nice=10
Environment=HOME=/root
ExecStart=/usr/local/bin/restic-backup
[Unit]
Description=Run restic backup every three hours
[Timer]
OnBootSec=10min
OnUnitInactiveSec=3h
[Install]
WantedBy=timers.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment