It's much simpler to just boot from a live CD and copy the entire disk image (partition table and all) to a file on external media with dd(1). When done with your experimentations, simply copy it back. This way you don't have to muck about with partitions, permissions, metadata, bootloaders, or anything. It takes a bit of time, but it is exceedingly simple and much less risky than other backup and restore methods. Here's an example. Assume that /dev/sda is the main disk in the machine that you're backing up and /dev/sdb1 is a partition on external storage.
mkdir /mnt/sdb1
mount /dev/sdb1 /mnt/sdb1
dd if=/dev/sda of=/mnt/sdb1/sda.img
This works great, but one obvious drawback is that the free space on sdb1 must be greater than (or by freakish coincidence, exactly equal to) the total size of /dev/sda. Unless, perhaps, we compress it. Then the minimum free space requirement on /dev/sdb1 becomes much lower. In effect, required space is equal to the amount of actual data on /dev/sda after it's been compressed. For example, if /dev/sda is 80GB in size, but the df(1) command reports that only about 10GB of it is in use, it's fair to guess that a compressed image of the disk should end up somewhere under 10GB in size.
So, there's no theoretical reason this couldn't work. But we have to recognize that the free space must compress well in order to get this kind of efficiency. Except in very specific circumstances, the "free" space on a hard disk is not actually empty. When you delete a file, the data is still there; the OS simply removes the references to the file. This data will be included in a dumped image of the disk and it may or may not compress well.
The solution is to fill up the free space with zeros. When it comes down to it, there's nothing that compresses better than a whole lot of nothing. For best results, this should be done on all file systems in addition to the swap partition.
The Backup Procedure
Mount the main disk and fill up the remaining free space on all partitions with a file containing all zeros. (/dev/sda1 is the only data partition in this example.)
mkdir /mnt/sda1
mount /dev/sda1 /mnt/sda1
dd if=/dev/zero of=/mnt/sda1/zero
Wait until you get the error "No space left on device", then remove the zero file on each partition.
rm /mnt/sda1/zero
Umount the partition.
umount /mnt/sda1
Zero the swap partition. (Shown as /dev/sda5 here.)
dd if=/dev/zero of=/dev/sda5
Reinitialize the swap partition. (Otherwise it won't be detected and enabled on boot after restore.)
mkswap /dev/sda5
Mount the backup partition.
mkdir /mnt/sdb1
mount /dev/sdb1 /mnt/sdb1
Copy the entire disk to an image file on the backup drive (/dev/sdb1), piping it through gzip for compression.
dd if=/dev/sda | gzip - > /mnt/sdb1/backup-sda.gz
Umount the backup device, reboot, and you're free to do whatever you like to your main disk, knowing that you have a good backup on hand.
The Restore Procedure
Restoring is even easier than backing up. Once you're up and running on the live CD:
mkdir /mnt/sdb1
mount /dev/sdb1 /mnt/sdb1
gunzip -c /mnt/sdb1/backup-sda.gz | dd of=/dev/sda
Unmount the backup drive, reboot, and you should have your original OS back just as it was before the backup.
2 comments:
Hello sir,
I just didn't understand, the swapping part, and what is sda5 ?
@Omar:
If you're not familiar with swap partitions in Linux, best to read up on it: https://www.linux.com/news/software/applications/8208-all-about-linux-swap-space/
Post a Comment