Automating Rsync Backups with Cron & Systemd (Hands-on Guide)
π
One of the best ways to ensure consistent and reliable backups in Linux is by automating Rsync with cron jobs or systemd timers. Whether you need hourly, daily, or weekly backups, setting up an automated Rsync schedule reduces manual effort and ensures data consistency.
π In this guide, you will learn:
β
How to set up automated Rsync backups using cron
β
How to configure systemd timers for more flexibility
β
Step-by-step hands-on examples for scheduling backups
β
How to log and monitor automated Rsync tasks
π 1. Why Automate Rsync Backups?
Manually running Rsync for backups is impractical, especially in large-scale environments. Automating Rsync ensures:
πΉ Regular backups β Prevents data loss by scheduling periodic backups.
πΉ Consistency β Keeps source and destination continuously synchronized.
πΉ Efficiency β Runs backups without human intervention, reducing administrative overhead.
β
Example: Sync /home/user/docs/
to /backup/docs/
every night
rsync -av --delete /home/user/docs/ /backup/docs/
π Now, letβs automate this process using cron
and systemd
.
β° 2. Automating Rsync with Cron Jobs
Cron is a built-in Linux scheduler that allows you to run commands at fixed intervals.
πΉ 2.1 Creating a Scheduled Rsync Task
β Edit the userβs crontab:
crontab -e
β Example: Run Rsync backup every day at 2:00 AM
0 2 * * * rsync -av --delete /home/user/docs/ /backup/docs/
π Understanding the cron format:
βββββββββ minute (0β59)
β βββββββ hour (0β23)
β β βββββ day of month (1β31)
β β β βββ month (1β12)
β β β β ββ day of week (0β7, Sunday = 0 or 7)
β β β β β
0 2 * * * rsync -av --delete /home/user/docs/ /backup/docs/
This job runs every day at 2 AM.
β Check active cron jobs:
crontab -l
πΉ 2.2 Running Rsync Every Hour
β Example: Schedule an Rsync job to run every hour
0 * * * * rsync -av --delete /home/user/docs/ /backup/docs/
π Now Rsync will run every hour on the hour.
πΉ 2.3 Logging Cron Jobs for Troubleshooting
By default, cron
does not log output. To capture Rsync logs, modify the cron job:
β Save Rsync output to a log file
0 2 * * * rsync -av --delete /home/user/docs/ /backup/docs/ >> /var/log/rsync.log 2>&1
β View the log file
tail -f /var/log/rsync.log
π Now, you can track Rsync execution and troubleshoot failures.
β‘ 3. Automating Rsync with Systemd Timers
For better reliability and more precise scheduling, Linux offers systemd timers as an alternative to cron
.
πΉ 3.1 Creating a Systemd Service for Rsync
β Step 1: Create the systemd service file
sudo nano /etc/systemd/system/rsync-backup.service
β Add the following configuration:
[Unit]
Description=Automated Rsync Backup Service
After=network.target
[Service]
ExecStart=/usr/bin/rsync -av --delete /home/user/docs/ /backup/docs/
User=root
π This service runs Rsync whenever it's called by a systemd timer.
β Reload systemd to apply changes:
sudo systemctl daemon-reload
πΉ 3.2 Creating a Systemd Timer for Rsync
β Step 1: Create the timer file
sudo nano /etc/systemd/system/rsync-backup.timer
β Add the following configuration:
[Unit]
Description=Run Rsync Backup Every Day
[Timer]
OnCalendar=*-*-* 02:00:00
Persistent=true
[Install]
WantedBy=timers.target
π This timer triggers the Rsync backup every day at 2 AM.
β Step 2: Enable and start the timer
sudo systemctl enable rsync-backup.timer
sudo systemctl start rsync-backup.timer
β Step 3: Check if the timer is active
systemctl list-timers --all
π You should see rsync-backup.timer
in the list of active timers.
πΉ 3.3 Running Rsync at Fixed Intervals
Instead of using OnCalendar=*-*-* 02:00:00
, you can set up an hourly job:
[Timer]
OnUnitActiveSec=1h
π Now, Rsync will run every hour after startup.
β Restart the timer for changes to take effect:
sudo systemctl restart rsync-backup.timer
π‘οΈ 4. Monitoring & Troubleshooting Automated Rsync Jobs
Whether using cron or systemd, monitoring backups is crucial.
β View Cron Job Execution Logs
grep CRON /var/log/syslog
π This shows all executed cron jobs.
β Check Systemd Timer Logs
journalctl -u rsync-backup.service --since today
π This displays Rsync logs for today's backup execution.
β Manually Run the Systemd Service
sudo systemctl start rsync-backup.service
π This allows you to test Rsync manually before automating it.
π 5. Summary
Automation Method | Use Case |
---|---|
Cron Jobs | Best for simple, time-based scheduling |
Systemd Timers | More flexible scheduling, better logging & monitoring |
Logging | Essential for debugging and verifying backups |
Monitoring | journalctl and syslog help diagnose failures |
β Automating Rsync ensures continuous data backups and reduces human intervention.
π¬ Join the Discussion!
Have you automated Rsync backups before?
Do you prefer cron jobs or systemd timers for scheduling tasks?
π¬ Share your experience in the comments below! π
π Next Up: Excluding Files & Directories in Rsync for Efficient Backups