Backing up and restoring (cloning) a live disk using simple linux tools
September 7, 2014
NOTE: THIS DOESN'T WORK. THE RESULTING SYSTEM DOES NOT BOOT. AFTER THE BOOT SCREEN THERE IS JUST A BLANK WINDOW. I'D RECOMMEND CLONING THE DRIVE OFFLINE USING CLONEZILLA, THEN KEEPING A TAR OF THE FILESYSTEM WHICH CAN BE RESTORED OVER THE TOP OF THE CLONED DRIVE IF NEEDED.
After a lot of searching on a simple way to back up and restore an entire linux disk (on a live system) I ended up having to put together my own small process.
There are backup programs available, but they are too heavy, and there are tools to back up partitions and file systems, but I needed to back up an entire live disk.
First off, if you aren't doing this on a live system. I would recommend using the clonezilla live CD. It'll handle a whole lot of situations automatically and is designed for cloning, backing up and restoring whole drives.
The actual purpose was for backing up a small system drive for servers, so in the event of a failed system disk, there is a live backup disk that can just be used in place.
Disclaimer: This works for me, but because we are backing up a live system, there is potential for the backup to not be 100% consistent in the event that the file system has been modified during the backup.
For my purposes, this isn't really much of an issue. The only things being modified are log files, and I don't really care if I lose those during a restore. The main things I want to keep are the system configuration, drive partitioning, boot records, etc, and I will keep multiple copies of the backup and I don't care if it is a little out of date.
You could potentially modify this so it mounts the partition read only and separates /var onto another partition, back that up using tar or some other tools. Or we could back the whole thing up using tar instead of dump, but I'm after more of a clone than a filesytem copy. I want it as exact to the original system as possible.
Another way (possibly better?) would be to use an LVM snapshot. But I'm just running a simple filesystem off a USB drive and it seemed a bit overkill.
sgdisk (apt-get install sdisk)
sfdisk (should be on most systems)
dump (apt-get install dump)
grub-install (should be on your system)
mkfs.ext4 (or whatever filesystem you are using)
Taking the backup
So here is the simple way of doing it.
sfdisk -d /dev/sda > /backup_drive/part_table
This will just dump the partition table to a file.
IF THIS FAILS COMPLAINING ABOUT A GPT PARTITION TABLE THEN USE:
sgdisk -b/backup_drive/part_table /dev/sda
Notice the missing space after the -b
To deconstuct this, all we are doing is backing up (-b) to a file, the partition table from sda.
Disclaimer: Using this method, you need to restore to a similar device, otherwise the partition table may not fit the device you are restoring to. The alternative it to manually recreate the partition table when restoring, which is quite simple really.
Next we are going to dump each of the fiesystems. In my case there is just a single filesystem (plus a swap partition, but we don't care about saving that one).
dump -0f /backup_drive/system_dump /dev/sda1
What is this doing?. Doing a full backup (-0) to a file (-f) of the partition
Restoring the backup (Or creating a clone of our drive)
If earlier sfdisk worked then use this command:
sfdisk /dev/sdb < /backup_drive/part_table
The restore device will need to be of the same or larger size and same sector size, otherwise the partition table will need to be manually created.
Otherwise to restore the GPT table use:
sgdisk -g -l/backup_drive/part_table /dev/sdb
This command is loading (-l) from a backup and converting from mbr to gpt if needed (-g)
Note: if the disk isn't using a GPT partition table, then this will convert it to GPT. Alternatively you can use sfdisk if you want to backup and restore an MBR partition table. If it is converted to a GPT partition table, you may need to add a boot partition to re-install grub.
Optional (Use this to generate a new GUID, so if you are cloning the disk) sgdisk -G /dev/sdb
If you need to reparition this, you can do this now using your cgdisk or some other tool.