How to Backup and Restore Files Using Borg in Linux


backing up our data is one of the healthiest ways to preserve the integrity of our system. Therefore, today you will learn How to Backup and Restore Files Using Borg in Linux. This application is great and with it, our files will be safe from any danger.

Introducing to Borg

Borg is an open-source application that allows you to backup your data using deduplicating. This allows the application to create incremental backups. This means that only new files or files that have been modified are stored in the new backups.

This deduplicating system makes Borg the ideal tool to create backups daily since the size of the backups will increase according to the changes.

Borg is not a new application but has been on the market for a long time. Its use is through the terminal so its use may scare more than one, but the reality is that it is quite simple.

Another of the main advantages of Borg is that it is cross-platform so we can install it on almost any system. So we do not have to worry about the installation on Linux.

So, let’s get started.

Install Borg in Linux

Borg is present in the official repositories of all major Linux distributions. So, open a terminal and run the command indicated for your distro

For Debian, Ubuntu, Linux Mint, and derivatives of these distributions run

sudo apt install borgbackup

If you are using Arch Linux, Manjaro, or other derivatives

sudo pacman -S borg

But if you use RHEL, Fedora, CentOS, and derivates

sudo dnf install borgbackup

Finally, in the case of OpenSUSE

sudo zypper in borgbackup

As you can see the installation is quite simple and will help us with the process.

How to Backup and Restore Files Using Borg in Linux

Backup files with Borg

First of all, we have to make a new repository to create the backup file.

To create the new repository then run the following command line

 sudo borg init --encryption=repokey /home/user/repository

When you run the above command, you will be prompted for a passphrase, which is like a password, which you must remember.

The name and location of the repository can be chosen by you without any problem. Borg supports several types of encryption in this case we chose repokey but you can also use none.

Making the backup file

With the repository created, you then have to start backing up the folder you want. In this case, I will use the /home/user/Pictures directory as an example.

Borg uses the concept of Files to identify each backup we make. This “File” is used to keep track of the backup and should be given an explicit name to avoid confusion.

 sudo borg create /home/user/repository::first-backup /home/user/Pictures

As soon as you start the command you will be prompted for the passphrase.

Now a new file named first-backup will be created.

By default, Borg uses the lz4 cipher but you can use zlib, lzma, and none. In the case of the first two, we have to specify a number from 0 to 9 indicating the compression level.

An example would be

 sudo borg create --compression zlib,8 /home/user/repository::first-backup /home/user/Pictures

So we would be using the zlib cipher with level 8 compression. Almost the maximum.

And so on you have to create the backup using these files. For example, if in several days you make a backup again you have to run again the command sudo borg create but specifying another name for the file.

To list all the “files” in a repository run

 sudo borg list /home/user/repository

So, to list the contents of one of these files

 sudo borg list /home/user/repository::[file-name]

Restoring files with Borg in Linux

To restore a backup with Borg you have to use the extract command in the directory where you want the restore to be done.

Let’s suppose we want to restore the backup we just made in the directory /media/my-disk/Pictures/.

cd /media/my-disk/Pictures/
sudo borg extract /home/user/repository::first-backup

For this to work, you need to have access to the first-backup file and the /media/my-disk/Pictures/ folder.

Other borg command options

If you want to delete a backup “file” then you have to use the delete command and specify the file. For example

sudo borg delete /home/user/repository::first-backup

You can also rename it

sudo borg reaname /home/user/repository::first-backup new-first-backup

Another important aspect is that you can mount the backup file and verify it

borg mount /home/user/repository::first-backup /path/to/mount/the/backup

You can then browse the mounted folder and verify your files.

To unmount it

borg umount /path/to/mount/the/backup

So you can use Borg.

Conclusion

Borg is a great tool for our backups. It uses an interesting technology that will allow us to take full advantage and security of our directories.

Help us grow and buy this post.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top