Scripting and Automating rsync with *nix
Once you've sorted out the rsync command and run the initial backup, you need only to put the rsync command into a script and automate it with cron. To do that, you just need a text editor like Vi/Vim, Emacs or Pico.
Simply copy the rsync command(s) you used into the text editor and save it to
/backups with a descriptive name. Just remember:
- You don't need to use
sudoin the script. - Make sure you've removed the
--dry-runargument from the rsync command. - When saving your script, don't use s p a c e s in the file name. Use-hyphens-, _under_scores or CamelCase.
- Give your script the
.shextension (it's a shell script).
Here's what a sample script might look like:
#!/bin/sh
# Daily Data Backup
rsync -avz -e "ssh -i /backup/ssh_key" /home bob@bob.evbackup.com:home-data
Once the script is saved, you need to make it executable with chmod +x file. For example:
chmod +x /backup/backup-home.sh
If you have gotten this far and it still looks like Klingon to you: .
Automating the rsync Script with cron
Once you have your backup script created and saved, you need only to add a cron job to automate it. To automate a cron job for your script:
- At terminal, add a cron job for the superuser:
sudo crontab -e - Enter the daily schedule command for backup-home.sh
For example, to run backup-home.sh every night at 11:42 PM, you would enter:
42 23 * * * /backup/backup-home.sh- What are the crontab fields? »
- « Hide this
The fields in crontab (separated by s p a c e s or tabs) are:
Use commas to designate a more than one value:[minute] [hour] [day of month] [month] [weekday] [command] [command args]00,09,11,22
Use hyphens to designate a range of values:0-6 - Save your crontab file and close your text editor. You\'re all set!
Should you have questions or need help:


