Skip to content

Instantly share code, notes, and snippets.

@paulte
Last active November 14, 2018 21:39
Show Gist options
  • Select an option

  • Save paulte/8a94d082924fe4e25eb192c8094a2946 to your computer and use it in GitHub Desktop.

Select an option

Save paulte/8a94d082924fe4e25eb192c8094a2946 to your computer and use it in GitHub Desktop.
lvm snapshot backups
#!/bin/env bash
#
# Restoring all snaps with PRESAMBA
# lvs -v | egrep PRESAMBA |awk '{print $1}' > /tmp/findme \
# && find /dev/centos | egrep -f /tmp/findme \
# | xargs -n 1 lvconvert --merge
# Delaying merge since origin is open.
# Merging of snapshot centos/PRESAMBA___centos___home___day___20181106230701___1541545621 will occur on next activation of centos/home.
# Delaying merge since origin is open.
# Merging of snapshot centos/PRESAMBA___centos___root___day___20181106222001___1541542801 will occur on next activation of centos/root.
# reboot
export PATH=/bin:/sbin
set -e
SNAPPREFIX="mybackup"
THISSECOND=$(date +%s)
THISDATE=$(date +'%Y%m%d%H%M%S')
DONTSNAP="centos:swap centos:data"
SNAPSIZE="3GB"
function prunecorruptsnaps {
echo "Checking for corrupt snapshots"
lvs --separator : --noheadings --options vg_name,lv_name,lv_attr \
| sed -e 's/^[ ]*//g' \
| egrep ':....I.....$' \
| awk -F: '{printf "/dev/%s/%s\n",$1,$2}' \
| xargs -n 1 lvremove -f
}
function createsnaps {
lvs --separator : --noheadings --options vg_name,lv_name,lv_attr \
| sed -e 's/^[ ]*//g' > /tmp/newlvs
awk -F: '{if ( $3 ~ /.w..ao....$/) {print $2,$1}}' < /tmp/newlvs | while read VOL GROUP
do
echo ${DONTSNAP} | fmt -1 | egrep -q "^${GROUP}:${VOL}$" && continue || true
echo "Checking snapshots for ${GROUP}/${VOL}"
for OPTIONS in \
day:86400:7 \
week:345600:4 \
month:2678400:3
do
TIMETAG=$(echo ${OPTIONS} | cut -f1 -d:)
GAPINSECONDS=$(echo ${OPTIONS} | cut -f2 -d:)
HOWMANYTOKEEP=$(echo ${OPTIONS} | cut -f3 -d:)
SNAPCMD="lvcreate -L ${SNAPSIZE} -s -n ${SNAPPREFIX}___${GROUP}___${VOL}___${TIMETAG}___${THISDATE}___${THISSECOND} /dev/${GROUP}/${VOL}"
SNAPSTOREMOVE=$(egrep "^${GROUP}:*${SNAPPREFIX}___${GROUP}___${VOL}___${TIMETAG}___" /tmp/newlvs \
| sort -n | head -n -${HOWMANYTOKEEP} \
| awk -F: '{print $2}')
for OLD in ${SNAPSTOREMOVE}
do
lvremove -f /dev/${GROUP}/${OLD}
done
OLDSNAPS=$(egrep "^${GROUP}:*${SNAPPREFIX}___${GROUP}___${VOL}___${TIMETAG}___" /tmp/newlvs | awk -F: '{print $2}')
if [ "X${OLDSNAPS}" != "X" ]; then
MOSTRECENT=$(echo ${OLDSNAPS} | fmt -1 | sed -e 's/.*___//g' | sort -n | tail -1)
HOWLONGAGO=$(expr ${THISSECOND} - ${MOSTRECENT})
if [ ${HOWLONGAGO} -gt ${GAPINSECONDS} ]; then
${SNAPCMD}
fi
else
${SNAPCMD}
fi
done
done
rm /tmp/newlvs
}
prunecorruptsnaps
createsnaps
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment