A simple and efficient script to automate the backup process using rsync
and CRON jobs. This script ensures only one instance runs at a time using a lock file mechanism.
-
Script Placement:
- Ensure the script is placed in a directory only accessible by the root user for security reasons.
-
Make the Script Executable:
-
You'll need to grant execute permissions to the script:
sudo chmod +x /path/to/rsync_script.sh
-
-
Modify Paths:
-
Open the script and modify the source and destination paths:
rsync -avzu '/put/your/original/path/' '/put/your/destination/path'
-
-
Set Up CRON:
-
Open the CRON editor with root privileges:
sudo crontab -e
-
Add the following line to run the script every 5 minutes:
*/5 * * * * /path/to/rsync_script.sh
-
Save and exit.
-
-
CRON Timing:
- If you're unfamiliar with setting up CRON timings, use online tools like CronTab or you can simply ask here for assistance.
The script first checks if an instance of rsync
is already running using a lock file. If it finds the lock file, it exits, ensuring that only one backup process runs at a time. If no such file exists, it creates one, runs the rsync
command, and then deletes the lock file once the process is complete.
Always make sure to test backup scripts in a controlled environment before deploying to ensure data integrity and prevent data loss.