How to Set Up Automatic Backups with `rsync` on Arch Linux

How to Set Up Automatic Backups with rsync on Arch Linux

In the ever-evolving world of Linux, where customization and control are king, system backups are a vital part of any user’s setup. Whether you’re a system administrator managing critical infrastructure or a desktop user who just wants to protect personal files, having a reliable and automated backup solution can save you from data loss due to accidental deletions, system failures, or hardware malfunctions.

One of the most flexible and powerful tools for backups is rsync. In this article, we’ll walk through the steps of setting up automatic backups using rsync on Arch Linux. We’ll cover everything from basic usage to scheduling backups with cron or systemd timers.


What is rsync?

rsync is a command-line utility for efficiently copying and synchronizing files between directories, either on the same system or across a network. It only transfers the changes (differences) between files, making it both fast and bandwidth-efficient. Some of its key features include:

  • Incremental backups
  • Local and remote synchronization
  • Preservation of symbolic links, permissions, and timestamps
  • Support for SSH-based transfers
  • Robust error handling

Installing rsync on Arch Linux

First, ensure that rsync is installed on your Arch Linux system. Open your terminal and run:

sudo pacman -S rsync

Once installed, you can verify it by running:

rsync --version

Basic rsync Usage

Before diving into automation, it’s helpful to understand the basic syntax. The general form is:

rsync [OPTIONS] SOURCE DESTINATION

Example: Back up your home directory to an external drive mounted at /mnt/backup:

rsync -avh --delete ~/ /mnt/backup/home_backup/

Explanation of options:

  • -a: Archive mode (preserves permissions, symbolic links, etc.)
  • -v: Verbose output
  • -h: Human-readable file sizes
  • --delete: Deletes files from the destination that no longer exist in the source

Step-by-Step Guide to Setting Up Automatic Backups

Now let’s walk through setting up an automated backup system using rsync.


Step 1: Create a Backup Script

First, create a bash script that encapsulates your rsync command.

nano ~/backup.sh

Add the following contents:

#!/bin/bash

# Define source and destination
SOURCE="$HOME/"
DEST="/mnt/backup/home_backup/"

# Create a timestamped log file
LOGFILE="/var/log/rsync_backup_$(date +'%Y%m%d_%H%M%S').log"

# Run the rsync command
rsync -avh --delete "$SOURCE" "$DEST" &> "$LOGFILE"

Make the script executable:

chmod +x ~/backup.sh

Step 2: Test the Script

Run it manually to make sure it works:

./backup.sh

Check the backup directory and log file to confirm successful execution.


Step 3: Automate with cron (Option 1)

If you prefer cron for task scheduling, here’s how to set it up.

a) Edit Crontab

crontab -e

b) Add a New Job

To run the backup daily at 2 AM:

0 2 * * * /home/yourusername/backup.sh

Replace yourusername with your actual username.

You can verify cron jobs with:

crontab -l

Note: Make sure your script paths are absolute and that the script has permission to access the directories involved.


Step 4: Automate with systemd Timer (Option 2)

For a more modern and system-native approach, you can use systemd timers.

a) Create a systemd Service

Create a new service file:

sudo nano /etc/systemd/system/rsync-backup.service

Paste the following:

[Unit]
Description=Rsync Backup Job

[Service]
Type=oneshot
ExecStart=/home/yourusername/backup.sh

b) Create a Timer File

sudo nano /etc/systemd/system/rsync-backup.timer

Paste:

[Unit]
Description=Run rsync backup daily

[Timer]
OnCalendar=*-*-* 02:00:00
Persistent=true

[Install]
WantedBy=timers.target

c) Enable and Start the Timer

sudo systemctl daemon-reexec
sudo systemctl daemon-reload
sudo systemctl enable --now rsync-backup.timer

d) Verify Timer

systemctl list-timers

You’ll see when the timer is next scheduled to run.


Optional: Remote Backups with rsync over SSH

If you want to back up to a remote server, use SSH with rsync:

rsync -avh -e ssh ~/ user@remotehost:/path/to/backup/

Make sure SSH keys are set up for passwordless login to allow automation without interaction.


Best Practices for Using rsync for Backups

Here are a few practical tips to make the most out of your rsync backups:

1. Use Include/Exclude Filters

Customize what to back up:

rsync -av --exclude 'Downloads/' --exclude '*.iso' ~/ /mnt/backup/home_backup/

2. Use --link-dest for Incremental Backups

Create daily snapshots while saving space:

rsync -av --link-dest=/mnt/backup/previous_backup/ ~/ /mnt/backup/today_backup/

3. Use Logging

Maintain logs for debugging and auditing.

4. Mount Checks

Ensure your destination (e.g., USB drive) is mounted:

if mountpoint -q /mnt/backup; then
    rsync -avh ...
else
    echo "Backup drive not mounted."
fi

5. Use a Lockfile to Prevent Overlapping Runs

Avoid concurrent executions with flock or lockfiles.


Troubleshooting

IssueSolution
Script not running via cronUse full paths; check cron logs with journalctl -u cron
rsync permission deniedEnsure script has proper permissions and access
Disk fullUse du -sh to monitor backup sizes
SSH authentication failsSet up SSH keys correctly

Conclusion

Automating backups with rsync on Arch Linux is a practical and powerful way to protect your data. Whether you prefer scheduling with cron or systemd, rsync provides flexibility and control over what and how you back up. With a few configuration steps, you can have a dependable backup system that runs quietly in the background, giving you peace of mind in the face of unexpected data loss.

Remember to test your backups regularly and consider combining rsync with other tools or off-site solutions for a comprehensive backup strategy.