It is said that there are two kinds of people: those who do the backups and those who will. Once you suffer from data loss, you will remember to perform backups regularly and to – this is also very important – check them on regular basis. This article is focused on Linux based backup tool named Borg.
What is Borg?
Borg, or using the full name, BorgBackup is the tool to help you create secure backups but also to keep them small. Borg is taking care of de-duplication and also provides an ability to compress and to encrypt your backups if you are storing them in the not-so-safe environment. It allows you to store your backups locally or remotely, but in this article, I will focus on the local usage.
I will provide you with the real-life example of usage on the typical Linux machine, along with the cron jobs I’m running on the daily basis.
Installation and initialization of your first archive
Borg is supported in most of the Linux distributions currently, so you can simply run
sudo apt install borgbackup
Once installed, you will have to choose the place for your backup repository. Borg is not simply copying files but is creating the place and structure to store them properly. This is why the repository needs to be created first. Once you will know the place, you can run this initialization command:
borg init /my/backup/directory
You will be asked whether you want to protect your backup with the password or not. The password is not necessary, you can leave it empty.
Creating a backup
Now, once you have your repository initialized, you can create your first backup.
borg create -sv /my/backup/directory/::Initial20180612 /etc /home
Please note that I used the backup name – just after the backup directory there is “::” and the name of the backup – in this example “Initial20180612”. Two directories are added to the repository – “/etc” and “/home”.
Listing backup contents and restoring data
In order to list backups stored in the repository, you should use this command:
borg list /my/backup/directory/
It will give you the list of backup names along with the date and time of creation. To review contents of the particular backup, use the backup name:
borg list /my/backup/directory::Initial20180612
If you want to extract contents of your archive, you should create a new directory and extract files to such empty place, not directly to the source file system – just in case something went wrong. Assuming that you already created such temporary directory and you are in it, you can run the following command to extract all files from the particular archive:
borg extract /my/backup/directory::Initial20180612
If you want to restore only a particular file (let’s say that you want to restore “home/username/filename”) you can use:
borg list /my/backup/directory::Initial20180612 home/username/filename
Instead of the particular file name, you can use pattern.
Automatization and old backups pruning
I’m performing my backups daily (and in some cases even more frequently). This means that I’m performing them automatically and I’m also automatically removing old archives to save disk space. Below you will find my sample backup script which is run as a cron job every night.
#!/bin/sh ### System Setup ### NOW=$(date +"%Y%m%d") ### Start Backup ### echo Backing up files >> $NOW.log borg create -sv /my/backup/directory::$NOW /my/source/directory/ 2>> $NOW.log echo Pruning >> $NOW.log borg prune -sv -d7 -w4 -m12 --show-rc /my/backup/directory/ 2>> $NOW.log
As you can see, I’m using current date for various things. It is used as the source of my backup name (“/my/backup/directory::$NOW”) but also as the name of the log file of the backup. Please note that Borg is sending output data to stderr, this is why the redirect to the log file looks like this “2>> $NOW.log”.
Let’s take a look at the prune options – I defined various retention options. My configuration is “-d7 -w4 -m12” which means: keep seven daily backups, four weekly backups, and twelve monthly backups. This gives me an ability to restore files long deleted, but not to spare too much of my disk space on the old archives. There are also “hourly” and “yearly” retention periods, but these are not in use by me.
As you can see, Borg is rather simple to use and because of its built-in de-duplication, it is very efficient. It allows me to store archives for a long time period while keeping them clear and ready to use at any moment.