From 010000e5cd8aa0c2b76421db5b14c5c7920cd344 Mon Sep 17 00:00:00 2001 From: stubbfel Date: Thu, 9 Jun 2016 00:41:48 +0200 Subject: [PATCH] update 20160609 --- complete_backup_to_remote.sh | 37 ++++++++++++++ mount_encrypt.sh | 28 ++++++++++ rsync_backup_lv.sh | 99 ++++++++++++++++++++++++++++++++++++ umount_encrypt.sh | 27 ++++++++++ 4 files changed, 191 insertions(+) create mode 100644 complete_backup_to_remote.sh create mode 100644 mount_encrypt.sh create mode 100644 rsync_backup_lv.sh create mode 100644 umount_encrypt.sh diff --git a/complete_backup_to_remote.sh b/complete_backup_to_remote.sh new file mode 100644 index 0000000..6cff637 --- /dev/null +++ b/complete_backup_to_remote.sh @@ -0,0 +1,37 @@ +#!/bin/bash +red=`tput setaf 1` +green=`tput setaf 2` +yellow=`tput setaf 3` +reset=`tput sgr0` + +echo "${green}### start complete backup${reset}" + +CMD=("ssh" "-t" "root.router" "./mount_backup.sh") +if ! "${CMD[@]}"; then + ssh root.router "./umount_backup.sh" + echo "${red}Error(1): remote mount failed${reset}" + exit 1 +else + echo "${green}Succesfull(1): remote mount success.${reset}" +fi + +echo "${green}### start backup for ${yellow}system root${reset}" +sudo sh rsync_backup_lv.sh system root root.router:/mnt/backup +echo "${green}### finished backup for ${yellow}system home${reset}" + +echo "${green}### start backup for ${yellow}system home${reset}" +sudo sh rsync_backup_lv.sh system home root.router:/mnt/backup +echo "${green}### finished backup for ${yellow}system home${reset}" + +echo "${green}### start backup for ${yellow}tank playonlinux${reset}" +sudo sh rsync_backup_lv.sh tank playonlinux root.router:/mnt/backup +echo "${green}### finished backup for ${yellow}tank playonlinux${reset}" + +echo "${green}### start backup for ${yellow}tank steamgames${reset}" +sudo sh rsync_backup_lv.sh tank steamgames root.router:/mnt/backup +echo "${green}### finished backup for ${yellow}tank steamgames${reset}" + +echo "${green}### unmount remote dir${reset}" +ssh root.router "./umount_backup.sh" + +echo "${green}### finished complete backup${reset}" diff --git a/mount_encrypt.sh b/mount_encrypt.sh new file mode 100644 index 0000000..22537a5 --- /dev/null +++ b/mount_encrypt.sh @@ -0,0 +1,28 @@ +#!/bin/bash +Device=$1 +MountPoint=$2 +MapName=$3 +ContainerName="${MapName}foo" + +red=`tput setaf 1` +green=`tput setaf 2` +reset=`tput sgr0` + +CMD=("cryptsetup" "luksOpen" "$Device" "$ContainerName") +if ! "${CMD[@]}"; then + echo "${red} can't open Device $Device ${reset}" + exit 1 +fi +sleep 3 +vgchange -ay +sleep 3 + +CMD=("mount" "/dev/mapper/${MapName}" "$MountPoint") +if ! "${CMD[@]}"; then + lvchange -a n "/dev/mapper/${MapName}" + cryptsetup luksClose "$ContainerName" + echo "${red}mount of $Device to $MountPoint failed ${reset}" + exit 1 +fi + +echo "${green}success mount $Device to $MountPoint ${reset}" diff --git a/rsync_backup_lv.sh b/rsync_backup_lv.sh new file mode 100644 index 0000000..18cdee3 --- /dev/null +++ b/rsync_backup_lv.sh @@ -0,0 +1,99 @@ +#!/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" diff --git a/umount_encrypt.sh b/umount_encrypt.sh new file mode 100644 index 0000000..877734c --- /dev/null +++ b/umount_encrypt.sh @@ -0,0 +1,27 @@ +#!/bin/bash +MapName=$1 +ContainerName="${MapName}foo" + +red=`tput setaf 1` +green=`tput setaf 2` +reset=`tput sgr0` + +CMD=("umount" "/dev/mapper/${MapName}") +if ! "${CMD[@]}"; then + echo "${red}can't umount ${MapName}${reset}" + exit 1 +fi + +CMD=("lvchange" "-a" "n" "/dev/mapper/${MapName}") +if ! "${CMD[@]}"; then + echo "${red}can't remove lv ${MapName}${reset}" + exit 1 +fi + +CMD=("cryptsetup" "luksClose" "$ContainerName") +if ! "${CMD[@]}"; then + echo "${red}can't close luks ${ContainerName}${reset}" + exit 1 +fi + +echo "${green}success umount ${MapName} ${reset}"