Data loss on a Linux server or workstation is rarely a matter of “if” but rather “when.” Hardware failure, accidental deletions, and system corruption can destroy years of work in an instant. Relying on manual backups is a flawed strategy because human memory is unreliable; eventually, you will forget to run the backup exactly when you need it most.
The most robust, standard, and lightweight solution for automated backups in Linux is combining the rsync command with the cron scheduling daemon. Rsync is a fast and incredibly versatile file-copying tool that supports delta encoding—meaning it only copies the portions of files that have actually changed since the last backup, drastically reducing network and disk usage.
In this guide, you will learn how to write a reliable rsync backup script, test its functionality, and automate it using crontab.
Step 1: Understand the Rsync Command Syntax
Before automating anything, you must understand how to execute a manual rsync backup. The basic syntax is:
rsync [options] [source] [destination]
For a standard, reliable backup, the most commonly used flags are -a (archive mode) and -v (verbose). Archive mode ensures that all file permissions, ownership, symlinks, and timestamps are preserved exactly as they are on the source.
For example, to back up your /var/www/html/ directory to a mounted external drive at /mnt/backup_drive/, you would use:
rsync -av /var/www/html/ /mnt/backup_drive/html_backup/
Important Note on Trailing Slashes: In rsync, /var/www/html/ (with a slash) means “copy the contents of the html directory.” Conversely, /var/www/html (without a slash) means “copy the html directory itself.” Always double-check your trailing slashes.
Step 2: Create a Backup Bash Script
Instead of putting a long, complex command directly into the cron scheduler, it is much safer and more maintainable to place your rsync command inside a bash script. This allows you to add logging and handle multiple directories easily.
- Open your terminal and create a new script file in a secure location, such as
/usr/local/bin/:sudo nano /usr/local/bin/daily_backup.sh - Add the bash shebang at the very top, followed by your rsync command. To keep a log of the backup process, append the output to a log file:
#!/bin/bash echo "-------------------------------------" >> /var/log/rsync_backup.log echo "Backup started on $(date)" >> /var/log/rsync_backup.log # Execute the backup and append the verbose output to the log rsync -av --delete /var/www/html/ /mnt/backup_drive/html_backup/ >> /var/log/rsync_backup.log 2>&1 echo "Backup completed on $(date)" >> /var/log/rsync_backup.log - Save and exit the file.
Note on the --delete flag: This flag tells rsync to delete files in the destination directory that no longer exist in the source directory. This creates an exact mirror. If you do not use this flag, your destination directory will continually grow with old, deleted files.
Step 3: Make the Script Executable
By default, newly created text files cannot be executed as programs. You must modify the file permissions to make the script executable.
sudo chmod +x /usr/local/bin/daily_backup.sh
Before scheduling, you should test the script manually to ensure there are no syntax errors and that the data transfers correctly:
sudo /usr/local/bin/daily_backup.sh
Check the destination folder and use cat /var/log/rsync_backup.log to verify the success of the test run.
Step 4: Automate the Script with Cron
Cron is the time-based job scheduler in Linux. You configure cron jobs by editing a file called a crontab.
- Because our script accesses system-level files (like
/var/www/and/var/log/), the cron job must run as the root user. Open the root crontab:sudo crontab -e - If prompted, choose an editor like Nano.
- Navigate to the bottom of the file. You will define the schedule using the standard cron syntax:
Minute Hour DayOfMonth Month DayOfWeek Command. - To run the backup script every day at 2:00 AM (02:00), add the following line:
0 2 * * * /usr/local/bin/daily_backup.sh - To run the script every Sunday at 3:30 AM, you would use:
30 3 * * 0 /usr/local/bin/daily_backup.sh - Save the file and exit the editor.
The cron daemon will automatically detect the updated crontab file; there is no need to restart any services.
Advanced Rsync Scheduling Over SSH
If you are backing up to a remote server rather than a locally mounted drive, rsync handles this natively over SSH. You simply modify the destination path in your script:
rsync -av --delete -e ssh /var/www/html/ user@remote_server_ip:/path/to/remote_backup/
For cron to execute this remote backup automatically without stalling at a password prompt, you must configure SSH key-based authentication between your local machine and the remote server.
By combining rsync’s efficiency with cron’s reliability, you create a robust, “set it and forget it” backup pipeline that ensures your critical Linux data is always protected against unforeseen disasters.