How to Hot-Clone Backup with Rsync
   (Using rsync_backup_local and rsync_restore_local)

A backup is only as good as it's tested to be!
An easy way to test a backup is to load virtualbox, and run your restore.

Before we begin, we need to know a few things about rsync_backup_local

rsync_backup_local is a simple bash script to facilitate your backup process. By "simple" we mean the script is written in a simple, easy to follow method so advanced users can follow the code and easily change to their needs.

On the other hand, no changes to the code are needed to make good backups via rsync and run them as a cron job in the background.
   # Valid arguments are:
   # -genkey      - generate ssh key for root user
   # -uploadkey - uploadkey to source machine (to be run after -genkey)
   # -delete        -  inserts the --delete and --delete-excluded to rsync options

Here is a list of interesting things you could do with rsync_backup_local

  # Make a copy then name the script according to the machine your backing up.
  # example: "rsync_backup_website" or "rsync_backup_main_server"
Danger of copying script from one machine to another. look good at the excludes!

  # Use this script to upload/update your website

  # Use this script to pull a backup from your server

  # User the rsync_restore_script afterwards to hot-clone to different hardware

  # Run this script without arguments daily or weekly, but once a month run  with the -delete argument to clean the destination directory.

Also note, some programs (databases like mysql) require special procedures to back them up. Back them up using that procedure! Of course, you may get away with a backup this way if you shut down the service..  

OK, let's get to it..

In a console window, logged in as root..

Download rsync_backup_local

Now let's move it to the /bin directory
mv ./ /bin/

Make it executable
chmod 755 /bin/

It is now ready to run, but wait, we need to edit the script

Let's look at the values..

  # Use this to test the script.
  # If set to no, the rsync command will not run
  # activescript=yes to run


  # The script detects if we are not running as
  # a cron and uses color. You can turn this off
  # activate_color=yes/no


  # The name of the script (Helpful in the mailings)
  # Example:

  # Name of machine the script is running on
  # Change this only if you want to call
  # this machine something different than "hostname"
  # Example: $HOSTNAME


  # Email address of whom shall be alerted on failure.
  # Example: your@email.address

  # The name of the --excludes file the script will create
  # You should not have to change this.
  # Example: /tmp/$nameofscript_excludes


  # Options examples are provided in the script. Notice
  # we are not using --delete or --delete-excluded.
  # That's because we run those options once a month
  # by using this command: -delete 


  # This is a security measure.
  # Don't let rsync delete and more than this many files
  # Example: 100


  # This is what you are going to backup
  # Value MUST be localhost or remote
  # Example localhost or
  # Example:


  # Note source cares about a trailing slash.
  # If you don't use one, the directory is created.
  # I always use a trailing slash. ie  "/home/myname/
  # Example: /home/


  # This where the backup shall reside
  # This could easily be written thus:
  # remoteuser@remotehost:/remote/dir
  # Create a hierarchy like this:
  #  mkdir -p /media/250backup/backup/computername/rsync/full_system/
  # Later you might want something like this ../../../tar/full_system
  # For god's sake, do NOT make this the / (root) directory!!!!!!
  # Example: /media/usb160/backup/localhost/rsync/full_system/


  # Now we have some excludes. The cat command
  # will write them to a file in the directory we defined above.
  # Enter excludes below the "cat" line
  # and do not remove the EOF at the bottom.

cat << EOF > $excludesfile

  # If you wish to List a command to run after backup is done.
  # Example: "echo I backed up like a good boy.."


  # For ssh-key generation if evoked: "rsync_backup_local -genkey"
  # You should NOT have to change this
  # Example: 4096


  # location of local key to upload if evoked: "rsync_backup_local uploadkey"
  # You should NOT have to change this
  # Example: /root/.ssh/id_rsa


Good, now that we've filled in all the information, let's go back in and do a test run.
Edit and change activate_script to no

Now go down to the options line and add the "n" switch. After we activate the script, this will do a test or dry run so we can see what would happen if we actually ran the rsync command.

Have we made our destination directory? If not, do it now from the command prompt. This is the line we put into the destination= option.
mkdir -P /media/250backup/backup/rodney/rsync/full_system

Ok, we should be ready to run the script. Just type in rsync_backup_local (if you haven't renamed the script)

If you edited activate_script=no, we should get a list of what the script would do. Notice is says it was successful. Yeah well, we know better. We crippled it.  

If you are satisfied with the output you see, go back and edit the activate_script=no and change it to yes. Now we'll go run it again.

On this run, which likely take some time as we are actually doing a dry run, you should get some output from rsync. If all looks as expected, we are ready for the real thing.
Edit the script one last time and remove the "n" we put into the "options".

Now run the script and enjoy the backup you just made..

NOTE:  As mentioned, we make a copy of this script for each machine we want to backup. ie "rsync_backup_homeserver" or "rsync_upload_website" and then adjust the options etc to that backup. Be careful if you do this to always examine the excludes and the destination.

You may run the scripts one at a time in a cron with the && sign.
Example: && &&

Note:  Once a month, we run the backup with the added argument -delete. This tells the script you wish to add the rsync options --delete and --delete-excluded. We do not run this every time we run the script which gives us a chance to retrieve a file or directory we deleted in the past.

Restoring to same or new hardware

Notice the backup scoops up almost everything while the restore defaults skip hardware specific and /var/run pid files etc.. This makes bare-metal restores simple. Load the base system, then run restore, reboot, done.

Let's run thru the restore procedure..

(ubuntu example)

Let's say we have a new machine and we'd like our operating system from the old machine. Let's get the cd of the distribution that matches our operating system, however, we need the "alternative cd".

At the boot prompt, you can enter "cli" and hit enter. This tells ubuntu to only install a base operating system and skip all the other stuff.

Note: I don't do sudo, so adjust this to your use.
Fill in the blanks as you run these commands.
Log into machine as a normal user
sudo su
passwd root


chmod 755

Edit the

Let's look at the options..

Notice in the example, our source is remote..  We'll be restoring from the backup we made with

Don't forget to take a look at the excludes. Although, if you exclude something like /home and you really do want it, you can just remove /home from the excludes list and run rsync_restore_local again..  It would be much faster the second time as rsync knows not to get all the other stuff again.  :)


A note about excludes when restoring. Some things like apache are to dumb to write to log if a log directory /var/log/apache2 does not already exist. Of course, mkdir /var/log and maybe chown www-data:www-data isn't really hard, but I wanted to make sure to mention this.

Ok, to run the restore, you have to change some things. Notice below, we changed no to yes and removed the "n" from the options.


Of course, you also need to edit nameofscript, and source.

Now you may run your restore which will destroy your current operating system on this new machine and copy all your programs, settings, files, etc to the new machine.

When done, reboot.
    shutdown -r now

If you run a gui, and restore to a new box, chances are, after rebooting, you'll need to get to the command prompt as root and type in: "dpkg-reconfigure xserver-xorg"

Enjoy your cloned machine..   Wasn't that easy?  :)