Backing Up With Borg


Photo by Viktor Talashuk on Unsplash

With Linux systems, there are many options for creating and maintaining backups for your files. In the earlier post, we introduced rsync, a tool that transfers and synchronizes files between machines on a network. Today, let’s focus on another useful back up tool: BorgBackup.

BorgBackup, or “Borg” for short, is a backup program that supports deduplication, compression, and encryption. Borg provides an efficient and secure method for backing up data.

Why Use Borg?

Due to Borg’s deduplication feature, it only stores changes between directories, which accelerates the backup process. Borg also provides an option for compressing files and directories, so that backups are efficient in terms of storage requires.

Finally, Borg supports the encryption of files on the client-side. This means that users can back up to storage spaces they do not fully trust since their data gets encrypted before it is transferred.

Install BorgBackup

BorgBackup is not installed on Linux systems by default. So, to use it, Borg first needs to be installed. This can be done with the following, single command.

sudo apt-get install borgbackup

Create a Backup Repository

Now that we have Borg installed, let’s proceed to backing up files. Before backups can be stored, a Borg backup repository need to be initiated.

First, use the --encryption flag to specify the encryption mode. You can take a look at the detail of each encryption mode here.

borg init --encryption=repokey /path/to/backup_repo

Borg will then prompt the user to enter the passphrase that should be used to encrypt the repository.

Create a Backup

Now, let’s go ahead and create our first backup! You can create a backup of the source_directory into a backup archive called “archive1” into the backup_repo:

borg create /path/to/backup_repo::archive1 /path/to/source_directory

You can also use the --compression flag to create a compressed backup archive.

borg create --compression COMPRESSION_ALGORITHM,COMPRESSION_LEVEL /path/to/backup_repo::archive1 /path/to/source_directory

Borg gives you four COMPRESSION_ALGORITHMs to choose from, lz4, zstd,zlib and lzma. Compression is lz4 by default. COMPRESSION_LEVEL can range from 0 to 9, with 9 being the highest. For example, to use zlib with the highest compression level, you can run:

borg create --compression zlib,9 /path/to/backup_repo::archive1 /path/to/source_directory

Recover a Backup

It is a good idea to familiarize yourself with the process of restoring a backup. Thankfully, Borg makes that process fast and easy.

The first step is to list all of the backup archives in the target backup repository by using this command:

borg list /path/to/backup_repo

Borg will list all the backup archives stored in the repository in output that looks like this:

archive1                               Thu, 2020-04-23 01:20:30

archive2                               Fri, 2020-04-24 01:20:30

The contents within an archive can also be listed by using the following command.

borg list /path/to/backup_repo::archive1

To extract a backup and download its files to the current directory use:

borg extract /path/to/backup_repo::archive1

Before extracting a particular archive file, a user may want to compare different archives to find the right version he or she wants to use. Archives can be compared without extracting them by using:

borg diff /path/to/backup_repo::archive1 archive2

Borg can also be used to operate on a remote backup directory. All syntax would be the same, but the user will need to specify the username and server in the path to the backup repository.

borg extract username@server:/path/to/backup_repo::archive1

Mount a Backup Repository

Finally, if a user is uncertain about which files to restore and needs to look into the files to find the answer, he or she can mount an archive or the entire backup directory. This will allow the user to browse the archive and restore individual files.

To mount a remote directory, a user must first create a writable local directory. After that is created, the user can go ahead and mount the repository using the following commands.

mkdir /tmp/mount

borg mount username@server:/path/to/backup_repo /tmp/mount

After operating on the backup repository has been completed, it can be unmounted using:

borg umount /tmp/mount

Conclusion

Borg offers efficient ways to handle a range of file backup tasks. While there is a lot more to Borg, we’ve covered most of its basic uses in this post. Remember, if users run into trouble, they can always get some help by using the command borg help, or by visiting Borg’s documentation page!

Vickie Li