I’ve setup some automated backups on my laptop (Ubuntu 22.04) to my NAS (Synology, but it shouldn’t really matter). Here is a guide about how I did it.
Notes: In this article, my remote drive IP will be 192.168.1.10. It’s best to use a DNS entry for more flexibility.
The remote service account is called borgbackup, the remote folder is /homes/borgbackup/backup, and my local user is remi, so you’ll have to change the command lines to your needs.
It’s best to also have a remote copy of your backups, in case of a fire for example.
Setting up borg backup
We will need to mount the remote drive where the backups will be stored. So the credentials will be in a protected file in /var/borgbackup-credentials (sudo touch /borgbackup-credentials
) containing your remote user username and password:
username=borgbackup
password=abcd123
Change the file permissions sudo chmod 600 /var/borgbackup-credentials
Mount the remote so we can initialise borg backup: sudo mount -t cifs -o credentials=/var/borgbackup-credentials //192.168.1.10/homes/borgbackup/backup /mnt/borgbackup
Init the borg repo : sudo borg init --encryption=repokey-blake2 /mnt/borgbackup
Now create a file that will contain all the paths to exclude from the backup, I’ve put mine in a new folder in /home/remi/.borgbackup/exclude-list.txt:
/home/*/.thumbnails
/home/*/.cache
/home/*/.local/share/Trash
/home/*/.mozilla/firefox/*.default/Cache
/home/*/.gvfs
To run the backup you will need to mount the remote drive then run the borg create command. I’ve wrote a script that does it below.
Run backups daily with a cron job
For your cron job to be able to run the backup, you need to store your password somewhere (rather than keeping it visible directly in the command line). In the ~/.borgbackup folder, I’ve created a .borg-passphrase file, added my borg password and then changed the permissions to allow only admin to open/edit it (chmod 600 ~/.borgbackup/.borg-passphrase
).
I’ve also created a cron script (~/.borgbackup/backup-cron-script.sh), that mounts the remote then creates at backup:
#!/usr/bin/env bash
set -ev
# Mount the NAS drive
mkdir -p /mnt/borgbackup
mount -t cifs -o credentials=/var/borgbackup-credentials //192.168.1.10/homes/borgbackup/backup /mnt/borgbackup
# Start backup
export BORG_PASSCOMMAND="cat /home/remi/.borgbackup/.borg-passphrase"
borg create /mnt/borgbackup::{now} /etc /home /root /var /usr/local /srv /opt --exclude-from /home/remi/.borgbackup/exclude-list.txt --compression zstd --stats
# Keep 1 backup per day over the last 7 days, 1 backup per week over the last 4 weeks and 1backup per months over the last 6 months
borg prune -v --list --stats --keep-daily=7 --keep-weekly=4 --keep-monthly=6 /mnt/borgbackup
umount /mnt/borgbackup
Finally I’ve got a separate script that compresses the repo, called cleanup-cron-script.sh (might not be the best name, I admit):
#!/usr/bin/env bash
set -ev
# Unmount potential mounted drive
umount /mnt/borgbackup || true
# Mount the NAS drive
mkdir /mnt/borgbackup
mount -t cifs -o credentials=/var/borgbackup-credentials //192.168.1.10/homes/borgbackup/backup /mnt/borgbackup
# Start cleanup
export BORG_PASSCOMMAND="cat /home/remi/.borgbackup/.borg-passphrase"
borg compact --progress --cleanup-commits /mnt/borgbackup
umount /mnt/borgbackup
You can then add you jobs to cron using sudo contab -e
and adding
00 09 * * * sh -x /home/remi/.borgbackup/cron-script.sh > /home/remi/.borgbackup/cron-log.txt 2>&1
Your backups will run everyday at 9am, and the logs output stored in the .borgbackup/backup-cron-log.txt file.
And for the cleanup script to run monthly at 8am:
00 08 1 * * sh -x /home/remi/.borgbackup/cleanup-cron-script.sh > /home/remi/.borgbackup/cleanup-cron-log.txt 2>&1
Retrieving a backup
Access your files by mounting the repository as a volume:
# Create a folder that will contain the backups
mkdir archive
# Mount the remote repo
sudo mount -t cifs -o credentials=/var/borgbackup-credentials //192.168.1.10/homes/borgbackup/backup /mnt/borgbackup
# Then mount it locally
sudo borg mount /mnt/borgbackup archive
Resources
Borgbackup documentation: https://borgbackup.readthedocs.io/en/stable/
Thanks to sebsauvage for his wiki page (in french): https://sebsauvage.net/wiki/doku.php?id=borgbackup