My Opera is closing 3rd of March

Cronache di Sarvegia

...because every new challenge hides an opportunity

A fast cold backup with LVM snapshots

, ,

Some time ago, the backup system of our LUG's server broke down. Some time was spent to repair it, but with no success. It was time to build a new solution. Now, our servers happens to use LVM, so why not take advantage of it to speed up the process and reduce the downtime of our mysql database?

So, the checklist was the following:

  • do some preparations, like dumping the package list and the OpenLDAP contents;
  • stop mysql, take a snapshot of the data directory, start mysql again, snap the other volume
  • mount the snapshot under the /snap directory: that will be a cold copy of the system
  • mount the archives directory under /snap, as well
  • run backup2l, chrooted in /snap
  • when finished, unmount everything under /snap and destroy the snapshot
  • now the backup archives are ready to be copied to a remote location, e.g.: using rsync

Guess that, I used a Makefile:

MOUNTPOINT_BASE=/snap
SERVICE=/usr/sbin/service
BACKUP2L=/usr/sbin/backup2l

.PHONY:	all preparations snapshot mount backup clean

all: preparations snapshot mount backup clean

test: preparations snapshot mount clean


preparations:
	dpkg --get-selections | \
		diff - /usr/local/etc/dpkg-selections.log > /dev/null || \
		dpkg --get-selections > /usr/local/etc/dpkg-selections.log
	date
	$(SERVICE) slapd stop
	slapcat -l /var/backups/slapd.ldif
	$(SERVICE) slapd start
	date

snapshot:
	date
	$(SERVICE) mysql stop
	lvcreate --size 1G --snapshot --name varlv_snap /dev/sysvg/varlv
	$(SERVICE) mysql start
	date
	lvcreate --size 1G --snapshot --name rootlv_snap /dev/sysvg/rootlv

mount:
	mount /dev/sysvg/rootlv_snap $(MOUNTPOINT_BASE)
	mount /dev/sysvg/varlv_snap  $(MOUNTPOINT_BASE)/var
	mount /dev/sysvg/backups     $(MOUNTPOINT_BASE)/backups

backup:
	chroot /snap $(BACKUP2L) -b

clean:
	-umount $(MOUNTPOINT_BASE)/backups
	-umount /dev/sysvg/varlv_snap
	-umount /dev/sysvg/rootlv_snap
	-lvremove -f /dev/sysvg/rootlv_snap
	sleep 1
	-lvremove -f /dev/sysvg/varlv_snap

Very little to say here: all is the default target, and it will execute preparations, snapshot, mount, backup, and clean. .PHONY is a pseudo-target, and it hints make to run the targets in any case (e.g., run the target "mount" even if there is an up-to-date file named mount in the directory).

It's often convenient to use make for automation. It saves you from inserting break points in case of errors from the commands (make normally stops if a command exits with a return code other than 0), and it's very easy to implement locking (e.g., by touch-ing a file named after the target when you start, and removing it when you are done). And it's even fun! Try it!

Grazie!La povertà