Saturday, 20 February 2010

Linux How to: Backup Ubuntu using Partimage (re-post)

Original Article: 20/03/2008
First, some words of advice: never underestimate the need for a backup copy of your system and data. You never know when you'll lose a hard drive, whether it's electrical spike or failure, fire, flood, burglary, or just some idiot dropping a heavy object on the computer case while the hard drive is writing -  who, me?
Err... yes. The important thing was, my backups  were secure. I had disk-images of system and data partitions as a baseline, incremental backups covering known restore points, all I had to rescue was a few days' worth of data. Please excuse the smug moment, I've been doing this a while. However, it was all using Windows.  Getting organised in the brave new world of Ubuntu isn't difficult, but does take preparation and planning.

Sticky, sticky tar
I've been able to perform adequate backups using tar.gz from the command line, but the syntax to include and exclude all the relevant stuff for my system partition looks like this;
sudo tar cvpzf 710bu010408.tgz --exclude="/proc/*"
--exclude="/vmdata/*" /
If it all goes wrong, I've got the whole file system, but no boot record or  bootloader to restore. I still need to boot up to a terminal then un-tar the whole thing. I need some tools, preferably on a boot disk, with a guided interface that will image and restore the partition, so I don't screw up or miss out on anything. I happen to have just the thing, a collection of utilities on a bootable disk under the name Parted Magic which comes with the Partimage application.

Just DD
There's a native command in every Linux distribution simply called dd, short for disk dump. It clones the source partition block-by-block. Its a very low-level disk utility, command-line only and the right syntax is crucial. If you mess up and accidentally mis-quote the source and target, you could wipe out everything you're trying to backup and more besides. Just for a laugh, try typing dd --help into a terminal. What you get is the manual page. Go for it if you want - I've got another thirteen pages of instructions and I'm still not confident. So not dd, then...

