Code :
## INFOS MYSQL ##
LOGIN=root # Login for mysql
PASS=toto # Pass for mysql
## SSH SERVER FOR TRANSFERT & REINJECTION ##
MYSQLSRV2="192.168.0.1"
## Email ##
ADMINMAIL=user@fqdn.com # This is the mail were the transferts failures will be notified
## Vars ##
TMPDIR=/tmp/baksql # Temp directory
## DO NOT MODIFY NOW ##
mkdir $TMPDIR
# Backuping databases
for databases in "bugzilla" "cacti" "mysql" "networkdb" "wiki" ; do
mysqldump -u$LOGIN -p$PASS $databases > $TMPDIR/$databases-'date +%y%m%d'.sql && baksql=$baksql'echo "Sauvegarde de la base $databases - OK ; "' || baksql=$baksql'echo "Sauvegarde de la base $databases - FAILED ; "'
done
# Transfering databases to the other SQL server (need ssh key)
tar -czf $TMPDIR/mysql_backup.tgz $TMPDIR/*.sql
scp $TMPDIR/mysql_backup.tgz $MYSQLSRV2:~/
ssh $MYSQLSRV2 tar -xzvf ~/mysql_backup.tgz
for databases in "bugzilla" "cacti" "mysql" "networkdb" "wiki" ; do
ssh $MYSQLSRV2 "mysql -u$LOGIN -p$PASS $databases < ~/tmp/baksql/$databases-'date +%y%m%d'.sql" && baksql=$baksql'echo "Reinjection de la base $databases - OK ; "' || baksql=$baksql'echo "Reinjection de la base $databases - FAILED ; "'
done
# Delete temps
ssh $MYSQLSRV2 rm -Rf /root/tmp/baksql ~/mysql_backup.tgz ~/tmp
rm -Rf $TMPDIR
# Send mail
echo $baksql | mail -s "Mysql transferts" $ADMINMAIL