OpenBSD backups with restic and /etc/daily.local
This is yet another one of the guides on how to do backups for a server, this time with restic and OpenBSD. This sets up a daily backup and mails the changes to root.
Install restic
This is the simplest part:
$ pkg_add restic
Setup a restic repository
From the official instructions, the example here sets up an sftp repository over ssh. For this to work, root
must have access to the backup server using its ssh key.
$ cat /root/.ssh/config
Host truenas
HostName 192.168.4.2
User restic
IdentityFile /root/.ssh/id_ed25519
$ restic --repo sftp:truenas:/mnt/Media/backups/fwall init
Store the password that was used in plain text in /root/.ssh/restic
:
$ ls -lh /root/.ssh/restic
-rw------- 1 root wheel 18B Dec 24 20:32 /root/.ssh/restic
$ cat /root/.ssh/restic
SomeReallyCoolResticPassword
NOTE: Make sure that the ssh user, restic
, has full access to the backup folder.
Setup /etc/daily.local
Create a new file /etc/daily.local.restic
with the contents:
# backup restic
R_FILE="/root/.ssh/restic"
R_REPO="sftp:truenas:/mnt/Media/backups/fwall"
env RESTIC_PASSWORD_FILE="${R_FILE}" \
HOME="/root" \
/usr/local/bin/restic --repo ${R_REPO} \
--verbose backup \
--exclude-if-present=no_restic \
--exclude-file=/etc/restic.exclude \
--files-from=/etc/restic.include \
--tag="$(date +%c)"
# list changes
PREV=$(env RESTIC_PASSWORD_FILE="${R_FILE}" HOME="/root" \
/usr/local/bin/restic --repo ${R_REPO} \
snapshots --compact | tail -4 | head -1 | awk '{print $1}')
LAST=$(env RESTIC_PASSWORD_FILE="${R_FILE}" HOME="/root" \
/usr/local/bin/restic --repo ${R_REPO} \
snapshots --compact | tail -3 | head -1 | awk '{print $1}')
RDIFF_FILE="$(mktemp)"
env RESTIC_PASSWORD_FILE="${R_FILE}" HOME="/root" \
/usr/local/bin/restic --repo ${R_REPO} \
diff ${PREV} ${LAST} > ${RDIFF_FILE}
NLINES=$(wc -l "${RDIFF_FILE}" | awk '{print $1}')
if [ ${NLINES} -gt 108 ] ; then
head -n 100 "${RDIFF_FILE}"
printf "======= SNIP ======\n"
tail -n 8 "${RDIFF_FILE}"
else
cat "${RDIFF_FILE}"
fi
rm -f "${RDIFF_FILE}"
unset R_REPO R_FILE RDIFF_FILE NLINES
And in the /etc/daily.local
, add a line which sources /etc/daily.local.restic
:
...
{other personal scripts}
...
. /etc/daily.local.restic
This sets up the daily script, which backs up the whole system. We still need to create the config files /etc/restic.include
and /etc/restic.exclude
.
/etc/restic.include
:
/etc
/root
/home
/usr
/bin
/sbin
/var
/etc/restic.exclude
:
/var/run
/var/spool
/var/tmp
Any folder which contains a file named no_restic
is also excluded. For example, if a user has multiple git repositories in /home/aisha/GIT/
and they wish to avoid backups of this folder, create a file /home/aisha/GIT/no_restic
, an empty file is fine, it can also contain the reason for excluding this folder.
$ locate no_restic
/home/aisha/GIT/no_restic
$ cat /home/aisha/GIT/no_restic
no need to do backups of online version controlled folders
Test backup
To test it out, first do a full back of the system:
$ env -i R_FILE="/root/.ssh/restic" \
R_REPO="sftp:truenas:/mnt/Media/backups/fwall" \
RESTIC_PASSWORD_FILE="${R_FILE}" \
HOME="/root" \
/usr/local/bin/restic --repo ${R_REPO} \
--verbose backup \
--exclude-if-present=no_restic \
--exclude-file=/etc/restic.exclude \
--files-from=/etc/restic.include \
--tag="$(date +%c)"
Now do a test run of /etc/dailly.local.restic
with:
$ sh /etc/daily.local.restic
open repository
repository f719d564 opened successfully, password is correct
lock repository
load index files
using parent snapshot 05b3f0dd
start scan on [/etc /root /home /usr /bin /sbin /var]
start backup on [/etc /root /home /usr /bin /sbin /var]
scan finished in 2.230s: 35818 files, 2.111 GiB
Files: 0 new, 10 changed, 35808 unmodified
Dirs: 0 new, 12 changed, 1859 unmodified
Data Blobs: 8 new
Tree Blobs: 11 new
Added to the repo: 817.516 KiB
processed 35818 files, 2.111 GiB in 0:05
snapshot 11d4745c saved
comparing snapshot 05b3f0dd to 11d4745c:
M /root/.local/share/nvim/shada/main.shada
M /var/cron/log
M /var/cron/log.0.gz
M /var/cron/log.1.gz
M /var/cron/log.2.gz
M /var/log/daemon
M /var/log/dhcpd
M /var/log/messages
M /var/log/pflog
M /var/log/rad
Files: 0 new, 0 removed, 10 changed
Dirs: 0 new, 0 removed
Others: 0 new, 0 removed
Data Blobs: 8 new, 8 removed
Tree Blobs: 10 new, 10 removed
Added: 815.139 KiB
Removed: 821.155 KiB
Congratulations
This sets up proper daily backups for the server, with blazing speed.