Hi Everyone,
Almost two months back I enquired about doing point-in-time backups
(snapshots) of my server and was pointed to some different products.
Here is what I finished up with in the end. It allows me to quiesce
live databases, take disk snapshots (they take less than a second),
un-quiesce databases (only tested with Postgresql so far) and then using
backup software copy the snapshot to another drive (in my case it is a
removable usb drive for now) for a consistent point-in-time backup.
I used LVM to take the snapshots, rdiff-backup to perform the backup and
restores, and some scripting to allow restore to new hard drive.
I'm not sure if it would restore to completely different hardware as I'm
not sure where linux has the hardware dependencies? - I'll have to test
that some time.
The LVM physical volume and volume group ("hrnzlx02") are around twice
the size of the LVM logical volumes in use, so there is plenty of room
for the snapshot volumes.
I had to label my partitions and use these labels in /etc/fstab instead
of the uuid's just to remove another layer of editing after the restore
- means I don't have to alter fstab after restore
The only manual editing I have to perform after restore is putting the
new UUID for the boot partition into the grub 'menu.lst' file.
This has been a great learning exercise for me (with a steep curve), had
many a frustrating dead-end found. Once I have a better grasp of bash
scripting I hope to generalize this to use parameters so I could run it
on any server. Also, once we have a central backup location that
understands linux file systems I'll change the rdiff-backup to go across
the network.
Anyway, in case I might have missed something important, or there is an
easier way to do something, here are my scripts. If you see anything
that may catch me out in the future when trying to restore please let me
know... like, for example, are my exclusions from backup all
I need or excessive?
Since you guys have a great depth of linux understanding I appreciate
any comments or ideas you may have that help this backup scheme.
BACKUP SCRIPT:
########################################################################
####
#!/bin/bash
# save partition table locally and on removable drive:
sfdisk -dx /dev/sda > /home/adminstrator/scripts/sda-pt.sf
cp /home/adminstrator/scripts/sda-pt.sf /media/usbdrive/restore_info
# save master boot record locally and on removable drive:
dd if=/dev/sda of=/home/adminstrator/scripts/mbr-backup bs=512 count=1
cp /home/adminstrator/scripts/mbr-backup
/media/usbdrive/restore_info/mbr-backup
#### Next will perform a 'snapshot' backup of root drive to usbdrive.
#
# insert databases quiesce/block commands here ....
#
# create the snapshot...
lvcreate -L8G -s -n rootsnapshot /dev/hrnzlx02/root
#
# insert databases un-quiesce/un-block commands here ....
#
# mount the snapshot for backing up from:
mkdir -p /mnt/hrnzlx02/rootsnapshot
mount /dev/hrnzlx02/rootsnapshot /mnt/hrnzlx02/rootsnapshot
# do the backup to removable drive
rdiff-backup -b -v2 --print-statistics --exclude-sockets --exclude
/mnt/hrnzlx02/rootsnapshot/tmp --exclude
/mnt/hrnzlx02/rootsnapshot/media --exclude
/mnt/hrnzlx02/rootsnapshot/proc --exclude /mnt/hrnzlx02/rootsnapshot/mnt
/mnt/hrnzlx02/rootsnapshot /media/usbdrive/backup
# unmount the snapshot
umount /mnt/hrnzlx02/rootsnapshot
# remove the snapshot logical partition
lvremove /dev/hrnzlx02/rootsnapshot -f
# backup boot partition (ubuntu so far has always created it as
/dev/sda5)
rdiff-backup -b -v2 --print-statistics --exclude-sockets /boot
/media/usbdrive/backup_boot_sda5
#end
########################################################################
####
RESTORE SCRIPT - using Ubuntu desktop live cd to boot machine...
Assume running from directory where script is on the removable drive,
where I also have the
Necessary rdiff-backup package files.
########################################################################
####
#!/bin/bash
# THIS IS FOR A RESTORE TO A NEW DISK.
echo NOTE: Must have root permissions to work - run 'sudo su' first.
echo Any key to continue
read -n 1
# load required software:
apt-get update
apt-get install lvm2
dpkg -i /media/disk/restore_info/librsync1_0.9.7-5_i386.deb
dpkg -i /media/disk/restore_info/rdiff-backup_1.2.7-1ubuntu2_i386.deb
echo New drive must be clean - no partitions nor LVM volumes, etc!!
echo Create partition table? using sfdisk
echo Any key to continue
read -n 1
sfdisk -fx /dev/sda < sda-pt.sf
echo creating the physical volume on /dev/sda1
pvcreate -ff /dev/sda1
echo creating virtual group hrnzlx02
vgcreate hrnzlx02 /dev/sda1
echo creating logical volumes
lvcreate -L10.24G -n root hrnzlx02
lvcreate -L1G -n swap hrnzlx02
echo formatting and mounting new volumes to use for restore...
mkfs.ext3 /dev/hrnzlx02/root
mkfs.ext2 /dev/sda5
mkswap -v1 /dev/hrnzlx02/swap
mkdir /media/hd
mkdir /media/boot
mount -t ext3 /dev/hrnzlx02/root /media/hd
mount -t ext2 /dev/sda5 /media/boot
## restore data to drive:
echo Restoring boot partition
rdiff-backup -v4 --force --restore-as-of now
/media/disk/backup_boot_sda5 /media/boot
echo Restoring root partition
rdiff-backup -v4 --force --restore-as-of now /media/disk/backup
/media/hd
echo Creating directories that were excluded from original backup:
mkdir /media/hd/proc
mkdir /media/hd/tmp
mkdir /media/hd/mnt
mkdir /media/hd/media
mkdir /media/hd/media/usbdrive
mkdir /media/hd/media/cdrom
mkdir /media/hd/media/cdrom0
mkdir /media/hd/media/floppy
mkdir /media/hd/media/floppy0
echo Restoring master boot record
dd if=/media/disk/restore_info/mbr-backup of=/dev/sda bs=446 count=1
echo Redo volume label for boot partition - in case I lost it?
e2label /dev/sda5 BootPart
echo NOTE - you now have to modify new /boot/grub/menu.lst to reflect
new UUID !!!! - use 'blkid' to get numbers
echo You are changing the uuid values to point to the boot partitions
uuid - /dev/sda5 in our case
# end.
########################################################################
####
Thanks and Regards,
Bryce Stenberg
IT Department
Harness Racing New Zealand Inc.
DISCLAIMER: If you have received this email in error, please notify us
immediately by reply email, facsimile or collect telephone call to +64 3
9641200 and destroy the original. Please refer to full DISCLAIMER at
http://www.hrnz.co.nz/eDisclaimer.htm