IT & + si affinités

Encore un blog de sysadmin … mais pas uniquement ;-)

Backup de VM Qemu/KVM

Ce script effectue une sauvegarde des VM (Configuration et disque), attention, les VMs sont killées si elles ne répondent pas à la commande d’arrêt dans le temps imparti. 

Note : il existe de meilleurs moyens de faire des sauvegarde, par exemple avec des snapshots si votre format de stockage le permet.

#!/bin/bash
# Name : backup_vm.sh
# Author : @Markhor75 
# Last Mod : 2014 10 20
# Backup VM (disk and config)

DESTINATION="/home/backup/vm"
DATE="$(date +"%Y%m%d_%H%M")"
RETENTIONDAYS="2"	# Days to keep backups 
KILLTIME=60		# Seconds to wait after excute shutdown VM and before destroying it
THISVMDEST=""
DOM=""
debug=1

if [ $debug -eq 1 ]; then
	echo "****************************************"
	echo "* Backup destination is "$DESTINATION
	echo "* Date is "$DATE
	echo "* Retention is "$RETENTIONDAYS" days"
	echo "* Killtime is "$KILLTIME"s"
	echo "* Debug is active"
	echo "****************************************"
fi	

for DOM in `virsh list --all | tail -n +3 | awk '{print $2}' | sort -u`
do 
        if [ $debug -eq 1 ]; then
		echo "*** Starting Backup $DOM ***"
	fi

	STATE=`virsh domstate $DOM`
	THISVMDEST=$DESTINATION"/"$DOM

	if [ ! -d $THISVMDEST ]; then
		if [ $debug -eq 1 ]; then 
			echo " Creating dir $THISVMDEST"
		fi
		mkdir $THISVMDEST
	fi

	virsh dumpxml $DOM >$THISVMDEST"/"$DATE"_"$DOM".xml"

	if [ "$STATE" == "running" ]; then
		if [ $debug -eq 1 ]; then
			echo " VM state is running, shutting down"
		fi
		virsh shutdown $DOM
		sleeping=0
		while [ $sleeping -lt $KILLTIME ]
		do
			if [ "`virsh domstate $DOM`" != "shut off" ]; then
				sleep 5
                        	sleeping=$(( $sleeping + 5))
			else
				sleeping=0
				break
			fi
		done

		#Destoying domain if not stopped after 60 seconds
		if [ "$sleeping" != "0" ]; then
                	if [ $debug -eq 1 ]; then
                        	echo " VM always running after $KILLTIME s, Destoying it"
                	fi
			virsh destroy $DOM
		fi
		
	fi
	
	for DISK in `virsh dumpxml $DOM |grep "source file" |cut -d "'" -f 2`
	do
		DISKNAME=`echo $DISK |awk -F"/" '{print $NF}'`
		if [ $debug -eq 1 ]; then
			echo " making cp $DISK $THISVMDEST\"/\"$DATE\"_\"$DISKNAME"
		fi
		#cp $DISK $THISVMDEST"/"$DATE"_"$DISKNAME
	done

        if [ "$STATE" == "running" ]; then
		if [ $debug -eq 1 ]; then
			echo " VM restarting"
		fi
		virsh start $DOM
	fi
        if [ $debug -eq 1 ]; then
                echo "*** Ending backup $DOM ***"
        fi
done

if [ $debug -eq 1 ]; then
	echo "Deleting old backups"
fi

find $DESTINATION -maxdepth 1 -ctime +$RETENTIONDAYS -type f -exec rm {} \;

if [ $debug -eq 1 ]; then
        echo "Script ending"
	echo""
fi

 

Backup mysql databases

Un script de backup des bases mysql simple :

il est prévu pour fonctionner sur debian.

#!/bin/bash
# Name : backup_mysql.sh
# Author : @Markhor75 
# Last Mod : 2014 08 27
# Backup all MySQL databases

DESTINATION="/home/backup/mysql"
RETENTIONDAYS="2"

MYSQLUSER=`cat /etc/mysql/debian.cnf |grep -m 1 user |awk '{print $3}'`
MYSQLPASS=`cat /etc/mysql/debian.cnf |grep -m 1 password |awk '{print $3}'`
DATE="$(date +"%Y%m%d_%H%M")"

mysqldump --user=$MYSQLUSER --password=$MYSQLPASS --events --all-databases |bzip2 >$DESTINATION"/"$DATE""_mysql_""$HOSTNAME""_db.bz2
find $DESTINATION -maxdepth 1 -ctime +$RETENTIONDAYS -type f -exec rm {} \;