Hm, yes, the above methods work ... often, but that's not guaranteed and depends on the disk geometry to be identical to be bootable and giving an identical partition layout. Keep in mind that anything that's dd-based is a byte-by-byte copy of the entire device, so even if you have just 10% of the capacity used on a 500G drive, you still transfer the entire 500G, which, as was pointed out, takes a long time.
I have been using variants of a different scheme for many years. It allows to clone to a disk that has a size different from the original, and also allows to make relatively small-sized backups that can serve as restore points.
1) The idea is that you re-create the same partition layout on the target disk. This even allows you to adjust the partition sizes in the process as long as they are large enough to hold the original content. This is useful if you clone your system to a smaller disk and want to be less generous with the sizes.
2) We then format the new partitions on the target disk the same way.
3) We mount the new partitions somewhere, and
4) we copy (preferably with rsync) the content of each data partition to the equivalent one of the target drive
5) we make the new disk bootable.
For example, here is a partition layout on one of my servers:
Quote:
Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 12584959 12582912 6G 83 Linux
/dev/sda2 12584960 488397167 475812208 226.9G 5 Extended
/dev/sda5 12587008 54530047 41943040 20G 83 Linux
/dev/sda6 54532096 71309311 16777216 8G 82 Linux swap / Solaris
/dev/sda7 71311360 92282879 20971520 10G 83 Linux
/dev/sda8 92284928 488397167 396112240 188.9G 83 Linux
|
that I obtained with the "fdisk -l" command that started this thread.
Using fdisk or parted, create the same layout on the target disk. If you do not want to change anything, and if your disk is below 2G, you can use sfdisk to streamline that process - make sure you don't confuse source and target disk!
sfdisk -d /dev/sda | sfdisk /dev/sdb
(for peace of mind, you might try adding the -n option to make a "dry run" first and see that all is ok:
sfdisk -d /dev/sda | sfdisk -n /dev/sdb
)
You should end up with the exact same layout on /dev/sdb as you have on /dev/sda.
Note: I rarely use this because you can end up with unused space. It's just a few keystrokes to accomplish that layout interactively and subtly adjust sizes so you fill the entire disk. Also, I always make it so that the last /dev/sda8 partition is mounted as /home, and I increase or shrink the space here and /home takes the "overflow", if any.
Here is what is mounted as what on my system:
Quote:
/dev/sda1 on / type ext4 (rw,relatime,data=ordered)
/dev/sda5 on /usr type ext4 (rw,relatime,data=ordered)
/dev/sda7 on /var type ext4 (rw,relatime,data=ordered)
/dev/sda8 on /home type ext4 (rw,relatime,data=ordered)
|
and /dev/sda6 is the swap partition.
I format the new partitions on sdb the same way:
Quote:
mkfs -t ext4 /dev/sdb1
mkfs -t ext4 /dev/sdb5
mkfs -t ext4 /dev/sdb7
mkfs -t ext4 /dev/sdb8
mkswap /dev/sda6
|
Before going on, here are the actually used sizes:
Quote:
/dev/sda1 6061632 1985996 3744680 35% /
/dev/sda5 20511356 10307220 9139176 54% /usr
/dev/sda7 10190136 5010396 4639068 52% /var
/dev/sda8 194815596 52783900 132112508 29% /home
|
You see that the relatively large (20G) /usr partition (sda5) is only 54% used, so if we were to clone to a significantly smaller disk (often happens if you switch from spinning disk to SSD), we could recoup some space by reducing the size to, say, 15G only.
Now let's make the corresponding mount points:
Quote:
mkdir /mnt/n{root,usr,var,home}
|
(the "n" stands for "new"). You mount the target partitions:
Quote:
mount /dev/sdb1 /mnt/nroot
mount /dev/sdb5 /mnt/nusr
mount /dev/sdb7 /mnt/nvar
mount /dev/sdb8 /mnt/nhome
|
and then we rsync the file systems over one by one:
Quote:
rsync -ax / /mnt/nroot
rsync -ax /usr/ /mnt/nusr
rsync -ax /var/ /mnt/nvar
rsync -ax /home/ /mnt/nhome
|
(note that each source in rsync ends with a slash (/) - important. The "x" says not to cross file system boundaries, which is needed here.)
How to make the target disk bootable depends on whether you use grub or grub2, but I usually chroot to the new root partition and make it bootable there. (I can provide more info if you don't know how).
At this point, we have a guaranteed-to-work clone in hand.
This method is super-versatile. I use this scheme for many other applications, and as you may have guessed, I have this all scripted. I often install dozens of machines in one fell swoop, and I use this method to set up each system from a "restore point" - the ultimate backup. In this way I get 100% identical systems, and all it takes is to customize a few configuration files to give the new clone its new identity (that is automated, too). Ip address, hostname, ssh host keys, and the like.
How to get such a restore point?
Staying with my partition layout, I make a tarball of each partition's contents:
Quote:
cd /
tar -c --one-file-system . | gzip -3c > /some_external_backupdisk/root.tar.gz
cd /usr
tar -c --one-file-system . | gzip -3c > /some_external_backupdisk/usr.tar.gz
cd /var
tar -c --one-file-system . | gzip -3c > /some_external_backupdisk/var.tar.gz
cd /home
tar -c --one-file-system . | gzip -3c > /some_external_backupdisk/home.tar.gz
|
If my harddisk crashes one day, I boot from a CD and do the same make partitions/format/mount steps, only instead of the rsync commands above I populate the partitions from the tarballs. Make the new disk bootable as before, done.
I hope this helps and shows a more versatile way.