This is sort of a follow up, or part two, to my last post on how to create a script to mirror two external hard drives. So, if you haven’t read that article yet, please check it out first.
After creating a script using rsync to mirror two hard drives we can take it one step farther and set it up so the script is automatically run at set times during the day. To accomplish this we’re going to use another free tool called crontab. It’s available on all Macs.
Cron
Cron is a piece of software that runs in the background and executes schedule commands. Crontab is a tool to edit these scheduled commands. Each account on a computer can have their own set of scheduled commands. We’re going to only set this on one account for now.
Crontab
To access crontab, open up the Terminal app found in the Application/Utilities folder. Then enter the following command:
crontab -e
This is the crontab program and should be empty by default. Each line is a scheduled command and is broken into two parts: when and what. First, press “i” to enter Insert mode and add the following line.
0 0,13 * * * ~/rsync-backup
In brief, the first zero tells cron to run this command at the top of the hour. The next group tells cron what hours to run it. In this case, I’m telling it to run at midnight and one in the afternoon. More hours can be added, just separate them by a comma. The last three asterisks tell cron to run this script every day, every month and every year. For more information, just do a quick google search. There’s plenty of resources on cron on the web.
The second part is just the name of the script. The tilde is a short cut to the home folder. Just make sure the file name here matches the name of your script from the previous article.
After the line has been added, type the following and press “Enter.”
:wq
This tells crontab to write the changes and then quit. Rsync-backup will now run twice a day in the background.
Notes
- Cron won’t wake up the computer. So, the computer must be awake for these commands to be run. Don’t choose a time late in the middle of the night if the computer is always off during these times.
- I’d recommend only using this method is the hard drives being mirrored are always connected and turned on. If they’re not, there is a chance rsync will create a folder on the root hard drive and mirror the files there. This can rapidly and unexpectedly fill up a hard drive.
