borgmatic-linux-backup-tutorial
Image: ribkhan/Adobe Stock

Do you have a reliable backup solution running on your Linux servers? If not, what’s your plan for disaster recovery? The word “disaster” alone should be enough to help you realize backups are an absolutely crucial part of your organization.

If you’re in the market for a new Linux backup solution, there’s a lesser-known solution that does an outstanding job, and it’s fairly easy to install and configure. That solution is Borgmatic. This simple, configuration-driven backup solution protects your files (and even databases) with client-side encryption and even offers third-party integration for things like monitoring.

I want to walk you through the process of installing Borgmatic on Ubuntu Server 22.04. When complete, you should feel confident your important data is regularly being backed up.

SEE: 40+ open source and Linux terms you need to know (TechRepublic Premium)

What you’ll need

The only things you’ll install and use Borgmatic are an instance of Ubuntu Server (you can also install this on Ubuntu Desktop and any Debian-derived distribution) and a user with sudo privileges. That’s it: Let’s make with the backups.

How to install Borgmatic

The first thing you might want to do is update and upgrade Ubuntu Server. This can be done with the two commands:

sudo apt-get update

sudo apt-get upgrade -y

If the kernel gets upgraded in the process, you’ll need to reboot for the changes to take effect.

Once the update/upgrade completes, install Borgmatic with the command:

sudo apt-get install borgmatic -y

Believe it or not, that’s it for the installation.

How to initialize a repository

Our next step is to initialize a repository for the backup. You’ll want to change into a directory to house the new repository. For example, if you have an external drive to house your backup that is mounted in the /data directory, change into /data with cd /data. We’ll call our repository test.borg and initialize it using a repository key. That command is:

borg init -e repokey test.borg

You’ll be prompted to type and verify a password for the new repository. You should now see a new directory, in /data, called test.borg.

How to create a config file

Now, we can generate a configuration file. For this, issue the command:

generate-borgmatic-config -d test.yaml

Open that new file for editing with:

nano test.yaml

You’ll see a section in that file that looks like this:

source_directories:

- /home

- /etc

- /var/log/syslog*

In that section, configure any directory you need to include in the backup.

You’ll also find a section that looks like this:

repositories:

- user@backupserver:sourcehostname.borg

- user@backupserver:{fqdn}

You’ll need to add the new repository and comment out the samples, so that section looks like this (editing for the name of the repository you created):

repositories:

- test.borg

#     - user@backupserver:sourcehostname.borg

#     - user@backupserver:{fqdn}

Save and close the file.

How to run your first backup

You can now run the first backup with the command:

sudo borgmatic --config test.yaml --verbosity 1

You will be prompted first for your sudo password and then twice for your repo key password.

After the backup completes, verify it with:

sudo borgmatic -c test.yaml --list

Your output should look something like this:

jammy-2022-05-19T12:13:28.105102 Thu, 2022-05-19 12:13:32 [91df7f2c66fa516d026fc0fb8e9ad777d2533e81e22b7da1da9a4b0245804fea]

Congratulations, your first backup was successful.

How to automate the backup

To automate the backup, we first need to create a systemd file with the command:

sudo nano /etc/systemd/system/borgmatic.service

In that file, paste the following:

[Unit]

Description=Borgmatic system backup

Requires=network.target

After=network.target

[Service]

Type=oneshot

Nice=10

IOSchedulingClass=best-effort

IOSchedulingPriority=6

ProtectSystem=full

ExecStart=/usr/bin/borgmatic --verbosity -1 --syslog-verbosity 1

Save and close the file. We then need to specify the backup time. To do that, we create a timer config file (that runs the backup at the same time every day) with:

sudo nano /etc/systemd/system/borgmatic.timer

In this new file, paste the following:

[Unit]

Description=Daily backup timer

[Timer]

OnCalendar=*-*-* 12:00:00

Persistent=true

[Install]

WantedBy=timers.target

Enable the timer with the command:

sudo systemctl enable --now borgmatic.timer

You will be prompted for your repository key password. Once that command completes, Borgmatic is set up to run every day at the same time. Congratulations on creating a new backup system for your Linux server in record time.

Subscribe to TechRepublic’s How To Make Tech Work on YouTube for all the latest tech advice for business pros from Jack Wallen.

Subscribe to the Developer Insider Newsletter

From the hottest programming languages to commentary on the Linux OS, get the developer and open source news and tips you need to know. Delivered Tuesdays and Thursdays

Subscribe to the Developer Insider Newsletter

From the hottest programming languages to commentary on the Linux OS, get the developer and open source news and tips you need to know. Delivered Tuesdays and Thursdays