infra-generale/mount_restore.sh

81 lines
2.0 KiB
Bash
Raw Normal View History

2023-11-21 22:11:55 +01:00
#!/bin/bash
# Script to help restore a virtual machine disk with Borg (plan B)
#
# Copyright (C) 2023 Adrien 'neox' Bourmault <neox@a-lec.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# Exit on error
set -e
# Enable quitting properly
trap clean_exit INT
function clean_exit() {
echo "Exiting..."
umount -q /mnt/${vm}-remote
sync
sleep 3
2023-11-21 23:09:59 +01:00
rmdir /mnt${vm}-remote
2023-11-21 22:11:55 +01:00
}
vm=$1
2023-11-21 23:09:59 +01:00
backuphost=$2
2023-11-21 22:11:55 +01:00
2023-11-21 23:09:59 +01:00
if [[ $# != 2 ]]
2023-11-21 22:11:55 +01:00
then
2023-11-21 23:09:59 +01:00
echo "ERROR: 2 parameters required : vm domain, backup host"
2023-11-21 22:11:55 +01:00
exit 1
fi
# Checking saves
echo -e "Checking backups...\n"
export BORG_PASSPHRASE=$(ssh root@${backuphost} cat /srv/borg/.borg-passphrase)
DATES=$(borg list --short root@${backuphost}:/var/backups/borg/${vm}.repo)
IFS=$'\n' DATES=(${DATES})
echo "* BACKUPS AVAILABLE *"
echo "---------------------"
echo " "
i=0
while [ $i -lt ${#DATES[@]} ]
do
echo -e "\t${i}) ${DATES[i]}"
i=$((i+1))
done
# Asking for save to restore
read -p "Please choose a backup to restore: " backupdate
echo -e "\nOK\n"
# Preparing path
echo "Preparing path..."
mkdir -p /mnt/${vm}-remote
# Mounting
echo "Mounting devices..."
borg mount root@${backuphost}:/var/backups/borg/${vm}.repo::${DATES[backupdate]} /mnt/${vm}-remote
# Syncing
echo "[Please sync now and press enter when finished]"
sleep 1
#rsync -ar --progress --sparse /mnt/${vm}-remote/. /mnt/${vm}
read
# Cleaning up
echo -e "\nOK\n"
clean_exit