DailyLVMSnapshot

From Mike Neir's Wiki

Jump to: navigation, search

Premise

The idea behind this script was to set up an easy way to back up Xen LVM partitions that don't have a public-facing IP address. The script is designed to make LVM snapshots of all the LVM partitions holding Xen instances, so that I could rsync them via domain0, which has a public IP. It first checks for snapshots to unmount and remove based upon a known filename pattern. Then it will attempt to create new snapshots and mount them, filtering out things that don't need to be backed up, such as swap partitions.

Script

#!/bin/bash
VOLUMEGROUPS="/dev/LVM-hdc"
TODAY=`date +%Y%m%d`
BACKUPLOC="/backups"

for VG in $VOLUMEGROUPS;
do      
       echo "Removing old backup volumes in $VG..."
       cd $VG
       for LV in `ls -1 | egrep "BACKUP-[0-9]{8}" | grep -v $TODAY`;
       do      
               echo "  Backup Volume: $VG/$LV"
               echo "    Unmounting..."
               /bin/umount $VG/$LV
               echo "    Removing...";
               /usr/sbin/lvremove -f $VG/$LV
       done
       echo "done."
       echo 
       echo "Creating and mounting new backup volumes..."

       cd $VG
       for LV in `ls -1 | egrep -v "BACKUP-[0-9]{8}" | grep -iv swap `;
       do      
               echo "  $VG/$LV"
               if [ -e "$VG/${LV}-BACKUP-$TODAY" ];
               then    
                       echo "    Backup volume already exists."
               else    
                       echo "    Creating backup volume for $VG/$LV"
                       SIZE=`/usr/sbin/lvdisplay $VG/$LV -c | cut -d : -f 7`
                       echo "      Size is ${SIZE}k"
                       /usr/sbin/lvcreate -L${SIZE}k -s -n${LV}-BACKUP-$TODAY $VG/$LV
               fi
               echo "    Mounting backup volume"
               mkdir -p $BACKUPLOC/$VG/$LV
               /bin/mount $VG/${LV}-BACKUP-$TODAY $BACKUPLOC/$VG/$LV
       done
       echo 
done

Caveats

This script worked really well for about a week. Then it failed, for reasons I'm still unsure of, and totally locked up two LVM partitions when the script was trying to create the new snapshots. Since then, I've stopped using the script.

Personal tools