#!/bin/bash VG=$1 LV=$2 BPATH=$3 SNAME="${VG}-${LV}-snapshot" OLVPATH="/dev/${VG}/${LV}" MLVPATH="/mnt/${SNAME}" SPATH="/dev/${VG}/${SNAME}" ZPATH="${BPATH}/${SNAME}" red=`tput setaf 1` blue=`tput setaf 4` green=`tput setaf 2` yellow=`tput setaf 3` pink=`tput setaf 5` reset=`tput sgr0` function removeSnapshot { COLOR="$1" STEP="$2" CMD="sudo lvremove -f $SPATH" if ! $CMD; then echo "${red}Error(${STEP}): removed snapshot - ${yellow}$SPATH${red} failed${reset}" exit 1 else echo "${COLOR}Succesfull(${STEP}): removed snapshot - ${yellow}$SPATH${reset}" fi } function removeMountDir { COLOR="$1" STEP="$2" CMD=" sudo rmdir $MLVPATH" if ! $CMD; then echo "${red}Error(${STEP}): removed mount folder - ${yellow}$SPATH${red} failed${reset}" exit 1 else echo "${COLOR}Succesfull(${STEP}): removed mount folder - ${yellow}$MLVPATH${pink}" fi } function unMountDir { COLOR="$1" STEP="$2" CMD=" sudo umount $MLVPATH" if ! $CMD; then echo "${red}Error(${STEP}): unmounted - ${yellow}$MLVPATH${red} failed${reset}" exit 1 else echo "${COLOR}Succesfull(${STEP}): unmounted - ${yellow}$MLVPATH${pink}" fi } echo "${green}=== start backup for ${yellow}$OLVPATH${pink}" CMD=" sudo lvcreate -L10G -s -n $SNAME $OLVPATH" if ! $CMD; then echo "${red}Error(1): snapshot ${yellow}$SNAME${green} failed${reset}" exit 1 else echo "${green}Succesfull(1): snapshot ${yellow}$SNAME${green} created.${pink}" fi CMD=" sudo mkdir -p $MLVPATH" if ! $CMD; then echo "${red}Error(2): snapshot mount folder ${yellow}$MLVPATH${green} failed${pink}" removeSnapshot $blue "Cleanup" exit 1 else echo "${green}Succesfull(2): snapshot mount folder ${yellow}$MLVPATH${green} created.${pink}" fi CMD=" sudo mount $SPATH $MLVPATH" if ! $CMD; then echo "${red}Error(3): mount ${yellow}$SPATH${red} to ${yellow}$MLVPATH${red} failed${pink}" removeMountDir $blue "Cleanup" removeSnapshot $blue "Cleanup" exit 1 else echo "${green}Succesfull(3): mount ${yellow}$SPATH${green} to ${yellow}$MLVPATH${pink}" fi echo "${green}--- start rdiff-backup from ${yellow}$MLVPATH${green} to ${yellow}$ZPATH${pink}" CMD=" sudo rdiff $MLVPATH $ZPATH" if ! $CMD; then echo "${red}Error(4): rdiff-backup aborted${pink}" unMountDir $blue "Cleanup" removeMountDir $blue "Cleanup" removeSnapshot $blue "Cleanup" exit 1 else echo "${green}Succesfull(4): rdiff-backup finished${pink}" fi unMountDir $green "5" removeMountDir $green "6" removeSnapshot $green "7"