From 811d2b0dd4eba78d475f09999befbba4678e361d Mon Sep 17 00:00:00 2001 From: admin666 Date: Tue, 21 Nov 2023 11:57:06 +0100 Subject: [PATCH] New plan B restore script --- old_restore.sh | 19 +++++++++++++ restore.sh | 77 ++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 87 insertions(+), 9 deletions(-) create mode 100755 old_restore.sh diff --git a/old_restore.sh b/old_restore.sh new file mode 100755 index 0000000..d075cd2 --- /dev/null +++ b/old_restore.sh @@ -0,0 +1,19 @@ +#!/bin/bash +vm=$1 +date=$2 +disk=$3 +destpath=$4 + +if [[ $# != 4 ]] +then + echo "ERROR: 4 parameters required (vm, date, disk, destination)" + exit 1 +fi + +backup_file=$(sudo virt-backup list -D ${vm} | grep ${date} | cut -d " " -f 2 | sed "s/.json/_${disk}.raw/g") + +echo "Restoring $backup_file" +sudo cp --sparse=always -v "$backup_file" "$destpath" + + + diff --git a/restore.sh b/restore.sh index d075cd2..d74ae4a 100755 --- a/restore.sh +++ b/restore.sh @@ -1,19 +1,78 @@ #!/bin/bash -vm=$1 -date=$2 -disk=$3 -destpath=$4 -if [[ $# != 4 ]] +# Exit on error +set -e + +# Enable quitting properly +trap clean_exit INT + +function clean_exit() { + echo "Exiting..." + sync + umount -q /tmp/mnt/${vm} + umount -q /tmp/mnt/${vm}-remote + + if [ ! -z ${loopdisk} ]; then + losetup -d ${loopdisk} + fi + rm -r /tmp/mnt +} + + +vm=$1 +dest=$2 +backuphost=$3 + +generic_image="/srv/vmverse/installation/generic.a-lec.org.raw" + +if [[ $# != 3 ]] then - echo "ERROR: 4 parameters required (vm, date, disk, destination)" + echo "ERROR: 3 parameters required : vm domain, destination path, backup host" exit 1 fi -backup_file=$(sudo virt-backup list -D ${vm} | grep ${date} | cut -d " " -f 2 | sed "s/.json/_${disk}.raw/g") +# 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 "Restoring $backup_file" -sudo cp --sparse=always -v "$backup_file" "$destpath" +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 /tmp/mnt/${vm} +mkdir -p /tmp/mnt/${vm}-remote +mkdir -p $(dirname ${dest}) + +# Creating new disk +echo "Creating new disk..." +cp --reflink ${generic_image} ${dest} + +# Mounting +echo "Mounting devices..." +loopdisk=$(losetup -fPL ${dest} --show) +mount ${loopdisk}p1 /tmp/mnt/${vm} +borg mount root@${backuphost}:/var/backups/borg/${vm}.repo::${DATES[backupdate]} /tmp/mnt/${vm}-remote + +# Syncing +echo "Syncing now !" +sleep 1 +rsync -ar --progress --sparse /tmp/mnt/${vm}-remote/. /tmp/mnt/${vm} + +# Cleaning up +echo -e "\nOK\n" +clean_exit