Ubuntu backups with Restic
Restic is a backup solution for Linux and other systems. I'm using it for automated backups to an external drive, for now. Will also add an offsite solution soon. For now, I'm mostly relying on Dropbox and GitHub.
Install Restic
I downloaded the pre-build binary and installed to /usr/bin/
.
Setup the repository
I used Basic guide on how to use restic for local backups
and restic -h
to set up the repository on my external drive.
This creates the repository for Restic to use for backups.
restic init --repo /media/dcandland/framework-backup/backups
Automate the backups
Following the post from Bithive Automatic Backups with Restic,
and adopting it to my setup, I set up the following in /etc/backup
.
Restic
backup_env
export RESTIC_REPOSITORY="/media/dcandland/framework-backup/backups"
export RESTIC_PASSWORD_FILE="/etc/restic/pw.txt"
backup-excludes.txt
.cache/*
.local/share/[Tt]rash*
*.backup*
.dropbox*
*.log
node_modules
**/.git/*
**/_build/*
**/build/*
**/.svelte-kit/*
**/wire/*
**/.vscode/*
**/.terraform/*
**/tmp/*
Add your repository password to /etc/restic/pw.txt
This script runs the backups from systemd. Adjust as needed.
auto_backup.sh
#!/usr/bin/env bash
# This script is intended to be run by a systemd timer
# Exit on failure or pipefail
set -e -o pipefail
# Set this to any location you like
BACKUP_PATHS="/home/dcandland/"
BACKUP_TAG=systemd.timer
# How many backups to keep.
RETENTION_DAYS=14
RETENTION_WEEKS=16
RETENTION_MONTHS=6
RETENTION_YEARS=1
source /etc/backup/backup_env
# Remove locks in case other stale processes kept them in
restic unlock &
wait $!
# Do the backup
restic backup \
--verbose \
--exclude-file /etc/backup/backup-excludes.txt \
--one-file-system \
--tag $BACKUP_TAG \
$BACKUP_PATHS &
wait $!
# Remove old Backups
restic forget \
--verbose \
--tag $BACKUP_TAG \
--prune \
--keep-daily $RETENTION_DAYS \
--keep-weekly $RETENTION_WEEKS \
--keep-monthly $RETENTION_MONTHS \
--keep-yearly $RETENTION_YEARS &
wait $!
# Check if everything is fine
restic check &
wait $!
echo "Backup done!"
Systemd
I'm not very familiar with Systemd timers, but I resisted using 'cron' so I could learn more about them.
The timer:
/etc/systemd/system/backup.timer
[Unit]
Description=Backup on schedule
[Timer]
OnCalendar=daily
Persistent=true
[Install]
WantedBy=timers.target
The service:
/etc/systemd/system/backup.service
[Unit]
Description=Backup with restic
[Service]
Type=simple
Nice=10
ExecStart=/etc/backup/auto_backup.sh
#$HOME must be set for restic to find /root/.cache/restic/
Environment="HOME=/root"
Add to Systemd
systemctl enable backup.timer --now
Usage
Manually run:
systemctl start backup
View the results:
journalctl -u backup.service
Mount the latest snapshot to view backup:
sudo mkdir /media/dcandland/snapshot
sudo restic mount -r /media/dcandland/framework-backup/backups /media/dcandland/snapshot
Webmentions
These are webmentions via the IndieWeb and webmention.io. Mention this post from your site: