Last active
November 26, 2018 13:45
-
-
Save chadyred/1ddf9c2d942ae86d14841450a858a367 to your computer and use it in GitHub Desktop.
Backup - restore KVM VM with disk and image and XML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Liste des domaines | |
# | |
#----------------------------------------------------------------- | |
if [ "$(groups $USER | grep staff)" == "" ] || [ "$(groups $USER | grep sudo)" == "" ]; then | |
echo "Vous ne faites pas partie du staff ou vous n'êtes pas sudoer !" | |
exit 1 | |
fi | |
# | |
# Repertoire par defaut | |
# | |
BACKUPDIR=PATH/TO_FOLDER_BACKUP_KVM | |
# Liste des machines virtuelles (domain KVM) | |
sudo virsh list --all | |
echo -n "Machine / domaine : " | |
read DOM | |
if [ -z $DOM ] ; then | |
echo "Pas de machine saisie..." | |
exit | |
fi | |
echo -n "Backup directory : [$BACKUPDIR] " | |
read BACKDIR | |
if [ -z $BACKDIR ] ; then | |
BACKDIR=$BACKUPDIR | |
fi | |
FILE_PATH=${BACKDIR}/${DOM} | |
mkdir -p $FILE_PATH | |
if [ ! -d $BACKDIR ] ; then | |
echo "Repertoire $BACKDIR inexistant ! " | |
exit 1 | |
fi | |
# Export de la configuration | |
touch ${FILE_PATH}/${DOM}.xml | |
sudo chown nobody:staff ${FILE_PATH}/${DOM}.xml && sudo chmod 775 ${FILE_PATH}/${DOM}.xml | |
sudo virsh dumpxml $DOM > ${FILE_PATH}/${DOM}.xml | |
if [ "$?" != "à" ] ; then | |
echo "Erreur dans l'export XML" | |
exit 1 | |
fi | |
# Recherche des partitions ou fichiers de la VM | |
IMGFILE=$(grep 'source dev' ${FILE_PATH}/${DOM}.xml | cut -d "'" -f2) | |
# premier test avec source dev | |
if [ "$IMGFILE" = "" ] ; then | |
echo "Test avec source file" | |
IMGFILE=$(grep 'source file' ${FILE_PATH}/${DOM}.xml | cut -d "'" -f2) | |
if [ "$IMGFILE" = "" ] ; then | |
echo "Impossible de trouver l'image de la VM" | |
exit | |
fi | |
fi | |
for rawfile in $IMGFILE ; do | |
echo | |
# VMSIZE=$(virsh vol-info $rawfile|grep Allocation|cut -c '15-25') | |
VMSIZE=$(du -b "$rawfile" | cut -f1) | |
VMSIZE_HUMAN=$(ls -lah $rawfile | awk '{print $5}') | |
echo -n "Espace occupé (en bytes) par $rawfile : $VMSIZE ( $VMSIZE_HUMAN )" | |
echo | |
DFSIZE=$(df -B1 ${BACKDIR} --output=avail |tail -1) | |
DFSIZE_HUMAN=$(df -h ${BACKDIR} --output=avail |tail -1) | |
echo "Espace dispo (en bytes) sur $BACKDIR : $DFSIZE ( $DFSIZE_HUMAN ) " | |
if [ $VMSIZE -gt $DFSIZE ] ; then | |
echo "Il n'y a pas assez de place disponible sur le répertoire cible $BACKDIR" | |
exit | |
fi | |
echo -n "Sauvegarde de $rawfile ? (o/n)" | |
read rep | |
if [ "$rep" = "o" ] ; then | |
filename=$(basename $rawfile) | |
if [ -f ${FILE_PATH}/${filename} ] ; then | |
echo -n "Le fichier ${FILE_PATH}/${filename} existe déjà, le remplacer ? (o/n)" | |
read replace | |
if [ "$replace" = "o" ] ; then | |
sudo rm ${FILE_PATH}/${filename} | |
else | |
exit 1 | |
fi | |
fi | |
echo dd if=${rawfile} of=${FILE_PATH}/${filename} bs=512K | |
sudo dd if=${rawfile} of=${FILE_PATH}/${filename} bs=512K | |
fi | |
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Liste des domaines | |
# | |
#----------------------------------------------------------------- | |
showLoadingAllSutdown() { | |
loadingText=$1 | |
echo -ne "$loadingText\r" | |
while : ; do | |
[[ -z "$(sudo virsh list --state-running --name | tr -d '\n' | tr -d ' ' | tr -d '\0')" ]] && break | |
echo -ne "$loadingText.\r" | |
sleep 0.5 | |
echo -ne "$loadingText..\r" | |
sleep 0.5 | |
echo -ne "$loadingText...\r" | |
sleep 0.5 | |
echo -ne "\r\033[K" | |
echo -ne "$loadingText\r" | |
sleep 0.5 | |
done | |
echo "$loadingText...FINISHED" | |
} | |
if [ "$(groups $USER | grep staff)" == "" ] || [ "$(groups $USER | grep sudo)" == "" ]; then | |
echo "Vous ne faites pas partie du staff ou vous n'êtes pas sudoer !" | |
exit 1 | |
fi | |
# | |
# Repertoire par defaut | |
# | |
BACKUPDIR==PATH/TO_FOLDER_BACKUP_KVM | |
# Liste des machines virtuelles (domain KVM) | |
sudo virsh list --all | |
echo -n "Machine / domaine à restaurer : " | |
read DOM | |
if [ -z $DOM ] ; then | |
echo "Pas de machine saisie..." | |
exit | |
fi | |
echo -n "Backup directory : [$BACKUPDIR] " | |
read BACKDIR | |
if [ -z $BACKDIR ] ; then | |
BACKDIR=$BACKUPDIR | |
fi | |
FILE_PATH=${BACKDIR}/${DOM} | |
if [ ! -d $FILE_PATH ] ; then | |
echo "Backup inexistant ! " | |
exit 1 | |
fi | |
if [ ! -d $BACKDIR ] ; then | |
echo "Repertoire $BACKDIR inexistant ! " | |
exit 1 | |
fi | |
# Export de la configuration | |
# Le fichier de définition se trouve dans /etc/libvirt/qemu/. Il est au format xml, avec le nom de la machine. Par exemple : | |
# /etc/libvirt/qemu/Ubuntu-LXD.xml décrit la configuration de la machine Ubuntu-LXD. | |
# Shutdown have data which is not corrupt | |
for i in $(sudo virsh list --name --state-running); do sudo virsh shutdown $i; done | |
showLoadingAllSutdown "Wait for all machine shutting down" | |
echo "Restauration de ${DOM} ? (o/n)" | |
read rep | |
if [ "$rep" = "o" ] ; then | |
sudo virsh define ${FILE_PATH}/${DOM}.xml | |
# Recherche des partitions ou fichiers de la VM | |
IMGFILE=$(grep 'source dev' ${FILE_PATH}/${DOM}.xml | cut -d "'" -f2) | |
# premier test avec source dev | |
if [ "$IMGFILE" = "" ] ; then | |
echo "Test avec source file" | |
IMGFILE=$(grep 'source file' ${FILE_PATH}/${DOM}.xml | cut -d "'" -f2) | |
if [ "$IMGFILE" = "" ] ; then | |
echo "Impossible de trouver l'image de la VM" | |
exit | |
fi | |
fi | |
for rawfile in $IMGFILE ; do | |
echo "Restauration de ${rawfile}" | |
filename=$(basename $rawfile) | |
# Same name but elsewere | |
echo dd if=${FILE_PATH}/${filename} of=${rawfile} bs=512K | |
sudo dd if=${FILE_PATH}/${filename} of=${rawfile} bs=512K | |
done | |
fi | |
# Start all machine backuped (--all to show not started VM) | |
for i in $(sudo virsh list --name --all); do sudo virsh start $i; done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment