Mai 112012
 
#!/bin/bash
# Nom du lv a backup
lv='var_lib_mysql'
 
# Connect mysql
mysql='/usr/bin/mysql -uroot -p'
 
# Chemin
chem='/mnt/snapshot'
backup='/var/backup'
rapport='/tmp/rapport_backup.txt'
log='/var/log/backup.log'
 
# Size
snapshot_size='30G'
 
# Bin
rsync='/usr/bin/rsync'
cut='/usr/bin/cut'
grep='/bin/grep'
awk='/usr/bin/awk'
lvs='/sbin/lvs'
vgs='/sbin/vgs'
lvdisplay='/sbin/lvdisplay'
lvcreate='/sbin/lvcreate'
lvremove='/sbin/lvremove'
mount='/bin/mount'
umount='/bin/umount'
rm='/bin/rm'
df='/bin/df'
 
# DATE
TODAY=$(date '+%Y.%m.%d')
DELETE=$(date -d now-1days '+%Y.%m.%d')
 
function quit {
	# Send rapport if error
	if [ -s /tmp/rapport_backup.txt ]; then
	        /usr/bin/mail -s "Erreur de backup $DATE" user@test.fr < /tmp/rapport_backup.txt
	        $rm /tmp/rapport_backup.txt
		exit 1
	fi
	$rm /tmp/rapport_backup.txt
	exit 0
}
 
echo "Debut des verifs et suppression des anciens backups : `date +%Y.%m.%d-%Hh%M`" >> $log
 
## Suppression rapport 
if [ -f $rapport ]; then
        $rm $rapport
fi
 
## Verifications paquet 
if ! dpkg -l rsync |grep ^ii >/dev/null ; then 
        apt-get install rsync --force-yes &> /dev/null
        if ! dpkg -l rsync |grep ^ii >/dev/null ; then
                echo "Erreur : Installation paquet rsync !!!!" >> $rapport
		quit
        fi
fi
 
# Verif lv existe
if ! $lvdisplay | $grep 'LV Name' | $cut -d/ -f4 | $grep $lv &> /dev/null ; then
	echo "Erreur : LV $lv n'existe pas" >> $rapport
	quit
fi
 
# Verif si chem existe
if [ ! -d $chem ] ; then
	/bin/mkdir $chem
fi
if [ ! -d $chem ] ; then
	echo "Erreur : Impossible de créer $chem" >> $rapport
	quit
fi
 
# Verif si backup existe
if [ ! -d $backup ] ; then
	/bin/mkdir $backup
fi
if [ ! -d $backup ] ; then
	echo "Erreur : Impossible de créer $backup" >> $rapport
	quit
fi
 
# Verif freespace for snap
VG=$($lvs | $grep $lv | $awk '{print $2}' | uniq )
VG_free=$($vgs | $grep $VG | $awk '{print $7}' | $cut -d'G' -f1 | $cut -d'g' -f1 | $cut -d',' -f1)
if [ $(echo $snapshot_size | $cut -d'G' -f1 | $cut -d'g' -f1 | $cut -d',' -f1) -ge $VG_free ] ; then
	echo "Erreur : Pas assez de place pour créer le snaphot" >> $rapport
	quit
fi
 
# Verif si snap existe
if $lvs | $grep $VG | $grep lvmsnap &> /dev/null ; then
	echo "Erreur : LV lvmsnap existe" >> $rapport
	quit
fi
 
# Verif si chem déjà monté
if $mount | $grep $chem &> /dev/null ; then
	echo "Erreur : $chem déjà monté">> $rapport
        quit
fi
 
# Delete old backup
if [ -d $backup/$DELETE ] ; then
	$rm -r $backup/$DELETE
fi
if [ -d $backup/$DELETE ] ; then
	echo "Erreur : Impossible de supprimer $backup/$DELETE" >> $rapport
	quit
fi
 
# Chemin lv mapper
LVmapper=$(echo $lv | sed 's/-/--/' )
 
# Compare freespace backup > usedspace source (if source mount)
if $mount | $grep $LVmapper &> /dev/null ; then
	LVmount=$($mount | $grep $LVmapper | $awk '{print $3}')
	if [ $($df -P $backup | $grep '/' | $awk '{print $4}') -lt $($df -P $LVmount | $grep '/' | $awk '{print $3}') ] ; then
		echo "Erreur : Espace restant sur la destination du backup non suffisant" >> $rapport
		quit
	fi
fi
 
 
# Backup
	## Log time 
        echo "Debut du backup : `date +%Y.%m.%d-%Hh%M`" >> $log
 
	# Chemin dev lv
	LVS=$($lvdisplay | $grep 'LV Name' | $grep $lv | $awk '{print $3}')
 
	# Stop replication
	$mysql -e"stop slave;"
	if ! $mysql -e"show slave status\G" | $grep "Slave_SQL_Running" | $grep "No" &> /dev/null ; then
		echo "Erreur : La réplication ne s'arrête pas avant le snaphost" >> $rapport
                quit
        fi
	# Create snap
	$lvcreate -L$snapshot_size -s -n lvmsnap $LVS
 
	# Start replication
	$mysql -e"start slave;"
	if ! $mysql -e"show slave status\G" | $grep "Slave_SQL_Running" | $grep "Yes" &> /dev/null ; then
                echo "Waring : La réplication ne démarre pas après le snapshot" >> $rapport
        fi
	SNAP=$($lvdisplay | $grep 'LV Name' | $grep lvmsnap | $awk '{print $3}')
	if [ ! $SNAP ] ; then
		echo "Erreur : Impossible de créer le snaphost"  >> $rapport
		quit
	fi
 
	$mount $SNAP $chem
 
	# Copy
	$rsync -a $chem/ $backup/$TODAY/ >> $rapport
 
	$umount $chem
 
	# Check snapshot used
	if [ $($lvs | $grep lvmsnap | $awk '{print $6}') = 100.00 ] ; then
		echo "Erreur : Snapshot plein à la fin du backup" >> $rapport
		$rm -rf $backup/$TODAY
		$lvremove -f $SNAP
		quit
	fi
	if [ $($df $backup/$TODAY | $grep '/' | $awk '{print $3}') -le 0 ] ; then
		echo "Erreur : Destination du backup plein après la copie" >> $rapport
                $rm -rf $backup/$TODAY
		$lvremove -f $SNAP
		quit
        fi
	$lvremove -f $SNAP
 
	## Log time 
        echo "Fin du backup : `date +%Y.%m.%d-%Hh%M`" >> $log
 
quit

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre lang="" line="" escaped="" cssfile="">

(required)

(required)

Spam protection by WP Captcha-Free