Clone a disk or partition
Usually it is wise to have backup copies of files. But quite often you might need to copy or actually clone an entire disk or partition. And sometimes there may arise a need to clone or transfer your system disk to another disk together root partition, MBR, Grub etc and make it bootable.
This could happen when:
- A) you want to experiment with operating system upgrade or some other unpredictable or risky procedure
- B) you want to change your boot disk for a bigger or faster disk, but do not want install everything from scratch
- C) you want to have an backup of partition, directory or disk and possibly store the backup over network
Regardless of a case this basically means that you have to copy whole disk or certain partitions to another disk. If this partition contains only data and nothing that affects booting or operating of system itself, then the procedure is very simple. If you want to make an operational copy of system disk with boot and root partitions, then it is a bit more complicated, but still quite simple.
When we are dealing with Linux, you do not have to purchase expensive applications. There are several free, open source tools available, and some of them are (probably) already installed on your machine. I’ll mention briefly some of them, and then show an example using one of them. But first you should be aware of a handy tool, that you need in some of these procedures, namely the System Rescue CD. It includes numerous utilities that come handy also when something is broken. You can burn System Rescue on CD or install it on USB memory stick. Then just boot your machine with it, and you can repair grub if something went wrong, copy partitions etc.
Backup and cloning applications
Rsync is installed by default in Ubuntu. It can be used from command line, but there are several GUI frontends available, e.g. GRsync. With this tool you can keep files and even directory trees synchronized over network. It is quite fast since it sends only differences in the files. Rsync is included in System Rescue CD.
Bacula is an application to manage backup, recovery, and verification of computer data across a network. It is scalable from single computer to hundreds of computers. Bacula may be an overkill, if you are looking for a simple solution.
Partimage is an application to save entire partition in an image file. It supports ext2, ext3, reiserfs, fat32, hpfs and ntfs. It backups only used blocks of the partition, and you image can be compressed and even splitted. Partimage is included in System Rescue CD.
Clonezilla is not an application, but an application suite on Live CD (just like Sytem Rescue CD). It includes Partimage and several other utilities. As such it has same limitations as Partimage.
FSArchiver is a filesystem archiver, and you can use it to backup filesystem by files. Since you are working with files (and not partitions) you have more flexibility. Target partition size may differ from original. It supports even NTFS filesystem. Archives can be compressed, splitted and even encrypted. FSArchiver is included in System Rescue CD.
dd is an old and common UNIX program – and very useful – since you can make a low-level copy of raw data. Instead of files you are copying bytes or blocks of raw data. You should be careful when using it, some mistakes may wipe all your real data into oblivion. But otherwise it is safe and useful tool, since it does not care what filesystems there are on the disk (since it copies bytes and blocks). With dd you can create an exact ISO disk image of CD-ROM, for example. Or you can use dd to wipe an entire disk with random data etc.
cp is often overlooked as a backup tool, but in many situations it is a perfect tool for backup and cloning. And it is included in every Linux distro there is available.
Example 1. Copy (Move) Root Disk to Another Disk
Lets say that you have a computer with one disk and you want to change that disk for faster or bigger disk. But you do not like the idea of installing operating system and configure all applications once again. Well, you do not have to. With this simple procedure you can copy your installed system as it is to another disk and make it bootable. You will use the familiar cp command within command line shell and then configure the copy to act as a new boot and root disk.
Parts of this procedure are applicable to other situations also. I use cp command as an example, since you will surely find it on any Linux Live CD. dd could be used an alternative, if you want an exact copy of partitions, but I find cp more flexible. Since you are copying contents of file systems (instead of copying partitions) you may partition the target disk any way you want and reorganize your directories to entirely new partitions if you want.
Since we are cloning root disk we have to boot from Live CD. You should not do this from the installed operating system.
N.B. When you use System Rescue CD, you are automatically root, so you do need sudo at all. However, if you are using your normal system, then you usually have to put sudo before all system commands. I have left out sudo from all commands below.
1. Partition the new disk
First you have to partition new disk. Since we are using cp, the target partitions can be of any size, as long as they have enough room for files. This way your new root partition could be even smaller than before. If you prefer, you can use GParted or command line tools. You have to make at least root and swap partitions, but could opt for separate home partition, too.
How you connect the target disk to your machine depends on your machine. In some cases (e.g. laptops) you may have to use USB connectable caddy. Some SSD drives (e.g. Buffalo MicroStation) have both SATA and USB interfaces. When the drive is connected, you should find out what device it is. Usually your boot disk is /dev/sda. If you do not have other HDDs, then your DVD-drive is probably /dev/sdb.
I happen to like command line, so I make partitions with fdisk.
where X is the letter of your TARGET drive. Commands
n - makes new partition
d - delete partition
t - changes partition id (type), default is 83 (normal linux partition), 82 is swap partition
a - toggle bootable flag
p - shows current partition table
w - writes partition table to disk
q - quits fdisk without writing partition table
? - shows list of all commands
are interactive, they ask partition number etc. Lets keep it simple, and make only root partition (dev/sdX1) and swap (/dev/sdX2) partition. Remember to make 1st partition bootable with command a. When you have written the partition table, you have to make filesystems.
mke2fs -t ext4 /dev/sdX1
Here I assumed that filesystem type will be ext4. You can change it to something else.
If you like you could make more partitions, e.g a separate partition for /home or /var. It is not necessary, but sometimes it is useful. Actually /home partition could be on a separate disk.
2. Copy the data
In order to copy the data between disks (and partitions) you have to mount them first. Since we booted from System Rescue CD, none of the physical disks are mounted automatically. Lets assume that the original disk also has only one data partition (/dev/sda1) and swap partition.
mount /dev/sda1 -t ext4 /originalroot
mount /dev/sdX1 -t ext4 /cloneroot
If your new disk is SSD disk, then you should tweak it with
tune2fs -o journal_data_writeback /dev/sdX1
And then we just copy everything from original to clone
cp -a /originalroot/* /cloneroot
Copying instead of moving (mv) has the advantage, that if something goes wrong, you still have the original disk intact and operational. Now clone disk contains every file the original disk had. But its not usable yet as a boot disk. You have edit filesystem table and update/install grub on clone disk.
When copying is finished, it is safe to unmount the original disk.
This way you cannot accidentally change or delete anything, and your old disk is bootable.
3. Edit fstab
Your clone disk has filesystem table, but all UUIDs are incorrect, since they refer to the partitions of the original disk. First find out UUIDs on new partitions with command
This will list UUID of every partition. Look for /dev/sdX1 and /dev/sdX2. Copy them somewhere. Then open and edit /cloneroot/etc/fstab. Change existing to UUIDs to those which you found in previous step.
If you are changing to SSD disk, then you could also update parameters to match this example of /etc/fstab
# proc /proc proc nodev,noexec,nosuid 0 0 UUID=ROOT_UUID / ext4 noatime,barrier=0,data=writeback,nobh,commit=100,errors=remount-ro 0 1 UUID=SWAP_UUID none swap sw 0 0 tmpfs /tmp tmpfs exec,defaults,noatime 0 0 tmpfs /var/tmp tmpfs exec,defaults,noatime 0 0
At this point, I suggest you shutdown your machine, take out the old disk and put the new disk in its place. Then reboot your machine again with System Rescue CD. This way nothing should go wrong when you update grub in the next phase.
4. Install Grub
Even if your filesystem table is in order, the disk won’t boot unless you update grub first. In order to do this you have chroot to clone disk, and follow this procedure. You can use this procedure to repair a broken grub on any boot disk. We have already mounted to new root partition /cloneroot.
for i in /dev /dev/pts /proc /sys; do mount -B $i /cloneroot$i; done
chroot /cloneroot /bin/bash
-- press CTRL-D to exit chroot !!!
for i in /sys /proc /dev/pts /dev; do umount /cloneroot$i; done
Now you can exit Live CD environment and reboot your machine normally (without System Rescue CD). It should boot with all applications and configurations you had before.