Difference between revisions of "Cloning a computer"

From Linuxintro
imported>ThorstenStaerk
(New page: Cloning a computer means you have one computer and want to copy "everything" to another. This can mean different things and can have different reasons. = Reasons = * You want to have a st...)
 
imported>ThorstenStaerk
 
(31 intermediate revisions by 4 users not shown)
Line 1: Line 1:
Cloning a computer means you have one computer and want to copy "everything" to another. This can mean different things and can have different reasons.
+
Cloning a computer means you have one computer and want to copy the complete harddisk to another. This can mean different things and can have different reasons.
  
 
= Reasons =
 
= Reasons =
* You want to have a stand-in computer in case your "pet computer" decides to go on vacation (it breaks for whatever reason). For this, a virtual machine is not enough. It is sufficient to copy every file from computer A to computer B.
+
* You want to have a [[backup]] of your computer. In this case it is enough to copy every file from hard disk A to hard disk B.
 
* You want to [[virtualize your computer]]. In this case you will have to dump every byte from your source (physical) to your target (virtual) computer.
 
* You want to [[virtualize your computer]]. In this case you will have to dump every byte from your source (physical) to your target (virtual) computer.
 +
* You are an administrator and want to deploy many computers with the same configuration.
 +
 +
= Ways =
 +
You can either clone byte-by-byte or file-by-file. The advantages are:
 +
* cloning byte-by-byte will clone the boot sector. The system will stay bootable.
 +
* cloning byte-by-byte will even work on a file system the computer does not understand, e.g. an encrypted Windows partition.
 +
* cloning file-by-file will allow you to deploy the system on a smaller partition
 +
* cloning file-by-file will not clone parts of a disk that are not part of a file
 +
 +
= ToConsider =
 +
* Compression keeps your backup small in file size. Plus it accelerates your backup procedure if you have a fast processor and a slow hard disk or network.
 +
* Your /boot/grub/menu.lst will list entries for a boot menu to boot from. The root device can be stated there as /dev/sda, /dev/by-id/whatever, /dev/by-uuid/whatever, /dev/mapper/whatever or some other ways to name the disk. The way that is optimal for cloning is to use the /dev/disk/by-uuid name because this name will be cloned with the disk and be the same even if deployed on another harddisk.
 +
* You may be using more than one partition or harddisk during normal operations. In this case you may want to clone all harddisks/partitions.
 +
 +
= Over the network =
 +
 +
== file-by-file copy ==
 +
To store a backup of one computer on the other via [[network]], use the command:
 +
cd /
 +
tar -cvz $(ls | grep -v proc)  | ssh root@<abbr title="replace by your target computer">192.168.178.3</abbr> "cat ><abbr title="replace by the name of your backup file">slash.tar.gz</abbr>"
 +
 +
To clone a computer over the network, say:
 +
cd /
 +
tar -cv $(ls | grep -v proc)  | ssh root@<abbr title="replace by your target computer">192.168.178.3</abbr> "(cd <abbr title="replace by the path where you want to restore, typically /">/public/ubunturoot</abbr>; tar xv )"
 +
 +
Afterwards you may want to change
 +
* IP address, netmask, gateway, name server, time server, hostname
 +
* /etc/fstab to contain generic device names like /dev/sda1 instead of /dev/disk/by-uuid/7e9e1890-312e-43eb-8ebb-82fe03b62732
 +
* /boot/grub/menu.lst to contain generic device names
 +
 +
== byte-by-byte copy ==
 +
This is how you can clone the harddisk of any computer, even if it is an encrypted Windows computer.
 +
Boot the computer from Knoppix, [[open a console]], enter
 +
[[dd]] if=/dev/sd''x'' | bzip2 -z | ssh root@''192.168.0.5'' "(cat >backup-sd''x''.bz2)"
 +
Be sure to replace /dev/sd''x'' by the harddisk you want to clone and ''192.168.0.5'' by your target computer's IP.
 +
 +
= local =
 +
 +
== file-by-file copy ==
 +
Local cloning is e.g. to a USB disk to [[make a USB disk bootable]]. In this case we assume the target disk is ''/dev/sdx2''. To clone your harddisk:
 +
* make sure there is no DVD, CD, network drive and other things mounted
 +
* mount the target harddisk to /mnt:
 +
mount ''/dev/sdx2'' /mnt
 +
* issue the [[command]]s
 +
cd /
 +
tar -cv $(ls | grep -v proc | grep -v sys | grep -v mnt) | ( cd /mnt; tar xv )
 +
[[mkdir]] /mnt/proc /mnt/sys /mnt/mnt
 +
 +
== byte-by-byte copy ==
 +
This is how you can clone the harddisk of any computer locally (e.g. to a USB drive), even if it is an encrypted Windows computer.
 +
* boot the computer from Knoppix
 +
* [[open a console]]
 +
* mount the USB disk, e.g.
 +
mount /dev/sdz1 /mnt/usb
 +
* enter
 +
[[dd]] if=/dev/sd''x'' | bzip2 -z | cat >/mnt/usb/backup-sd''x''.bz2
 +
Be sure to replace /dev/sd''x'' by the harddisk you want to clone.
 +
 +
= Post cloning steps =
 +
* If you are cloning one harddisk to another byte-by-byte to boot from the other harddisk, make sure the harddisk's ID (/dev/disk/by-id) is not in /boot/grub/menu.lst. Rather, the harddisk's UUID should be there because this will be cloned with the other data.
 +
* change hostname
 +
* change IP address
 +
* under some distributions, e.g. SUSE you have to re-configure [[networking]]. The IP address will be bound to an unknown network device.
 +
* change /etc/hosts so the computer can ping itself
 +
* consider changing the ssh keys ~/.ssh/id_*sa*
 +
 +
= TroubleShooting =
 +
 +
== Your computer does not boot ==
 +
If you have cloned a harddisk byte-by-byte and now booting from it does not work, check the device name you are booting from in /boot/grub/menu.lst. The device ID of your harddisk will change, but the UUID will come with the image. So make sure you boot from the device under /dev/disk/by-uuid.
 +
 +
[[Category:Guides]]

Latest revision as of 12:27, 28 March 2015

Cloning a computer means you have one computer and want to copy the complete harddisk to another. This can mean different things and can have different reasons.

Reasons

  • You want to have a backup of your computer. In this case it is enough to copy every file from hard disk A to hard disk B.
  • You want to virtualize your computer. In this case you will have to dump every byte from your source (physical) to your target (virtual) computer.
  • You are an administrator and want to deploy many computers with the same configuration.

Ways

You can either clone byte-by-byte or file-by-file. The advantages are:

  • cloning byte-by-byte will clone the boot sector. The system will stay bootable.
  • cloning byte-by-byte will even work on a file system the computer does not understand, e.g. an encrypted Windows partition.
  • cloning file-by-file will allow you to deploy the system on a smaller partition
  • cloning file-by-file will not clone parts of a disk that are not part of a file

ToConsider

  • Compression keeps your backup small in file size. Plus it accelerates your backup procedure if you have a fast processor and a slow hard disk or network.
  • Your /boot/grub/menu.lst will list entries for a boot menu to boot from. The root device can be stated there as /dev/sda, /dev/by-id/whatever, /dev/by-uuid/whatever, /dev/mapper/whatever or some other ways to name the disk. The way that is optimal for cloning is to use the /dev/disk/by-uuid name because this name will be cloned with the disk and be the same even if deployed on another harddisk.
  • You may be using more than one partition or harddisk during normal operations. In this case you may want to clone all harddisks/partitions.

Over the network

file-by-file copy

To store a backup of one computer on the other via network, use the command:

cd /
tar -cvz $(ls | grep -v proc)  | ssh root@192.168.178.3 "cat >slash.tar.gz"

To clone a computer over the network, say:

cd /
tar -cv $(ls | grep -v proc)  | ssh root@192.168.178.3 "(cd /public/ubunturoot; tar xv )"

Afterwards you may want to change

  • IP address, netmask, gateway, name server, time server, hostname
  • /etc/fstab to contain generic device names like /dev/sda1 instead of /dev/disk/by-uuid/7e9e1890-312e-43eb-8ebb-82fe03b62732
  • /boot/grub/menu.lst to contain generic device names

byte-by-byte copy

This is how you can clone the harddisk of any computer, even if it is an encrypted Windows computer. Boot the computer from Knoppix, open a console, enter

dd if=/dev/sdx | bzip2 -z | ssh root@192.168.0.5 "(cat >backup-sdx.bz2)"

Be sure to replace /dev/sdx by the harddisk you want to clone and 192.168.0.5 by your target computer's IP.

local

file-by-file copy

Local cloning is e.g. to a USB disk to make a USB disk bootable. In this case we assume the target disk is /dev/sdx2. To clone your harddisk:

  • make sure there is no DVD, CD, network drive and other things mounted
  • mount the target harddisk to /mnt:
mount /dev/sdx2 /mnt
cd /
tar -cv $(ls | grep -v proc | grep -v sys | grep -v mnt) | ( cd /mnt; tar xv )
mkdir /mnt/proc /mnt/sys /mnt/mnt

byte-by-byte copy

This is how you can clone the harddisk of any computer locally (e.g. to a USB drive), even if it is an encrypted Windows computer.

  • boot the computer from Knoppix
  • open a console
  • mount the USB disk, e.g.
mount /dev/sdz1 /mnt/usb
  • enter
dd if=/dev/sdx | bzip2 -z | cat >/mnt/usb/backup-sdx.bz2

Be sure to replace /dev/sdx by the harddisk you want to clone.

Post cloning steps

  • If you are cloning one harddisk to another byte-by-byte to boot from the other harddisk, make sure the harddisk's ID (/dev/disk/by-id) is not in /boot/grub/menu.lst. Rather, the harddisk's UUID should be there because this will be cloned with the other data.
  • change hostname
  • change IP address
  • under some distributions, e.g. SUSE you have to re-configure networking. The IP address will be bound to an unknown network device.
  • change /etc/hosts so the computer can ping itself
  • consider changing the ssh keys ~/.ssh/id_*sa*

TroubleShooting

Your computer does not boot

If you have cloned a harddisk byte-by-byte and now booting from it does not work, check the device name you are booting from in /boot/grub/menu.lst. The device ID of your harddisk will change, but the UUID will come with the image. So make sure you boot from the device under /dev/disk/by-uuid.