How to Automate Linux Backups Using rsync and systemd

In this tutorial we build an automated Linux backup using rsync and a systemd timer.

In this step-by-step tutorial, we’ll create a reliable rsync backup script, test it safely using dry-run mode, and automate it using a systemd service and timer so your backups run automatically. You’ll also learn how to prevent silent backup failures by adding a mount check, and how to integrate healthchecks.io so you get alerts if your backup script fails.

This tutorial is perfect for Linux users, sysadmins, and homelab enthusiasts who want a simple, reliable, and transparent backup solution without relying on heavy backup software.

YouTube player

Backup Script

#!/bin/bash

backup_src="/home/jay/my_files"
backup_mount="/mnt/backup"
backup_dest="$backup_mount/current"
current_date=$(date +%Y-%m-%d)
log_path="$backup_mount/logs"
previous_files="$backup_mount/files_previous/$current_date"

healthcheck_url="https://hc-ping.com/..."

# Create required directories
#mkdir -p "$log_path"
#mkdir -p "$previous_files"

# Run rsync
#rsync -av --delete --backup \
#  --backup-dir="$previous_files" "$backup_src" "$backup_dest" \
#  > "$log_path/backup_$current_date.log" \
#  2> "$log_path/backup_${current_date}_error.log"

echo "test"

# Alert halthchecks.io that rsync was successful:
if [ $? -eq 0 ]; then
  curl -fsS "$healthcheck_url" >/dev/null
fi

Systemd Service Unit

[Unit]
Description=Run a daily backup
After=network-online.target
Wants=network-online.target

[Service]
Type=oneshot
ExecStart=/usr/local/bin/backup.sh

[Install]
WantedBy=timers.target

Systemd Timer

[Unit]
Description=Daily rsync backup timer

[Timer]
OnCalendar=00:00
Persistent=true

[Install]
WantedBy=timers.target

Relevant Files

If you need sample files to use with this script, you can download this zip file which contains multiple file types.

  • Ad-free Content
  • Early access to select videos
  • Discord access

And more!