In the previous post we saw a simple backup script, that backs up the entire /home/ directory.
While this is great, we would have to remember to run this everyday for it to be of any use.
Fortunately for us, we can do that very easily with crontab. crontab maintains a chronological (clock order) entry of all the tasks that need to be run at a certain time.
So now that we have the backup script, let’s schedule it for daily backup!
1. We want to use the system (root) crontab, so we enter:
sudo crontab -e
the sudo tells use we want temporary super user powers, to run crontab, and we want to edit it.
Enter you password, and you will see a script! Oh my gosh! Hey it’s not that hard. We’re going to add a line to the end of the script.
scroll down to the end of the script, and you will see a line that looks like:
# m h dom mon dow command
that’s sort of a template.
m – is the minute you want the script to run (0 – 59)
h – is the hour (24 hour military time) that you want the script to run. (0 – 23)
dom – is the day of the month (1 – 31)
mon – is the month (1 – 12)
dow – is the day of the week (0 – 6. 0=Sunday)
You can use 3 letter abbreviations for month and day of week, but for now we’ll stick with numbers.
2. so my entry looks like this:
05 01 * * * sh /etc/nwaynobkup/dailyhome.sh
So let’s decode this shall we?
05 – says run this at 5 minutes after the hour.
01 – says run it at 1 am.
so we are running the script at 1:05 am every day. Notice MINUTES are first, then hours.
But what are all the asterisks (*) doing?
The asterisks are a wild card. In this case it says, run any day of the month (dom), in any month, and on every day of the week. TADA! This script will run at 1:05 am EVERYDAY. Easy yes?
Still worried about the command part? Don’t be. Remember that is how we ran it manually in the previous post. We simply copy that here:
sudo sh /etc/nwaynobkup/dailyhome.sh
NOTE: IT IS NOT NECESSARY TO PUT SUDO IN CRONTAB SINCE WE ARE RUNNING THIS AS ROOT.
(or whatever you called your backup script!)
I happened to put my script in /etc/nwaynobkup/. I put it there, since I am not likely to change it. You could run this out of your home directory, just fully qualify it. (/home/nwayno/dailyhome.sh for example)
so the command is simply:
05 01 * * * sh /etc/nwaynobkup/dailyhome.sh
Again, or whatever you called your backup script.
remember to hit ENTER at the end of the line. Save it, and you are done. (for nano, hit control-o to write the file, and then control-x to exit)
3. Now if we go to /var/log/ we’ll see:
cd /var/log
ls -l waynodaily.log
-rw-r–r– 1 root root 90909 2011-01-23 01:09 waynodaily.log
4. Tail the output and we’ll see:
sudo tail waynodaily.log
Or whatever you called your log file. You will see something like this:
2011/01/23 21:57:04 [13326] .d..t…… nwayno/.purple/logs/yahoo/
2011/01/23 21:59:10 [13326] sent 827487594 bytes received 38810 bytes 3330086.
13 bytes/sec
2011/01/23 21:59:10 [13326] total size is 221400641467 speedup is 267.55
How did you do?