How to Backup Partitions

Accesses to this page since 19/08/2006: Page Visit Counter

Index


Partimage

Partimage is a program that allows one to backup/restore the partitions of a disk. It is similar to Norton Ghost, the only difference being that the size of the restored partition must be greater or equal than the size of the saved partition. The goal of saving a partition is either for safety, in the event a disk failure, or to replicate it on several other computers.

For backing up a Linux system (without /home), it is necessary only four or five CDs, in general. As a consequence, in less than 30 min, everything is saved and the system can be upgraded safely, since it can be restored if the result of the upgrade is not adequate.

In order to make a backup, the partition can not be mounted. Therefore, the easiest way is using a live distribution, such as SystemRescueCd. Another possibility is just to umount it, if it is not being used.


Creating Partitions

Disks are divided in partitions, which can be primary or extended. For historical reasons, there is a maximum of four primary partitions. However, an extended partition can hold up to 63 subpartitions. Therefore, if the total number of partitions is greater than four, one of them must be extended. It should be noted that there is no advantage in a partition being primary or extended. However, booting is only possible from a primary partition, which must be made bootable, and it is called the active partition. The active partition can be checked by running: sudo fdisk -l /dev/sdX (it is marked with an "*").

The size of the partitions is important if a backup is necessary. The greater the partition, the greater the backup. For a Linux system running on a workstation, we suggest four partitions:

/boot (100 MB) - 18% Used with 3 kernels installed
/ (15360 MB = 15 GB) - 70% Used in my case (80% for an x86_64)
swap (1024 MB = 1 GB)
/home ( the rest of the disk )

Therefore, for a master disk on IDE 1, the partitions are going to be: /dev/sda1, /dev/sda2, /dev/sda3, and /dev/sda4 (they were called hdax in the past).

Please, think before dimensioning your partitions. Unless you use LVM, it is very complicate to resize them later. Parted was unable to shrink ext3 partitions on Fedora systems (incompatible attribute), for example.


SystemRescueCd and Partimage

System Rescue CD is a live distribution with several tools for repairing file systems. It can be used through a CD-Rom, a pen-drive, or a floppy. In the case of a pendrive, boot from a USB device must be on in the BIOS. Furthermore, in some mobos, it is necessary to access the BIOS with the pendrive plugged in, select the tab Boot -> Hard Disk Drives, and set the pendrive to be the 1st drive. In any case, the pendrive must be made bootable.

During the boot, SystemRescueCd detects the hardware and allows the choice of the keyboard type. If the computer is on a network, it will use DHCP to get an IP. In case of fail, just type net-setup eth0 from the command-line, and configure the network.

For creating partitions, run_qtparted used to start QtParted, a clone of Partition Magic. Newer versions, however, are shipping gparted, instead. In this case, xinit has to be run before calling gparted, because it only works in graphical mode. It is possible then to delete, create, or format partitions. It is never too much to remind people to backup important data before any attempt to modify partitions. There is always the risk of messing the partition table, although, in general, this does not happen.

Partimage can be used for backing up a partition, and its usage is straightforward. Just highlight the partition to be saved, choose a name for its backup, and optionally another machine on the network, running a partimage server, to receive the image. In the server, the file /etc/sysconfig/partimaged should contain options like this:

OPTIONS="--port=4025 --nologin --dest /your_image_dir/images"

The client must always be run as root, and it can optionally use a password for accessing the server. However, the client must have been compiled with "login" enabled, in this case. In fact, the client compilation options, and the server options, must match, e.g., ssl or login. Otherwise, a "version mismatch" error occurs. Should your client use "login", the users allowed to connect to the server must be listed in the file /etc/partimaged/partimagedusers, one per line. If, besides using login, pam support is also enabled, the authentication is done based on a db4 database created by the script /usr/share/partimaged/partimaged-passwd. The database entries are pairs of (user,password). Please, read man partimaged-passwd for getting all the options on how to add/delete users from the database.

For using the ssl capability, the certificates have to be created by running /usr/share/partimaged/create_certificates.sh. The file /etc/partimaged/partimage-certs.cnf can be customized, in order to change information, such as, Country, State and Location. The client supplied with SystemRescueCd is not compiled with ssl or login enabled. However, for security reasons, the partimage package was created with pam and login enabled. Please, note that, for disabling login in the server, it is enough to add the option --nologin to its configuration file, and restart the server. To check which compilation options have been chosen, just type partimaged -i.

Another point is that the client and server architectures must be the same. Therefore, if you use a CD with a 32 bit client, the server must be a 32 bit version too, even if it is running on a 64 bit platform.

