Categotry Archives: backup


Scheduling a backup script with crontab for Ubuntu/Debian


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/

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/


(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/ for example)

so the command is simply:

05 01 * * * sh /etc/nwaynobkup/

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?


Simple rsync backup/restore bash scripts for Ubuntu/Debian


Two (2) Rules for Successful Computing:

1. Always have a path back to the way it was, before you messed it up!

2. Always follow Rule #1!

This is something I’ve been meaning to post for a long time. How to do simple command line backup and restore operations, using a very simple bash script.

As always anything with a # in a command is simply a comment and need NOT be coded. So here’s the simple backup script, you can just copy, paste and save it (mine is called, yours might be different), with your favourite editior: gedit, nano, vi. Whatever makes your boat float!

This post will seem excessively long, but I am trying to make this simple enough that a n00b could do this. Apologies in advance.

So, using the editor of your choice (gedit, nano, vi), create a file. Copy/Paste, save the following to something like

# BACKUP the /home/ directory
# * this script is now executable!
# * fully qualified path for rsync
# * now invoked with bash since the ubuntu 'sh' is anemic
# * used end of line continuation character for greater readability
# * js
/usr/bin/rsync -azvu --log-file=/var/log/waynodaily.log --append
--exclude '.gvfs'
/home/ /media/bfdlinux/Homer/homerbkup/

well let’s see what we have here.

1. the options -azvu says:

a – archive mode. Files are compared against the backup,and only those files that have been added, changed, deleted, moved, since the last backup, are sent. This is an “incremental” backup. If this is the first time you are running this script, ALL files are backed up.

z – compress files during transfer
v – verbose mode (gives us a lot of debug info
u – update. skip files that are newer in the destination.

–logfile points us to where we want to the logfile to appear. in this case we want the output to go to /var/log/ which is the same place we would find the system messages file: /var/log/messages (you can use dmesg to look at the last few entries in the system log.

the log will contain a list of all the files archived, debug messages, and other things like:

sent 4598381 bytes received 23273 bytes 70559.60 bytes/sec
total size is 234383146344 speedup is 50714.13

–append simply says, hey append the output of rsync to the current file. Someday we’ll talk about logrotate but I just want to backup things.

4. By now you are wondering what the is all about. You will notice there is nothing after the backslash. This simply tells bash that more information is on the following line. We do this so we can read it! Remember there is NOTHING after the backslash. No spaces, no tabs. NOTHING!

5. on the next line, you’ll see:


what this tells rsync is to exclude the .gvfs directory. What’s that? It’s the gnome virtual file system. This way, we shouldn’t get any error codes returned from running this script.

6. The

/home/ /media/bfdlinux/Homer/homerbkup/

is the meat and potatoes here.

/home/ (yes it MUST have the trailing /) tells rsync that the source of the backup is the entire /home/ directory.

7. By now you might have guessed that


points us to the destination. /media/ is used by Ubuntu, to point to usb devices. In my case a 1 t/b usb backup drive.

the /bfdlinux is the volume label (and MOUNT POINT) of the volume.

/Homer is the hostname of my computer. I use that because I do multiple machine backups, and I can easily identify what machine I want.

/homerbkup/ (yup MUST have the trailing /) is the location where I want to backup the home directory.

Replace the /media/ (and so on) with whatever is applicable in your case.

8. Since we are backing up the entire /home/ directory, we MUST have root access so we’d run the script:

sudo sh

9. Okay how are we doing so far? Not too hard, is it?

So now that we have the system backed up, and a user just accidently
shot himself in the foot (nobody EVER does that, right?) or we need to transfer the files to another machine. What do I do? First of all like Douglas Adams said: Don’t Panic!

10. Restoring is pretty dang easy. All you do is just swap the source and destination in the script. So to restore the last line in the script would read:

/media/bfdlinux/Homer/homerbkup/ /home/

The entire restore script is simply:

# RESTORE the /home/ directory
# * this script is now executable!
# * fully qualified path for rsync
# * now invoked with bash since the ubuntu 'sh' is anemic
# * used end of line continuation character for greater readability
# * js
/usr/bin/rsync -azvu --log-file=/var/log/waynodaily.log --append
--exclude '.gvfs'
/media/bfdlinux/Homer/homerbkup/ /home/

And again, you can copy, paste to your favourite editor, and save it as say

Replace the /media/ (and so on) with whatever is applicable in your case.

And to run it:

sudo sh

or whatever you saved it as.

simple, huh?

but let’s say we only want to restore ONE user directory. How should that look?


/media/bfdlinux/Homer/homerbkup/mikey/ /home/

Again, replace the /media/ (and so on) with whatever is applicable in your case.

terminal output will look like:


sent 4603815 bytes received 23279 bytes 52881.07 bytes/sec
total size is 234383634253 speedup is 50654.61
nwayno@Homer:/etc/nwaynobkup$ sudo sh
sending incremental file list

sent 216 bytes received 34 bytes 500.00 bytes/sec
total size is 4691 speedup is 18.76

In the next post, I’ll show you how to automatically run the backup script using crontab.

So how did you do?