- Technical difficulty: ★★★☆☆
- Server OS covered: Debian, Ubuntu
- Client OS covered: Any Unix-like operating system
Borg Backup is a deduplicating archiver with compression and authenticated encryption capabilities, making it an excellent choice for this purpose. This guide will walk you through setting up Borg Backup on your home server and creating a backup strategy that suits your needs.
Prerequisite knowledge
- Using Unix 101
- Backups 101
What will be covered
In this guide, you will learn the basics on how to:
- Install Borg backup
- Set up a backup repository on a remote server
- Perform backups remotely
- List and manage backups
- Check the integrity of backups
- Restore files from backups
Why use Borg Backup?
Borg Backup is a powerful tool for creating backups of your data. Here are some reasons why you should consider using Borg Backup:
- Deduplication: Borg Backup uses deduplication to store only unique data blocks, reducing the storage space required for backups. For example, if you take 10 snapshots of the same folder over time, Borg will only store the unique data blocks, instead of using 10 times the storage space.
- Compression: Borg Backup compresses data before storing it, further reducing the storage space needed for backups.
- Encryption: Borg Backup can optionally encrypt your data to ensure that it is secure and protected from unauthorized access.
- Powerful features: Borg Backup provides features like pruning old backups, verifying data integrity, and restoring files from specific archives.
Video guide
Written guide
Installation
First, you need to install Borg Backup on both the source machine (the machine you want to back up) and the target machine (the machine where you want to store the backups).
The installation process may vary depending on your operating system. For most Linux distributions, you can install Borg using the package manager.
For Debian/Ubuntu:
sudo apt install borgbackup
Instructions for other operating systems can be found in the Borg Backup documentation.
Initializing a Borg Repository
A Borg repository is where your backups will be stored. To create a repository, use the following command, replacing /path/to/your/repository
with the desired location on your server or on an external storage device.
borg init --encryption=repokey /path/to/your/repository
This command initializes a new Borg repository with encryption. The repokey
mode encrypts your data with a passphrase. Remember this passphrase, as you will need it to access your backups.
If you are using a remote server, you can create a repository on the server using SSH. For example, to create a repository in the ~/backups
directory on a server accessible at my_server
, using the username bob
, you would run:
borg init --encryption=repokey bob@my_server:~/backups
Doing your first backup
To create a backup, you need to specify what files or directories you want to include in the backup. The following command creates a new archive in your repository:
borg create --progress --stats bob@my_server:~/backups::"My first backup" ~/photos
In this example, the ~/photos
directory of the local machine is backed up to the repository, into an archive named "My first backup".
You will notice that the flags --progress
and --stats
are used. If you are running the command interactively, these flags provide real-time progress information and statistics about the backup process, which can be helpful for monitoring the backup.
Pro Tip If you are running this command on a remote machine via an SSH connection, and you believe the backup process might take a long time, consider learning and using the
screen
command ortmux
to keep the process running even if the SSH connection is interrupted.
Doing subsequent backups
For subsequent backups, you can create new archives with different names to keep track of the changes over time, but otherwise the process is the same.
You can also use {hostname}
and {now}
placeholders in the archive name to automatically include the hostname and the current date and time in the archive name. For example:
sudo borg create bob@my_server:~/backups::'photos-{now}' ~/photos --stats --progress
Listing your backups
To list the archives in your repository, use the following command:
borg list username@hostname:/path/to/your/repository
In our example, this would be:
borg list bob@my_server:~/backups
This will show you a list of all the archives in the repository, along with their creation dates and fingerprints.
You can also list the contents of a specific archive by adding the archive name after the repository path:
borg list bob@my_server:~/backups::"My first backup"
This will show you the files and directories included in the specified archive.
Checking the integrity of your backups
To check the integrity of your backups, you can use the check
command:
borg check username@hostname:/path/to/your/repository
In our example, this would be:
borg check bob@my_server:~/backups --verbose
Notice that the --verbose
flag is added in the example. This is a nice option when running the command interactively, as it provides more detailed information about the integrity check.
This command will run some integrity checks on the repository and its archives.
However, once in a while, it might also be a good idea to run this with the --verify-data
flag, which will verify the data integrity byte by byte to see if any data corruption has occurred — for example when the hard drive has started to fail. But only do this sporadically, as that command takes up a long time and computing resources.
Restoring Files from Backup
In case you need to restore files, Borg allows you to extract files from a specific archive.
- First, list all archives in your repository:
borg list /path/to/your/repository
- Then, list the contents of a specific archive:
borg list /path/to/your/repository::the_archive_name
This will show you the files and directories included in the specified archive. You will need to select the files or directories in the archive that you want to restore.
- Then, extract the needed files or directories from a specific archive:
borg extract /path/to/your/repository::the_archive_name /path/to/restore/in/the/archive
Caution The path
/path/to/restore/in/the/archive
is the path inside the archive that you saw in step 2. This is NOT the path where you will dump the files to on your local machine. The extract command will dump the files to the current directory, so make sure you are in the right directory when running the command.Consider the following example:
daniel@client_machine: / $ cd ~ daniel@client_machine: ~ $ ls ~ photos test.txt daniel@client_machine: ~ $ rm -r ~/photos daniel@client_machine: ~ $ ls ~ test.txt daniel@client_machine: ~ $ borg list daniel@server_machine:~/backups My first backup Fri, 2024-06-21 00:25:16 [6cc2b584c5bcd169fb784aa5a506fe93f6865333c57742e56e8f4bba45dcc804] Photos - 2024-06-21T00:28:01 Fri, 2024-06-21 00:28:02 [7ccf707be135e77881927e2e8d344eab94670d58ec6937c8dba2d366c0e85b4e] daniel@client_machine: ~ $ borg list daniel@server_machine:~/backups::"Photos - 2024-06-21T00:28:01" drwxr-xr-x daniel daniel 0 Fri, 2024-06-21 00:27:20 home/daniel/photos -rw-r--r-- daniel daniel 6148 Fri, 2024-05-03 00:17:26 home/daniel/photos/.DS_Store -rw-r--r-- daniel daniel 1994640 Fri, 2024-05-03 00:16:56 home/daniel/photos/photo1.jpg -rw-r--r-- daniel daniel 1948696 Fri, 2024-05-03 00:17:06 home/daniel/photos/photo2.jpg -rw-r--r-- daniel daniel 3219085 Fri, 2024-05-03 00:16:29 home/daniel/photos/photo3.jpg -rw-rw-r-- daniel daniel 0 Fri, 2024-06-21 00:27:20 home/daniel/photos/photos4.jpg daniel@client_machine: ~ $ borg extract daniel@server_machine:~/backups::"Photos - 2024-06-21T00:28:01" home/daniel/photos daniel@client_machine: ~ $ ls ~ home test.txt daniel@client_machine: ~ $ find ~/home /home/daniel/home /home/daniel/home/daniel /home/daniel/home/daniel/photos /home/daniel/home/daniel/photos/.DS_Store /home/daniel/home/daniel/photos/photo1.jpg /home/daniel/home/daniel/photos/photo2.jpg /home/daniel/home/daniel/photos/photo3.jpg /home/daniel/home/daniel/photos/photos4.jpg
Notice how because the paths in the archive were absolute, and we extracted while in the home directory, we ended up with a
home
directory inside thehome
directory. This is why it is important to be in the right directory when extracting files, or to use relative paths when creating the archive.
Conclusion
Borg Backup is a powerful tool for creating secure and efficient backups of your home server data. By following this guide, you can set up a robust backup strategy that automates the process and ensures the safety of your important files and directories. Remember to test your backups regularly to ensure that they can be restored in case of an emergency.
For more information on Borg Backup, you can refer to the official documentation.