Files
myscripts/rsync_backup_lv.sh
2016-06-09 00:41:48 +02:00

100 lines
2.6 KiB
Bash

#!/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${reset}"
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${reset}"
fi
}
echo "${green}=== start backup for ${yellow}$OLVPATH${reset}"
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.${reset}"
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.${reset}"
fi
CMD=("sudo" "mount" "$SPATH" "$MLVPATH")
if ! "${CMD[@]}"; then
echo "${red}Error(3): mount ${yellow}$SPATH${red} to ${yellow}$MLVPATH${red} failed${reset}"
removeMountDir $blue "Cleanup"
removeSnapshot $blue "Cleanup"
exit 1
else
echo "${green}Succesfull(3): mount ${yellow}$SPATH${green} to ${yellow}$MLVPATH${reset}"
fi
echo "${green}--- start rsync-backup from ${yellow}$MLVPATH${green} to ${yellow}$ZPATH${reset}"
CMD=("sudo" "rsync" "-Pavxh" "--delete" "--progress" "-e" "ssh" "$MLVPATH" "$ZPATH")
if ! "${CMD[@]}"; then
echo "${red}Error(4): rsync-backup aborted${pink}"
unMountDir $blue "Cleanup"
removeMountDir $blue "Cleanup"
removeSnapshot $blue "Cleanup"
exit 1
else
echo "${green}Succesfull(4): rdiff-backup finished${reset}"
fi
unMountDir $green "5"
removeMountDir $green "6"
removeSnapshot $green "7"