Why Use rsync
and cron
for Backups? #
- rsync: A powerful file synchronization tool that copies only changed files, minimizing bandwidth and storage usage.
- cron: A scheduling tool that allows you to run commands at specified intervals automatically.
Using rsync
with cron
enables efficient, automated, and scheduled backups for important files or directories on your Linux server.
Prerequisites #
- A Linux server with
rsync
installed.- Install
rsync
if it’s not already installed:
- Install
sudo apt install rsync # On Debian/Ubuntu
sudo yum install rsync # On CentOS/RHEL
2. SSH Access (if backing up to a remote server).
- Set up SSH key authentication for passwordless access to the remote server.
Step 1: Basic rsync
Command for Backups #
The basic syntax for using rsync
to back up files is:
rsync -avz /path/to/source /path/to/destination
Flags:
-a
: Archive mode (preserves permissions, timestamps, symlinks).-v
: Verbose (shows details of files being transferred).-z
: Compress data during transfer (useful for remote backups).
Example: To back up a directory /var/www
to another local directory /backup
:
rsync -avz /var/www /backup
Remote Backup Example: To back up /var/www
to a remote server:
rsync -avz /var/www username@remote_server:/backup
Step 2: Create a Backup Script #
To simplify the backup process and make it suitable for scheduling, create a script.
- Create a new file for the script:
sudo vi /usr/local/bin/backup.sh
2. Add the following script, modifying paths and credentials as needed:
#!/bin/bash
# Set variables
SOURCE_DIR="/var/www"
DESTINATION_DIR="/backup"
REMOTE_USER="username"
REMOTE_HOST="remote_server"
REMOTE_DESTINATION="/backup"
# Local backup
rsync -avz $SOURCE_DIR $DESTINATION_DIR
# Uncomment the following line for remote backup
# rsync -avz $SOURCE_DIR $REMOTE_USER@$REMOTE_HOST:$REMOTE_DESTINATION
# Print completion message
echo "Backup completed on $(date)"
3. Make the script executable:
sudo chmod +x /usr/local/bin/backup.sh
Step 3: Schedule the Script with cron
#
- Open the cron editor for the root user or a specific user (e.g., root):
sudo crontab -e
2. Add a cron job to schedule the backup. For example, to run the backup daily at 2 AM, add the following line:
0 2 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1
Explanation of the schedule:
0 2 * * *
: Runs every day at 2:00 AM.>> /var/log/backup.log 2>&1
: Appends output and errors to a log file, helping with troubleshooting.
3. Save and exit the cron editor.
Step 4: Verify and Monitor the Backup #
- Check the Backup Directory: After the cron job runs, verify that files are being copied to the backup location:
ls /backup # Check the backup directory
2. Review the Log File: The log file (/var/log/backup.log
) will contain details of each backup run, useful for verifying successful execution and troubleshooting errors.
cat /var/log/backup.log
Step 5: (Optional) Set Up Rotations to Manage Storage #
To prevent backups from consuming excessive storage, set up log rotation for backups. For example, use find
to remove backups older than 7 days.
Add this to the script for automatic cleanup:
Delete backups older than 7 days #
find /backup -type f -mtime +7 -exec rm {} \;
Example Full Backup Script with Cleanup #
#!/bin/bash
# Set variables
SOURCE_DIR="/var/www"
DESTINATION_DIR="/backup"
REMOTE_USER="username"
REMOTE_HOST="remote_server"
REMOTE_DESTINATION="/backup"
# Local backup
rsync -avz $SOURCE_DIR $DESTINATION_DIR
# Uncomment the following line for remote backup
# rsync -avz $SOURCE_DIR $REMOTE_USER@$REMOTE_HOST:$REMOTE_DESTINATION
# Delete backups older than 7 days
find $DESTINATION_DIR -type f -mtime +7 -exec rm {} \;
# Print completion message
echo "Backup completed on $(date)"
This script backs up the /var/www
directory, optionally to a remote server, and deletes backups older than seven days.
Conclusion #
Automating server backups with rsync
and cron
provides a robust solution for maintaining regular backups. By customizing the schedule and locations, you can ensure reliable and efficient backups with minimal manual intervention.