The backup files should be named using mnemonics easy to remember, such as, boot-hostname-Fx or root-hostname-Fx. There is also an option to break the image in parts, so they fit on a CD. Generally, 690MB is a good choice, which will produce root-hostname-Fx.000, root-hostname-Fx.001, etc... If you do not want to save the image on another computer, then just mount another partition on the same computer to receive the image.

To restore a backup, just do the reverse, highlight the partition to be restored, and type the name of the first file of the backup: root-hostname-Fx.000.

Remember that the size of the partition to be restored must be greater or equal than the size of the saved partition. If it is greater, then the corresponding file system should be enlarged to occupy all of the available space. For an ext3 file system, use:

e2fsck -f /dev/sdan
resize2fs -f /dev/sdan

The enlargement of the size of the file system can be done at any time later, and it is not mandatory (but the excess is lost, otherwise).


Linux Rescue

After restoring a partition, if it is the / from another computer, it is necessary to edit some configuration files. Any Fedora installation CD or DVD is enough. Boot the computer using it and type linux rescue at the prompt line. Then, choose the language, the keyboard type, and do not activate the network. The first thing to do is to change:

chroot /mnt/sysimage

After that, it is possible to access any file as if it were mounted on an ordinary Fedora system. For instance, vi /etc/fstab. A word of caution: any file can be written this way with root privileges.


Configuration Files

The list of files that might have to be changed correspond to those that would have to be edited in a normal installation. We suppose you know what changes to make in each file. The most important file is fstab, which informs how partitions should be mounted. In case the restored partition is going to be on a different place, the new one has to be known. Therefore, before restoring any partition, save the output of the command more /proc/partitions or df. If the root partition is going to be restored in say, /dev/sda2, it should be listed in /etc/fstab, otherwise it will not be mounted and the system may be unusable, until someone fix fstab. Trial and error can take a long time ... RedHat is using, by default, the UUID of the partition to identify it in fstab. As a consequence, the two lines below are equivalent:

LABEL=/ / ext3 defaults 1 1
/dev/sda2 / ext3 defaults 1 1
UUID=7cb425aa-b871-4304-bcde-253da4c1c68a / ext3 defaults 1 1

Note that UUIDs are cloned. Therefore, it will not correspond to the actual UUID if you kept a partition untouched, such as /home. In this case, you should replace the UUID by the device in fstab (e.g., /dev/sda4), before attempting to boot.

Another important file is /etc/grub.conf, which describes how to boot the system. For grub, the first disk is hd0, the second is hd1, and so on. The partitions are numbered starting from 0. So, if /boot is the first partition of the first disk, there has to be a block like this one:

title Fedora (2.6.26.6-49.fc8)
root (hd0,0)
kernel /vmlinuz-2.6.26.6-49.fc8 ro root=LABEL=/ quiet
initrd /initrd-2.6.26.6-49.fc8.img

On the other hand, if /boot is the second partition, it should be: root (hd0,1). If it is necessary to recreate the MBR, type:

grub-install /dev/sda,

for writing the MBR on the master disk of IDE1. Do not forget to check if /boot/grub/device.map has an entry like this

(hd0) /dev/sda

before trying grub-install.

It is possible to run

mkinitrd /boot/initrd-xxx.img `uname -r` or

dracut -f /boot/initramfs-`uname -r`.img `uname -r`,

for Fedora > 11, to recreate the initial ramdisk, which was created in the post install section of the kernel rpm. Should you get a message like "Unable to access resume device (LABEL=SWAP-sda3)", or your new computer does not a have a floppy and the old one had, recreating initrd will suppress any error message during the boot. Also, remove the file /etc/blkid/blkid.tab before the first boot, so the cached LABELS are not used.

For not having a huge delay in the first boot because of the network, remove the HWADDR entry in /etc/sysconfig/network-scripts/ifcfg-ethx and remove any entry in /etc/udev/rules.d/70-persistent-net.rules

After the first successful boot, the first service to be set is the network. In Gnome, choose the tab System -> Administration -> Network, configure the IP, probe the new card, and change the name of the computer (DNS option). Finally, choose which services to be disabled/enabled.

If you created an ext3 partition sdax using QtParted, and do not want that it is checked by fsck after a certain number of mounts, just turn this feature off:

tune2fs -i 0 -c 0 /dev/sdax

or if you want to change its label to /home:

tune2fs -L /home /dev/sdax

For a vfat file system (e.g., pendrive):

mlabel -i /dev/sdxx ::my_label

Conversely, to force a complete file system check, just create a file called /forcefsck and reboot.

/Paulo Roma.