Automated Incremental Backups With Rsync And Cron (Part 2)

In Part 1 of this tutorial, we covered how to configure rsync so we can backup our important files. Now we will use Cron to automate our backup, so we don’t have to worry about remembering!

Cron Basics

Cron is simply a daemon that executes tasks at specified times. Essentially, you assign it a task and an interval to execute the task, and it performs said task in the background. Every user on the system can have their own cron jobs which will be stored in the user’s own crontab.

Getting Started With Crontab

There are three basic options that you may use along with the crontab. The first, “-l”, will display your crontab in the terminal. The second, “-r”, removes your crontab. Think of this as the nuclear option. The third, “-e”, allows you to edit your crontab.

Note: If you are going to use cron to run a command that requires admin privileges, you need to use “sudo crontab -e” to edit the root user’s crontab.

To create a job in cron we only need to input one line of information consisting of five numbers and a command separated by single spaces. The format is as follows:

minute hour dayOfMonth month dayOfWeek nameOfCommand

The ranges for each number are as follows:

  • minute: 0-59
  • hour: 0-23
  • dayOfMonth: 1-31
  • month: 1-12
  • dayOfWeek: 0-6

If you are looking at the above and thinking, what if I want to run a job everyday (or more often), there are some special characters that may be helpful.

A dash can be used for a continuous range of values (e.g., 0-5), commas can be used if you want multiple values in a time slot that are not continuous (e.g., 0,5) and an asterisk can be used as a wild card to capture all the values (e.g., *). You can even use a slash in conjunction with an asterisk to run at increments you specify using the power of division (e.g., */10 tells cron to run at times divisible by 10). Let’s look at an example to test these out:

*/15 9-17 1,15,30 * * nameOfCommand

This will run nameOfCommand every 15 minutes, on the first, fifteenth and thirtieth of every month, from 9 am to 5 pm.

0 9 1 1 * nameOfCommand

The above will run nameOfCommand at 9 am on January 1.

Crontab Shorthand

There are a few cases where you can use a special string and forgo having to figure out which numbers you need to specify in cron. They are @hourly, @daily, @weekly, @monthly, @yearly, and @reboot. Note that the @reboot string runs each time your computer boots up, whether you just powered it up or actually rebooted. Let’s take a look at a quick example. If you want to run a command every hour, you only need enter the following in crontab:

@hourly nameOfCommand

Which saves you from having to come up with this:

0 * * * * nameOfCommand
Is your machine running?

It is important to note that your cron jobs will only run if your machine is also running. Cron also won’t realize it missed a job and run it late when you next turn on your machine, it just won’t run it. So if you do not have your machine on at all times and you want to use one of the special strings listed above, pay close attention to how they actually work. E.g., if you set your cron job to run @daily, it will be set to run at midnight everyday, as @daily is equivalent to:

0 0 * * * nameOfCommand

If you know your computer won’t be on at midnight, you want to run a task daily, and you know it will be on at say 9 am, try this instead:

0 9 * * * nameOfCommand

Note: You can also look into using anacron which does not assume that your machine is always running, but that is beyond the scope of this tutorial.

Cron job for rsync

You should now be ready to automate your backup. If you followed part 1 of this tutorial, you should have an rsync script stored in ~/bin, perhaps called backup.sh. Once you figure out how often you would like cron to run, you are ready to go. The steps are as follows:

  1. Open Crontab crontab -e

  2. Add your job, e.g.: 0 */2 * * * /home/yourUserName/bin/backup.sh

  3. Save your crontab. You can make sure your new job was added with this command: crontab -l

  4. Check to make sure that the job ran when you thought it would. One way to do this is by checking /var/log/syslog for your cron job. less /var/log/syslog | grep -i “cron”

When I ran the above commands, amongst other lines I found:

    Sep 15 14:00:01 hostName CRON[xxxx]: (userName) CMD (/home/userName/bin/rsync/backup.sh)

From here on out you no longer have to remember to copy and paste your files over to your backup medium. Rsync takes care of the copying (and saves time with its delta transfer algorithm), and cron makes sure rsync runs at the interval you specify! If you have a question or other feedback, leave it in the comments.

comments powered by Disqus