2022-05-21 00:50:39 +02:00
|
|
|
#!/bin/bash
|
2023-11-21 11:57:06 +01:00
|
|
|
|
|
|
|
# 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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-05-21 00:50:39 +02:00
|
|
|
vm=$1
|
2023-11-21 11:57:06 +01:00
|
|
|
dest=$2
|
|
|
|
backuphost=$3
|
2022-05-21 00:50:39 +02:00
|
|
|
|
2023-11-21 11:57:06 +01:00
|
|
|
generic_image="/srv/vmverse/installation/generic.a-lec.org.raw"
|
|
|
|
|
|
|
|
if [[ $# != 3 ]]
|
2022-05-21 00:50:39 +02:00
|
|
|
then
|
2023-11-21 11:57:06 +01:00
|
|
|
echo "ERROR: 3 parameters required : vm domain, destination path, backup host"
|
2022-05-21 00:50:39 +02:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2023-11-21 11:57:06 +01:00
|
|
|
# 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 /tmp/mnt/${vm}
|
|
|
|
mkdir -p /tmp/mnt/${vm}-remote
|
|
|
|
mkdir -p $(dirname ${dest})
|
2022-05-21 00:50:39 +02:00
|
|
|
|
2023-11-21 11:57:06 +01:00
|
|
|
# Creating new disk
|
|
|
|
echo "Creating new disk..."
|
|
|
|
cp --reflink ${generic_image} ${dest}
|
2022-05-21 00:50:39 +02:00
|
|
|
|
2023-11-21 11:57:06 +01:00
|
|
|
# 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
|
2022-05-21 00:50:39 +02:00
|
|
|
|
2023-11-21 11:57:06 +01:00
|
|
|
# Syncing
|
|
|
|
echo "Syncing now !"
|
|
|
|
sleep 1
|
|
|
|
rsync -ar --progress --sparse /tmp/mnt/${vm}-remote/. /tmp/mnt/${vm}
|
2022-05-21 00:50:39 +02:00
|
|
|
|
2023-11-21 11:57:06 +01:00
|
|
|
# Cleaning up
|
|
|
|
echo -e "\nOK\n"
|
|
|
|
clean_exit
|