From a334bb675f87dfbc9d3d4a6f3c3c7c2a5e017b49 Mon Sep 17 00:00:00 2001 From: admin666 Date: Thu, 30 May 2024 13:26:28 +0000 Subject: [PATCH] nouveaux outils de restauration --- complete_restore.sh | 100 ++++++++++++++++++++++++++++++++++++++ migrate_many_to_aunt.sh | 6 +-- migrate_many_to_mother.sh | 5 +- migrate_to_aunt.sh | 2 +- reboot_all.sh | 25 ++++++++++ restore.sh | 10 +--- shutdown_all.sh | 25 ++++++++++ 7 files changed, 154 insertions(+), 19 deletions(-) create mode 100755 complete_restore.sh create mode 100755 reboot_all.sh create mode 100755 shutdown_all.sh diff --git a/complete_restore.sh b/complete_restore.sh new file mode 100755 index 0000000..6b82469 --- /dev/null +++ b/complete_restore.sh @@ -0,0 +1,100 @@ +#!/bin/bash +# Script to restore a virtual machine disk with Borg (plan B) +# +# Copyright (C) 2023 Adrien 'neox' Bourmault +# +# 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 . + +# Exit on error +set -e + +# Enable quitting properly +trap clean_exit INT + +function clean_exit() { + echo "Exiting..." + set +e + umount -q /mnt/${vm} + umount -q /mnt/${vm}-remote + sync + + if [ ! -z ${loopdisk} ]; then + losetup -d ${loopdisk} + fi + sleep 3 + rmdir /mnt/${vm}* + exit 0 +} + + +vm=$1 +dest=$2 +backuphost=$3 +rsync_options=$4 + +generic_image="/srv/vmverse/installation/generic.a-lec.org.raw" + +if [[ $# < 3 ]] +then + echo "ERROR: 3 parameters required : vm domain, destination path, backup host" + 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} +mkdir -p /mnt/${vm}-remote +mkdir -p $(dirname ${dest}) + +# Creating new disk if necessary +if [ ! -e ${dest} ]; then + echo "Creating new disk..." + cp --reflink ${generic_image} ${dest} +fi + +# Mounting +echo "Mounting devices..." +loopdisk=$(losetup -fPL ${dest} --show) +mount ${loopdisk}p1 /mnt/${vm} +borg mount root@${backuphost}:/var/backups/borg/${vm}.repo::${DATES[backupdate]} /mnt/${vm}-remote + +# Syncing +echo "Syncing now !" +sleep 1 +rsync -ar --info=progress2 --progress --no-i-r --sparse ${rsync_options} /mnt/${vm}-remote/. /mnt/${vm} + +# Cleaning up +echo -e "\nOK\n" +clean_exit diff --git a/migrate_many_to_aunt.sh b/migrate_many_to_aunt.sh index 7e4c2a9..34395ce 100755 --- a/migrate_many_to_aunt.sh +++ b/migrate_many_to_aunt.sh @@ -28,9 +28,5 @@ do fi echo MIGRATING $vm ... - sudo virsh migrate --verbose --live --persistent --undefinesource --abort-on-error $vm qemu+ssh://192.169.254.3:223/system - if [ $? == 0 ]; then - sudo mv /var/backups/vm/mother/$vm /var/backups/vm/aunt - fi - + sudo virsh migrate --verbose --live --persistent --undefinesource --abort-on-error $vm qemu+ssh://aunt.lan:223/system done diff --git a/migrate_many_to_mother.sh b/migrate_many_to_mother.sh index ae38340..1dfff11 100755 --- a/migrate_many_to_mother.sh +++ b/migrate_many_to_mother.sh @@ -28,8 +28,5 @@ do fi echo MIGRATING $vm ... - sudo virsh migrate --verbose --live --persistent --undefinesource --abort-on-error $vm qemu+ssh://192.169.254.2:222/system - if [ $? == 0 ]; then - sudo mv /var/backups/vm/aunt/$vm /var/backups/vm/mother - fi + sudo virsh migrate --verbose --live --persistent --undefinesource --abort-on-error $vm qemu+ssh://mother.lan:222/system done diff --git a/migrate_to_aunt.sh b/migrate_to_aunt.sh index 3291b77..3e367d3 100755 --- a/migrate_to_aunt.sh +++ b/migrate_to_aunt.sh @@ -26,7 +26,7 @@ then fi echo MIGRATING $vm ... -sudo virsh migrate --verbose --live --persistent --undefinesource --abort-on-error $vm qemu+ssh://192.169.254.3:223/system +sudo virsh migrate --verbose --live --persistent --undefinesource --abort-on-error $vm qemu+ssh://aunt.lan:223/system if [ $? == 0 ]; then sudo mv /var/backups/vm/mother/$vm /var/backups/vm/aunt fi diff --git a/reboot_all.sh b/reboot_all.sh new file mode 100755 index 0000000..6b56eec --- /dev/null +++ b/reboot_all.sh @@ -0,0 +1,25 @@ +#!/bin/bash +# Script to start every virtual machine on a given host +# +# Copyright (C) 2023 Adrien 'neox' Bourmault +# +# 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 . + +VMs=$(sudo virsh list --all --name) + +for vm in $VMs +do + echo Starting $vm ... + sudo virsh reboot $vm +done diff --git a/restore.sh b/restore.sh index f3938ac..cec7f7c 100755 --- a/restore.sh +++ b/restore.sh @@ -43,8 +43,6 @@ dest=$2 backuphost=$3 rsync_options=$4 -generic_image="/srv/vmverse/installation/generic.a-lec.org.raw" - if [[ $# < 3 ]] then echo "ERROR: 3 parameters required : vm domain, destination path, backup host" @@ -78,12 +76,6 @@ mkdir -p /mnt/${vm} mkdir -p /mnt/${vm}-remote mkdir -p $(dirname ${dest}) -# Creating new disk if necessary -if [ ! -e ${dest} ]; then - echo "Creating new disk..." - cp --reflink ${generic_image} ${dest} -fi - # Mounting echo "Mounting devices..." loopdisk=$(losetup -fPL ${dest} --show) @@ -93,7 +85,7 @@ borg mount root@${backuphost}:/var/backups/borg/${vm}.repo::${DATES[backupdate]} # Syncing echo "Syncing now !" sleep 1 -rsync -ar --info=progress2 --no-i-r --sparse ${rsync_options} /mnt/${vm}-remote/. /mnt/${vm} +rsync -ar --info=progress2 --progress --no-i-r --sparse --delete --exclude={"/bin","/sbin","/proc","/dev","/sys","/run","/tmp","/mnt","/media","/lost+found"} ${rsync_options} /mnt/${vm}-remote/. /mnt/${vm} # Cleaning up echo -e "\nOK\n" diff --git a/shutdown_all.sh b/shutdown_all.sh new file mode 100755 index 0000000..7c4c530 --- /dev/null +++ b/shutdown_all.sh @@ -0,0 +1,25 @@ +#!/bin/bash +# Script to start every virtual machine on a given host +# +# Copyright (C) 2023 Adrien 'neox' Bourmault +# +# 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 . + +VMs=$(sudo virsh list --all --name) + +for vm in $VMs +do + echo Starting $vm ... + sudo virsh shutdown $vm +done