Using rsync and systemd to Automate Offsite Backups of Selected Config Files and User Data

Introduction to Automated Offsite Backups

As a Linux user, you’ve probably learned the hard way how important it is to protect your configuration files and user data from loss or corruption. I’ve seen this go wrong when a disk fails or a configuration change goes awry. One way to ensure the integrity of this data is to set up automated offsite backups. In this article, we’ll explore how to use rsync and systemd to create a reliable and efficient backup system.

Understanding rsync

rsync is a powerful command-line utility that allows you to synchronize files and directories across different locations. The real trick is that it only transfers the differences between the source and destination, making it faster and more efficient than other backup methods. Don’t bother with other methods unless you have a specific reason to - rsync is the way to go. You can install rsync on most Linux distributions using the package manager. For example, on Debian-based systems, you can use apt:

sudo apt update
sudo apt install rsync

On Arch Linux, you can use pacman:

sudo pacman -S rsync

In practice, I usually start with a simple rsync command to test the connection and make sure everything is working as expected.

Setting up the Backup Destination

Before you can start backing up your data, you need to set up a destination for the backups. This can be an external hard drive, a network-attached storage (NAS) device, or even a cloud storage service. For this example, let’s assume you have a remote server with SSH access. You can use a tool like ssh-keygen to generate a pair of SSH keys and copy the public key to the remote server:

ssh-keygen -t ed25519
ssh-copy-id user@remote-server

This is where people usually get burned - make sure you test the SSH connection before proceeding.

Creating the Backup Script

Now that you have rsync installed and a backup destination set up, you can create a script to automate the backup process. Create a new file, for example, backup.sh, and add the following contents:

#!/bin/bash

# Set the source and destination directories
SRC=/etc/config
DST=user@remote-server:/backup/config

# Use rsync to synchronize the files
rsync -avz -e ssh $SRC $DST

Make the script executable and test it:

chmod +x backup.sh
./backup.sh

If everything works as expected, you should see the files being transferred to the remote server.

Integrating with systemd

To automate the backup process, you can use systemd to create a timer that runs the backup script at regular intervals. Create a new file, for example, backup.timer, and add the following contents:

[Unit]
Description=Backup timer

[Timer]
OnUnitActiveSec=1d
AccuracySec=1m

[Install]
WantedBy=timers.target

Create a corresponding service file, for example, backup.service, and add the following contents:

[Unit]
Description=Backup service

[Service]
ExecStart=/path/to/backup.sh

Reload the systemd daemon and start the timer:

sudo systemctl daemon-reload
sudo systemctl start backup.timer

In practice, I usually start with a simple timer that runs every day, and then adjust the frequency as needed.

Security Considerations

When setting up automated backups, it’s essential to consider the security implications. Make sure to use secure protocols, such as SSH, to transfer the data, and use strong passwords and authentication methods to protect the backup destination. You can also use tools like rsync with --checksum option to verify the integrity of the backups. For more information on rsync and its options, you can visit the official rsync website.

Troubleshooting and Monitoring

To ensure that the backup process is working correctly, you should monitor the logs and check for any errors. You can use journalctl to view the logs:

sudo journalctl -u backup.service

You can also use tools like rsync with --verbose option to get more detailed output:

rsync -avz -e ssh --verbose $SRC $DST

If you encounter any issues, don’t hesitate to dig into the logs and troubleshoot the problem.

Additional Tips and Trade-Offs

When setting up automated backups, you need to consider the trade-offs between the frequency of the backups, the amount of data being transferred, and the resources required. You can adjust the OnUnitActiveSec option in the backup.timer file to change the frequency of the backups. You can also use rsync with --exclude option to exclude certain files or directories from the backup.


See also