LINUX - LVM Daily Snapshot - Only works with 1 SNAPSHOT

HOW TO MAKE SNAPSHOT SCRIPT
###########################

This script only works when there is one logical volume snapshot!
The cron job is set to run once a day at one minute after midnight!

MAKE THE LVSNAP SCRIPT - (note might need to remove the tabs/extra spaces in if block for this to work - bash can be picky)
######################

#!/bin/bash
DATE1=`date +%D | tr / -`
SNAPNAME=daily-$DATE1
LV1=/dev/this/root
echo Creating Daily Snapshot of $LV1 for today $DATE1
echo Name of new snapshot: $SNAPNAME
echo First Deleting Old Snapshot If there are any...
LVS0=`lvscan`
#echo -e "HERE ARE ALL THE LOGICAL VOLUMES\n${LVS0}\n"
if echo $LVS0 | grep -q "Snapshot"
then
LVS1=`lvscan | grep -i snapshot | cut -f2 -d"'"`
#LVS1=`echo $LVS0 | grep -i snapshot | awk '{print $3;}' | tr \' " "`
echo FOUND!!!! Found this snapshot: $LVS1
echo "====STATS OF SNAPSHOT TO BE DELETED:===="
lvdisplay $LVS1
echo DELETING SNAPSHOT: $LVS1
echo lvremove -f $LVS1
lvremove -f $LVS1
echo "Deleted old snapshot, proceeding to make new snapshot"
else
echo  "Other snapshots not found, proceeding to make new snapshot"
fi
echo CREATING: ... new snapshot ...
echo lvcreate --size 15g --snapshot --name $SNAPNAME $LV1
lvcreate --size 15g --snapshot --name $SNAPNAME $LV1
echo "DONE!"
echo -e "HERE ARE THE NEW LOGICAL VOLUMES\n`lvscan`\n"

MAKE THE WRAPPER SCRIPT (FOR CRON JOB)
######################################

#!/bin/bash
echo "[`date`] RUNNING lvsnap.sh BY KOSTIA" >> /var/log/syslog
date >> /var/log/lvsnap.log
/jobs/lvsnap.sh >> /var/log/lvsnap.log

TYPE crontab -r AS root USER & MAKE THIS ENTRY
##############################################

1 0 * * * /jobs/lvjobrun.sh

Comments