Bonjour à tous :)

Ce sujet n’a rien à voir avec Kelio à proprement parlé.
Toutefois certains le savent, nous souhaitons migrer de la solution de virtualisation « OpenVZ » à « VMware ESXi ».

Pour l’entreprise où Vinux et moi mêmes sommes employés, j’ai eu à écrire un script de sauvegarde de VM à chaud pour économiser le cout d’une licence « Sauvegarde VCB » du logiciel Bakbone NetVault.
Aussi, j’ai écris un script en BASH permettant de sauvegarder et compresser les VM avant de les envoyer dans un dossier, le tout sans interruption de service !

L’idée de base étant d’installer un agent NetVault sur le « service console » de ESX qui se chargera de sauvegarder le tout.
Toutefois, il est tout à fait possible de faire un montage NFS, CIFS dans un dossier ; Et donc de sauvegarder les données sur un autre serveur.

Commencez par créer /opt/backup-vm.sh

#!/bin/bash
 
#Copyright (C) 2010 Mercier Benjamin
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 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 General Public License for more details.
#You should have received a copy of the GNU General Public License along
#with this program; if not, write to the Free Software Foundation, Inc.,
#51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
SOURCE=/vmfs/volumes/ISCSI-datastore
DESTINATION=/vmfs/volumes/ISCSI-datastore/backups
DATE=`date "+%F"`
SERVEUR=`hostname`
 
echo ""
echo "Script de backup des VM a chaud"
echo "Par Benjamin Mercier <bmercier@teclib.com>"
echo "Celui-ci fait une copie à chaud dans un dossier des VMs"
echo "Serveur : $SERVEUR"
echo "Datastore : $SOURCE"
echo "Datastore de backup : $DESTINATION"
echo
 
echo "Serveur ESX primaire. Suppression des VMs du répertoire de déstination" > /tmp/backup-vm.log
rm -Rfv $DESTINATION/* 2>&1 >> /tmp/backup-vm.log
echo "Suppression terminée" >> /tmp/backup-vm.log
echo "" >> /tmp/backup-vm.log
 
for MACHINE in $(ls $SOURCE)
do
        if [[ "$MACHINE" =~ 'srv-' ]]; then
                echo  -n "Backup de la machine $MACHINE "
                TEST_REGISTERED=$(vmware-cmd -l | grep $MACHINE | wc -l)
                if [ -e $SOURCE/$MACHINE/$MACHINE.vmx ] && [[ $TEST_REGISTERED -eq 1 ]]; then
                        echo "" >> /tmp/backup-vm.log
                        echo "Machine virtuelle : $MACHINE" >> /tmp/backup-vm.log
 
                        echo -n '. '
                        vmware-cmd $SOURCE/$MACHINE/$MACHINE.vmx createsnapshot "BackupScript" "Snapshot de backup." 1 1 2>&1 >> /tmp/backup-vm.log
                        echo -n '. '
 
                        echo -n '. '
                        tar czvf $DESTINATION/$MACHINE.tar.gz $SOURCE/$MACHINE --exclude '*.vswap*' --exclude '*.vmsn*' 2>&1 >> /tmp/backup-vm.log
                        echo -n '. '
 
                        echo -n '. '
                        vmware-cmd $SOURCE/$MACHINE/$MACHINE.vmx removesnapshots 2>&1 >> /tmp/backup-vm.log
                        echo -n '. '
 
                        echo '. [terminé]'
                else
                        echo '. [ESX non propriétaire de cette VM]'
                fi
        fi
done

Vous prendrez soin de renseigner changer vos chemins ; Et vous pourrez aussi ajouter un contrôle des points de montage si vous effectuez la sauvegarde sur un serveur distant.
Sachez aussi que la sauvegarde ne sera effective que pour les VMs gérés par le serveur. Si vous avez trois serveurs ESX, vous devrez avoir un agent par serveur.

Editez le fichier /etc/crontab :

vim /etc/crontab
 
# Rendez executable le script :
chmod +x /opt/backup-vm.sh
 
# Ajouter à la fin :
01 4 * * 2 root /opt/backup-vm.sh 2>1 > /dev/null
 
# Redémarrez Cron
/etc/init.d/cron restart

Cela pourra peut être rendre service à certains.

Étiquettes: , , , , , ,