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 dailyhome.sh, 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 dailyhome.sh


#!/bin/bash
#
#
# 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.

2.
–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

3.
–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:


--exclude='.gvfs'

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


/media/bfdlinux/Homer/homerbkup/

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 dailyhome.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:


#!/bin/bash
#
# 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 restore.sh

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

And to run it:


sudo sh restore.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?

easy:


/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:

mikey/
mikey/myfile

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

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?