Backup/Restore Scripts For Imaging/Cloning Disks Using Partimage Under Linux
In a recent post some advantages were discussed in using Partimage over g4u and Symantec Ghost. Following on from that, example scripts and a simple methodology are provided below to backup and restore Linux partitions using partimage run from a client system, to a remote server running partimaged.
Partimage comes installed on Knoppix as well as SystemRescueCD. The installation on Knoppix v4.02 is built to require user authentication and so for this simple example I chose SystemRescueCD. An assumption has been made below that DHCP has been configured such that the client and server systems obtain an IP address when booted with SystemRescueCD.
Preparing the backup repository
- Boot backup server with SystemRescueCD.
- Partition and format an ext3 filesystem on /dev/hda (or as appropriate) which will act as the backup repository:
dd if=/dev/zero of=/dev/hda bs=512 count=1 fdisk /dev/hda << EOF n p 1 w EOF mkfs -t ext3 /dev/hda1
- Create a mount point for the backup partition:
mkdir /image - Mount the backup partition:
mount /dev/hda1 /image - Launch the partimage server application:
partimaged
Operations on the client system
- Prepare a USB key with the desired backup and restore scripts for your particular partition configuration based on the examples below.
- Boot the client system with SystemRescueCD.
- Create a mount point for the USB key:
mkdir /usbkey - Insert and mount the USB key:
mount /dev/uba1 /usbkey - Execute the backup or restore script as desired.
Example scripts
Backup script
#!/bin/sh SERVER=some.ip # Backup master boot record dd if=/dev/hda of=/usbkey/backup.mbr count=1 bs=512 # Backup partition table sfdisk -d /dev/hda > /usbkey/backup.sf # Create Image partimage -z1 -c -d -o -b --server=$SERVER save /dev/hda1 /image/hda1 partimage -z1 -c -d -o -b --server=$SERVER save /dev/hda2 /image/hda2
Restore script
#!/bin/sh SERVER=some.ip # Restore master boot record dd if=/usbkey/backup.mbr of=/dev/hda # Restore partition table sfdisk /dev/hda < /usbkey/backup.sf # Restore Image partimage -b --server=$SERVER restore /dev/hda1 /image/hda1.000 partimage -b --server=$SERVER restore /dev/hda2 /image/hda2.000 # Format swap partition mkswap -c /dev/hda2 # Install Grub boot loader mkdir /disk mount /dev/hda1 /disk grub-install --root-directory=/disk /dev/hda
If you are restoring an image to a disk with different geometry than the source system you will need to comment out the sfdisk line above and create partitions either by hand (or by scripting fdisk). Be aware that, due to the low-level manner in which it operates, partimage requires the desination partition to be at least as large as the source partition, regardless of how much data was on the source partition.
Thank you for the scripts they are very well
But I dont understood where is supposed the emplacement of grub
# Install Grub boot loader
mkdir /disk
mount /dev/hda1 /disk
grub-install –root-directory=/disk /dev/hda
Comment by aymen — October 17, 2006 @ 9:13 am
Excelent scriptes.If you have any scripts related to backups and restores please send it to me.
Comment by Good one — October 20, 2006 @ 7:07 am
Excellent Page. You just saved me many hours of work!
Comment by David Morrisette — January 5, 2007 @ 5:29 pm
partimage can restore the whole mbr including the partition table, but I have not been able to figure out how to script restoring the mbr.
Comment by Mark West — May 1, 2007 @ 11:04 pm