Cloning Systems

From docwiki
Revision as of 13:49, 29 October 2020 by Mond (talk | contribs)
Jump to: navigation, search


Motivation

Once you have setup a system you might want to have a copy of that same system without the need to configure it the same. Of course: When you install the same lists of packets you have a similar system. If you maintain a larger number of systems you will also want to use a configuration management system to configure them all alike. But still you might find situations where it is convenient to be able to create a clone of an existing system.

Cloning with dd

When your system is not running you can make a 1:1 copy of your disk. You should not do this when the system is running since you will not get a consistent state of filesystem. One way to do this is by booting your system from a Live-CD of a Live-USB drive. Those live systems run off CD or USB and you can safely access the data on the hard drive.

E.g. If your harddrive is /dev/sda and you have connected an external USB drive and you want to store a .gz compressed image of your complete harddrive you could run:

dd if=/dev/sda bs=1M | gzip > /media/mydrive/img.gz

If you then boot your target system from a live CD you can overwrite the harddrive with

#ATTENTION: this overwrites your harddrive
zcat /media/mydrive/img.gz | dd of=/dev/sdX bs=1M

where /dev/sdX should be replaced with the disk you want to overwrite.

E.g. The images for installing a raspberry pi often come in this way and you can use dd to write them to an SD-card.

The new system of course needs a disk of the same size or bigger. If the disk is bigger you can later add the remaining free space at the end to be used by your system.

Cloning with tar

Of course you can also create a backup of your files and then restore it on a target system but it is a bit more complicated. The problems you will be facing are:

  • Your system might consist of multiple partitions and you you need to copy all of them.
  • Some files on your root partition (/) are hidden because other filsystems are mounted over it. Especially /dev
  • You will have to create a boot sector on your target system to be able to boot into your clone.
  • You need to be careful to copy all special permissions and attributes of your files.

Let's assume we have a system that consists only of a / and a /boot partition and we use ssh to copy this to a different system:

In order to have good access to our root partition we first do a bind mount:

mkdir /mnt2
mount --bind / /mnt2

Now we see everything that is in our root partition also under /mnt2. E.g.: /mnt2/dev/ contains the files that where initially there before /dev was mounted via the virtual file-system that contains the devices that the kernel created on the fly.

cd /
tar --xattr