#!/bin/bash #set -e NUM_TO_KEEP=2 NICE_LEVEL='10' TGT="/opt/backups" PATH=/sbin:${PATH} LVMS=$(/sbin/lvs | grep 'wi' | awk {'print $2 ":" $1'} | grep -v orig) echo "$(date) - Starting..." for LVM in ${LVMS}; do VG=$(echo ${LVM} | cut -f1 -d':') LV=$(echo ${LVM} | cut -f2 -d':') case $LV in snap) ;; *) echo "Working with ${VG} ${LV}..." # ensure that the snapshot volume is gone lvremove -f /dev/${VG}/snap > /dev/null 2>&1 # create a new snapshot volume lvcreate -L10G -s -n snap /dev/${VG}/${LV} sleep 2 # perform the actual backup DST="${TGT}/${VG}-${LV}-$(date +%Y%m%d).img.gz" if [ ! -f ${DST}.done ]; then rm -f ${DST} nice -n ${NICE_LEVEL} dd bs=5M if=/dev/${VG}/snap | \ nice -n ${NICE_LEVEL} gzip --rsyncable > ${DST} && \ touch ${DST}.done fi # clean up after ourselves lvremove -f /dev/${VG}/snap > /dev/null 2>&1 sleep 2 if [ -f ${DST}.done ]; then # need to remove old backup instances PATTERN="${VG}-${LV}-[0-9][0-9][0-9][0-9]" PATTERN="${PATTERN}[0-9][0-9][0-9][0-9].img.gz" I=0 for FILE in $(ls -t ${TGT}/${PATTERN}); do if [ "${I}" -lt ${NUM_TO_KEEP} ]; then echo "Keeping ${FILE}" else echo "Removing ${FILE}" rm -f ${FILE} rm -f ${FILE}.done fi I=$((I+1)) done # need to update the hard link folder LINK="${TGT}/link/${VG}-${LV}.img.gz" rm -f ${LINK} ln ${DST} ${LINK} fi ;; esac done echo "$(date) - Finished..."