Tips and howtos for the unix / linux systems administrator.
How to clone a linux installation with tar
This describes an easy way to copy one linux hard drive to another, so that, for example, you can "clone" an installation to a new/bigger hard drive, or to a hard drive that will be installed in a different system. "NEW" represents the appropriate device node of a partition on the new hard drive, and "OLD" represents the corresponding device node partition on the old hard drive, as mounted in the live "rescue mode" cd, pen drive, etc..
- Have on hand a bootable linux system on a thrid media - a cd, a usb pen drive, a third hard drive, etc. For examples, Knoppix, Xubuntu Live CD, or RIP Linux (for this purpose, I prefer RIP)
- Boot up the pen drive / cd linux and login as the root user
- Create the partition on the new hard drive using a utility like cfdisk. (HINT: the command "sfdisk -l" will show you technical information for all detected drives attached to the system, both those mounted and not mounted.) CAUTION!! ONE MISTAKE HERE COULD NUKE YOUR OLD HARD DRIVE AND ITS DATA!!!! Remember that if the new hard drive will hold a bootable linux installation, you'll need to mark the appropriate partition "bootable" with the fdisk utility and that you probably will want to create a swap partition. After partitioning is completed, create the filesystem with fsck. Inexperienced admins should create the partitions identically on the new hard drive as on the old hard drive; gurus will know how to modify these steps to allow for different partition schema on the 2 drives. After the filesystem creation is complete, you can tweak it with tune2fs to create labels, journals (ie. upgrade the filesystem to ext3), specify the number of mounts between filsystem checks, etc. The command I usually use for the root filesystem, for example, is:
tune2fs -c 3 -L rootfs -j /dev/sdb1
This says to set the filesystem check to occur every three mounts (-c 3), to label the partition "rootfs" (-L rootfs), and to create an ext3 filesystem journal. This example assumes the new partition you are tweaking is the first partition partition on the second detected hard drive (/dev/sdb) - substitute the correct device and partition node for your case.
- Mount the new and the old partitions in 2 different directories, for example /NEW and /OLD
- Execute the following commands... CAUTION!! TYPOS COULD BE FATAL!!:
cd /OLD
tar -cSp --numeric-owner --atime-preserve -f - . | ( cd /NEW && tar -xSpv --atime-preserve -f - )
- The /OLD partition is now copied to the /NEW partition.
- Repeat this process for each partition that needs to be copied from the old drive to the new drive.
- If the new hard drive holds a bootable linux installation, and if the partition device nodes will be different on the new hard drive when natively booted as compared to the old hard drive when natively booted, edit the file "etc/fstab" that is held on the new drive (for example, if the "etc" tree on the new hard drive is in the /NEW mount, "vi /NEW/etc/fstab") and adjust the device node references.
- If the new hard drive will be a bootable linux installation, you will now need to deal with installing the bootloader (usually grub or lilo).