Magical Rescue
Fortunately we have a choice of utility CD's; SystemRescueCd (project at is a 170MB download of a bootable ISO CD-ROM. To quote from the project wiki: "SystemRescueCd is a Linux system on a bootable CD-ROM for repairing your system and recovering your data after a crash."
My current favourite, however is Parted Magic (project wiki at which is a small - under 40MB - CD-ISO download which I picked up first as a disk rescue tool.
Both boot CD's  contain the Partimage tool for creating our baseline images.

Partimage: does what it says...
What it says on the project wiki is; 
'Partimage is a Linux utility which saves partitions ... to an image file.' 
Run it from the Parted Magic disk which boots a complete Linux kernel into memory so you can work on your hard drives in safety.

Features include:
  • support for most Linux and Windows file systems (dual-booters can smile here)
  • image file can be compressed with gzip/bzip2 to save space on your backup media
  • image files can be split into segments to be copied on CDs/DVDs
  • imaging across networks using Partimage network support, Samba, NFS
Nothing Like a plan...
Two ground rules you should set before you start:

1. make sure you know ALL your partition names, mount points, labels, sizes and free space so that you don't get confused under Partimage - you'll see why soon. I screen-dumped the File Systems tab from  Gnome System Monitor as it lists the mounted names. Alternatively you could run Parted / GParted or save the text file  generated by the List Partition Tables utility on the Parted Magic CD.

In my case, I'm backing up /root, /home and /usr which is my baseline for my Ubuntu system. So that's SDA1, SDA6 and SDA7. Nearly all my data files live on the other Windows/NTFS, ext3 and vmdata partitions that I backup separately. No need to worry about the swap partition as I'll create a new one when I do a restore.

 2. My opinion: don't image your hard drive partitions while they are mounted. You want nothing nothing writing to, or deleting from the disk while you create the partition image; not your host operating system, any of your applications - or you, the user. Boot from the live-CD and run the tools from there.  Partimage doesn't need anything creating, moving and deleting files and folders while it attempts to create a true snapshot.

In true Columbo-style, 'just one more thing...'
Before backing up: make sure your target drive for storing image files has properly allocated read/write permissions, otherwise Partimage will give you a cryptic message when it can't write its temporary files on starting the backup.

Parted Magic Tricks
Hitting F12 during power on should give you a one-time boot menu so you can select Boot from DVD/CD. On boot-up, Partition Magic gives you a neat, clutter-free Xfce desktop, a single icon panel and slide-up menus for all the tools.

Mount/Unmount partitions
I start by firing up the Mount Utility, firstly to make sure none of the source partitions (to be backed up) are mounted; secondly so I CAN mount the destination partition for Partimage to write its' backup images! You tick the box for each item to mount/unmount. Some versions of the Mount Utility close on clicking the OK button without confirming completion. In the screenshot, I've relaunched it in order to discover the full mount point it assigned to my slow-but-reliable USB hard drive.

Yes, you can mount everything manually in a terminal if you really want...

At last - create partition image

Open a terminal session and enter partimage. Its a character-mode application, so no mouse, you move around with arrow and tab keys. Spacebar changes values in option lists, the active choice marked with asterisk *. This layout works and guides you through the process.

Set the partition to save/restore: this is where you need to know your partition details. Since I've got /home  and /usr on identical 6GB partitions, I need to identify which is which when I backup and restore. Be alert to the fact that Ubuntu called my (internal) source partitions SDA# while Partimage used HDA# for the source and SDA# for the (external) destination USB drive! Thankfully the identifying number is the same. Its easy to get partitions confused, that's nothing to do with the software, just operator error.

Set the image file to create/use: you need the full path including the device, root and target folder as well as the backup file name. Type carefully and proof-read before you move on. Spaces and punctuation characters in the name should be avoided. 

Action to be done: defaults to Save partition to a new image file.
Connect to server, IP/name and Port number are only if you are backing up across a network, otherwise leave blank. You will need appropriate user logon, password and write-permissions for network drives – Partimage will prompt for them when it begins.

Tab onto the Next option or press F5 key to move onto the next screen.

Compression Level: the guidelines are sound. None means no compression so you need a backup drive with space equivalent space to USED space on your source partition. I take this as a general rule, as compression ratios vary according to the contents; .doc and .odt files compress well, .jpeg and .avi may not compress at all. Have some free space as contingency.

Image Split: Automatic is fine if you know you'll span several disks and expect a prompt to change media.

File size: I always keep the default value as its easier to manage portable 2GB segments. Also if I get a read-error in one segment file I may still be able to salvage the rest.

Continue with F5 key again.

Partimage will confirm back to you what you've chosen.

OK this to continue
If you chose to Check Partition before backup, you get the information pane

OK this to start the backup.

You will get a progress bar with elapsed and estimated time to complete. Writing to my old USB drive is slooow.

You get completion status at the end of the backup.

Quick Guide to DVD backups
It is possible to write directly to DVD+R  and R/W disks (DVD-R are problematic, apparently).
  • Mount the DVD-drive first, then in Partimage,
  • set path for the image file to the DVD writer;
  • clear the "check for free space" option;
  • set the size of backup file segments to a safe multiple to fit on DVD, such as 1.5GB (3 segments to a DVD);
  • set it to "pause on volume changes" so you can swap in empty DVD's as they fill up.

Restore a disk image
Important - before restoring: make sure your replacement drive is in a fit state to boot a restored image. Partimage does NOT cover the master boot record (MBR) and bootloader. If you intend to restore to a brand new hard drive, you need additional tools to save and load your MBR, partition tables and a bootloader (all material for a separate How-to, but in the meantime, there's a useful guide in the on-line Partimage manual at Partimage-manual_Backup).

For this Howto, I cheated slightly; I installed a scratch-instance of Ubuntu 7.10 from the live CD with custom partitions matching my old ones. This verified my new drive as working inside 20 minutes complete with valid MBR and GRUB bootloader.  The rest will be overwritten when I run the  Partimage restore.

The restore process works much the same as the backup, you need to select the "Restore partition from an image file" option and identify your image file from the backup drive, mounted with read permissions. You should have your destination drive prepared with a target partition of equal or greater size to the uncompressed size of your image contents (I'll not go into creating and resizing partitions here, either). Partimage will obliterate existing data on any destination partition, so check your settings before you commit to a restore!

Before you reboot, make sure you have a valid Linux swap partition somewhere on the drive. GParted on the Parted Magic CD can create this.

Best of all, I know this works. I picked up a spare hard drive for the laptop for another project some time back, so I swapped out the main drive, put in the spare and  booted the Partition Magic CD. Launching Partimage, I restored SDA1, SDA6 and SDA7. I rebooted and was able to logon to my regular Ubuntu desktop. Result - 100% confidence in my base backups.

Now, what to do about the incremental backups...?

No comments:

Post a Comment

At least try to be nice, it won't kill you...