rsync

rsync

linux-logo debian-logo

Copying/cloning partitions with rsync

rsync is an absolutely fantastic, great and fast tool to copy / clone a complete partition to another partition; whether the second partition is on the same disk, on another disk on the same PC or even on a disk of a remote PC. We have tested and are regulary using the procedures described below on both SuSE and Debian (Sarge) Linux ditributions. Just note, that on both, the source and target PCs the program 'rsync' has to be installed. This is usually the case in all distributions; if not, install it from the installation CD (SuSE) or enter apt-get install rsync in case you are running Debian Linux.

We generally have two identical disks on our PCs and we regularly copy all the contents from the first to the second disk for backup purposes. The first time, we clone the complete disk with the following procedure:

- Boot from a SuSE installation CD or DVD and choose 'Rescue System'
  or, alternatively and preferred, because the SuSE CD's (not even in version
  9.1) do not recognize S-ATA disks, boot with a recent Knoppix CD
- In rare cases, where Knoppix does not correctly detect the graphics card and
  you see a flickering screen, just enter Alt-Cntl-F1 and get a
  console to login as root
- The following command then copies your first to your second disk:
  cp /dev/hda /dev/hdb
  (Well, on some laptops the second disk may be /dev/hdc and with S-ATA
  disks you may have to enter:  cp /dev/hde /dev/hdg )
- After having cloned your disk, you will have the same partitions as on
  the first disk.
From now on, having the same partion table on both disk, you will only have to copy those partitions to the second disk, where modifications were done.
In order to save our data from the root file system / to the second disk in our PC, we just have to make sure, that the destination partition is correctly entered in the file /etc/fstab .
Say, your root file system / is on /dev/hda6 (see /etc/fstab), then after the cloning of the first disk your equivalent partition on the second disk is on /dev/hdb6 . By doing the following commands, you create a mount point for the root file system of the second disk on the first disk:
cd / ; mkdir 2 ; cd 2 ; mkdir rootfs
Make sure, your /etc/fstab now has the following two lines:
/dev/hda6       /               reiserfs   defaults     0  0
/dev/hdb6       /2/rootfs       reiserfs   noauto,user  0  0
Should you have another filesystem than 'reiserfs' (e.g. 'ext3'); make sure this file system type is entered in /etc/fstab in column 3.

Now, the fun part may begin: making the contents of /2/rootfs the same as on / the same, you first nedd to mount the partition on the second disk:
mount /2/rootfs In order to have the partition /dev/hdb6, now mounted on /2/rootfs, with the same content as / , you simply enter the following command:
rsync -avHx --delete / /2/rootfs
This will take some time, depending on the size of the partition, the number and size of files, which are there, and also depending on how many and files have been modified since you did such an 'rsync' the last time.
For a detailed description of the rsync-command, see its man page: man rsync. Should you have installed the web browser 'konqueror', simply enter man:rsync in the small window where you usually enter web-addresses and you will see a nicely formated man-page of rsync.

One of the advantages of 'rsync' is, that the source and destination partitions do not have to be of the same size; it is sufficient, that the destination partition has at least the size, which is used by files on the source partition.

Now, we describe an example of how a partition is copied / cloned / rsync'ed to a remote machine.
Let us assume, you want to clone a partition from a running system with the hostname 'zak' to another PC called 'zik'. It is probably a good idea, not to clone the 'running' partition of PC 'zak' to the remote PC 'zik', but rather the copy of the root file system of 'zak' on its second disk to the remote PC.

So, we first make sure, that the partition on PC 'zak' is mounted:
On PC 'zak' we enter: mount /2/roots
On the remote PC, booted with e.g. Knoppix, we check the file /etc/fstab an look, where the approriate partition is mounted; this could be /mnt/hda6 - so we enter on the remote machine mount /mnt/hda6

We experienced, that when a remote machine is booted with Knoppix, we had to do two things to make an rsync successful: first, give a password to the remote machine: enter passwd and give a password. Second, the ssh-daemon should be running, so enter: /etc/init.d/ssh start

Now, the copying of the partition may start: On the source PC enter
rsync -avHx --delete /2/rootfs/ root@zik:/mnt/hda6

This will, after you have entered the root-password of 'zik' copy the entire partition /2/roots from PC 'zak' to PC 'zik'.

Three things are important when using rsync:
1. The source partition must be mounted on the source PC (see df -h)
2. The destination partition must be mounted on the detination PC
3. The source partition in the rsync-command must end with a '/'

Example: You want to copy the partition /home from PC 'zak' to PC 'zik'; then you have to enter on PC 'zak' the following command:
rsync -avHx --delete /home/ root@zik:/home
A useful description on this feature of the 'trailing slash' can be found on http://www.mikerubel.org/computers/rsync_snapshots/#Rsync

Of course, instead of 'sending' a partition, you may also do it the other way round, by 'receiving' a partition from a remote PC. Thus, the example above would look as follows, if you fetch a partition on PC 'zik' from PC 'zak':
rsync -avHx --delete root@zak:/home/ /home
And please remember also in this case, that the source partition (here /home/) must have a trailing slash '/', in order to work as wished.

Should you have copied a partition to a new PC, you may have to install the Master Boot Record (MBR) in order to allow a proper boot. If you use 'grub' as boot loader, you may find our GRUB issues page useful.

Last Update: 18Jul2004 uk   -   Created: 15Jul2004

Go to our Linux homepage