Friday, November 27, 2009

Time Machine on Linux with LVM

Mac's Time Machine is a good thing. You can accomplish that behavior on windows with shadow copy (as far as I've heard), about what about Linux?

You can do it with LVM volumes with a small bash magic!:)

I do this way from cron or by hand if I need an instant snapshot.

#!/bin/bash

VG=vg1
LV=root
MOUNTPOINT=/lvm-backup
KEEP=4
SIZE=100M

DATE=$(date +%F-%H-%M-%S)

sync
lvcreate --size $SIZE --snapshot --name $LV-$DATE /dev/$VG/$LV
mkdir -p $MOUNTPOINT/$LV-$DATE
mount /dev/$VG/$LV-$DATE $MOUNTPOINT/$LV-$DATE

sync
COUNT=$(lvdisplay $VG\/$LV|grep -v Name|grep $VG\/$LV|wc -l)
while [ $COUNT -gt $KEEP ]
do
TOREMOVE=$(lvdisplay $VG\/$LV|grep -v Name|grep $VG\/$LV|sort|head -n 1|cut -d'/' -f4|cut -d' ' -f1)
umount $MOUNTPOINT/$TOREMOVE
rmdir $MOUNTPOINT/$TOREMOVE
lvremove -f $VG/$TOREMOVE
sync
COUNT=$(lvdisplay $VG\/$LV|grep -v Name|grep $VG\/$LV|wc -l)
done

No comments: