Sunday, May 4, 2008

LINUX partition backup

Option 1: Backup with partimage

First install the partimage application:
$ apt-get install partimage

Switch to runlevel 1 with this command:
$ init 1

umount the partition to be backupped through umount, check the disk integrity with a badblocks control on it:
$ e2fsck -cv /dev/hdxx
-c [Starts badblocks in order to look for and sign the corrupted blocks]
-v [verbose mode]
additional options:
-n [Executes the check in read only mode, replying with n (no) to every possible question]
-p [Repares automatically the filesystem without asking]
-y [Answers automatically with y (yes) to every possible question, to allow the non-interactive usage via script]

$ partimage -z1 save /dev/hdxx /path/img/debian_sid.partimg.gz

The above command executes directly partimage from the command line using a GZ compression.

To eventually restore of the partition just type:
$ partimage restore /dev/hdc7 /path/img/debian_sid.partimg

Option 2: Backup with tar

Again, switch to runlevel 1 with:
$ init 1

Don't umount the partition, but create a system image with:
$ tar -cvpf /path/of_the/file/img.tar --exclude=/mnt --exclude=/proc --exclude=/sys /

Note: don't forget the final / !

In order to reduce the image's size you can add the options for filtering data with bzip2 to tar. This allows you to save space, but the file creation will last 3/4 times more than without compression. If you wish to use the bzip2 compression type this:
$ tar -cvjpf /path/of_the/file/img.tar.bz2 --exclude=/mnt --exclude=/proc --exclude=/sys -exclude=/path/of_the/file/img.tar.bz2 /

According to the data contained in your root partition (and to your processor's speed) the time can vary. The author of this tutorial spent 15/20 minutes with 9 GB without using the compression.

To eventually restore, mount the target partition, move into it with cd, and uncompress the img file with this command:
$ tar -xvf img.tar

If during its creation you compressed it with bzip2 the new command will be:
$ tar -xvjf immagine.tar.bz2

Note: in these examples the root partition is used, and from it some directories are excluded which can be useless or dangerous to include. These directories can be re-created with mkdir after restoring the backup.

Note: if during the backup you changed the partition number or the hard disk (for example from hda98 to hdf3), /etc/fstab and the bootloader configuration need to be updated.

No comments: