Cloning

From LQWiki
Jump to navigation Jump to search

You can clone a computer by copying all its files to another. There are some things you need to be aware of, that is what this topic is about.

Scenarios

  • You have two computers and want one to act like the other
  • You do Booting from USB and want to clone your USB disk

Reasons

  • You want to backup regularly
  • You want to enhance your computer park

Which program should you use to clone

  • cp -pr: Will not work because it does not handle devices and links correctly.
  • tar: Can be used to copy files, can handle devices, links and hidden files correctly.
  • rsync -avz: Should do the same as tar, it should only copy the files that differ. However, it has not yet been tested.
  • dd: Used to take byte after byte and copy it. So it does not allow to change the size of partition and does not allow to use different file systems.

Cloning a USB disk

with tar

To clone a USB disk

  • have your target disk deleted
  • have your target disk bootable (see Booting_from_USB)
  • have your target disk mounted (I assume under /mnt/sdb1)
  • have your system booted from your source disk
  • use tar to copy the files over
cd /
tar -cv $(ls -1 | grep -Ev "proc|sys|tmp|media|mnt") | (cd /mnt/sdb1; tar -xv)

The above command excludes some directories from the copy: /proc and /sys that store the kernel's state, /tmp that is for temporary files and /media and /mnt that are for mounted filesystems.

Cloning between computers

With tar

Clone your computer like this:

  • Have your source and target computer in one network.
  • Use tar to transmit the files. This tutorial assumes your target's hostname is target, and you are working on the source computer:
cd / 
tar -cvl -f- / | ssh root@target "(cd /; tar -xv)"

with the parameter l you only tar the local filesystem and do not need to care about mounts, /proc and /sys. Maybe your computers have different hardware, e.g. network cards or disk controllers. Then you should leave the /boot directory on your target computer untouched - obviously, you cannot apply a kernel then. In this case, you can use the following transmission command:

cd /
tar -cv -f- $(ls -1 | grep -Ev "proc|sys|tmp|media|mnt|boot") | ssh root@target "(cd /; tar -xv)"

The above command excludes some directories from the transmission: /boot that contains the kernel and initrd, /proc and /sys that store the kernel's state, /tmp that is for temporary files and /media and /mnt that are for mounted filesystems.

With dd

See clone a disk using dd.

See also