Last updated
Last updated
Veeam supports backup and restore of MySQL databases.
The following options are supported to backup MySQL databases:
HotBackup Database Online Dump.
HotBackup Database Freeze.
ColdBackup Database Shutdown.
There are multiple options available regarding Database Online Dump, one of the option is to use Veeam to dump the database during the backup operations and other option to dump the database to another staging server and protect the staging server from Veeam.
Let’s go through each option one by one in the details:
In this option the pre-freeze script will dump all databases hosted on the guest to a single file under the /tmp directory. Before the VM snapshot creation, the mysql dump native command will dump a copy of the database while service will remain available.
The dump will be deleted by post-thaw script after the guest snapshot has been successful. Pre Freeze Sciprt:
Use Editor
Copy the content in the editor.
Save script as PreFreeze.sh
Post-Thaw Scripts 1. Use Editor 2. Copy the below in the editor:
Save file as PostThaw.sh.
**Online Dump to Staging server
Another option is to dump the MySQL database to staging server and protect staging server from backup job.
Create new server or use any existing server as NFS Share.
Create Script to dump the MySQL database to Staging server
Use Editor
Copy below sample code in the editor:
```
!/bin/bash
Shell script to backup MySQL database
MyUSER="" # DB_USERNAME MyPASS="" # DB_PASSWORD MyHOST="" # DB_HOSTNAME
DEST="" # /home/username/backups/DB
EMAIL=""
DAYS=3
MYSQL="$(which mysql)" MYSQLDUMP="$(which mysqldump)" GZIP="$(which gzip)"
NOW="$(date +"%d-%m-%Y_%s")"
MBD="$DEST/$NOW/mysql" install -d $MBD
SKIP="information_schema another_one_db"
DBS="$($MYSQL -h $MyHOST -u $MyUSER -p$MyPASS -Bse 'show databases')"
for db in $DBS do skipdb=-1 if [ "$SKIP" != "" ]; then for i in $SKIP do [ "$db" == "$i" ] && skipdb=1 || : done fi
done
cd $DEST tar -cf $NOW.tar $NOW $GZIP -9 $NOW.tar
echo "MySQL backup is completed! Backup name is $NOW.tar.gz" | mail -s "MySQL backup" $EMAIL rm -rf $NOW
find $DEST -mtime +$DAYS -exec rm -f {} \;
defaults_file="/etc/my.cnf" timeout=300 lock_file=/tmp/mysql_tables_read_lock
if [ -f $defaults_file ]; then opts="--defaults-file=$defaults_file" fi if [ -n $use_credentials ]; then opts="$opts $use_credentials" fi sleep_time=$((timeout+10)) rm -f $lock_file echo "$0 executing FLUSH TABLES WITH READ LOCK" | logger mysql $opts -e "FLUSH TABLES WITH READ LOCK; system touch $lock_file; system nohup sleep $sleep_time; system echo lock released|logger; " > /dev/null & mysql_pid=$! echo "$0 child pid $mysql_pid" | logger c=0 while [ ! -f $lock_file ] do
if ! ps -p $mysql_pid 1>/dev/null ; then echo "$0 mysql command has failed (bad credentials?)" | logger exit 1 fi sleep 1 c=$((c+1)) if [ $c -gt $timeout ]; then echo "$0 timed out waiting for lock" | logger touch $lock_file kill $mysql_pid fi done echo $mysql_pid > $lock_file exit 0
lock_file=/tmp/mysql_tables_read_lock
mysql_pid=$(cat $lock_file) echo "$0 sending sigterm to $mysql_pid" | logger pkill -9 -P $mysql_pid rm -f $lock_file exit 0
timeout=300 if [ -f /var/run/mysqld/mysqld.pid ] then mysql_pid=$(cat /var/run/mysqld/mysqld.pid) >/dev/null 2>&1 else echo "$0 : Mysql not started or bad mysql pid file location" | logger exit 1 fi echo "$0 : Processing pre-freeze backup script" | logger /etc/init.d/mysqld stop mysql & > /dev/null 2>&1 c=0 while [ true ] do if [ $c -gt $timeout ] then echo "$0 : timed out, mysql shutdown failed" | logger exit 2 fi
if [ -f /var/run/mysqld/mysqld.pid ] then echo "$0 : Waiting 5 more seconds for mysql shutdown" | logger sleep 5 c=$((c+5)) else echo "$0 : Mysql stopped" | logger sync;sync break
fi done
timeout=300 echo "$0 : processing post-thaw backup script" | logger if [ -f /var/run/mysqld/mysqld.pid ] then mysql_pid=$(cat /var/run/mysqld/mysqld.pid) >/dev/null 2>&1 echo "$0 : Mysql already started with PID $mysql_pid" | logger exit 1 fi /etc/init.d/mysqld start mysql & > /dev/null 2>&1 c=0 while [ true ] do if [ $c -gt $timeout ] then echo "$0 : timed out, mysql startup failed" | logger exit 2 fi
if [ -f /var/run/mysqld/mysqld.pid ] then mysql_pid=$(cat /var/run/mysqld/mysqld.pid) >/dev/null 2>&1 echo "$0 : MySQL started with pid $mysql_pid" | logger break else echo "$0 : Waiting 5 more seconds for mysql startup" sleep 5 c=$((c+5)) fi done
``` 3. Save code as Postthaw.sh 4. Configure the backup job to run the script as Post Thaw Script.
The restore is the integrated part of MySQL protection strategy. Veeam provides multiple option of MySQL restores depends on the backup method. Let’s go through the option for each backup method.
For this backup option, Veeam provides following restore options depends on the failure:
In this backup job, Veeam provides following restore options
Tip
In addition to online dump to staging server, take crash-consistency backup of mysql server and in case of server failure restore the mysql server from crach-consistency backup and use database dump from staging server to restore the database.
For this backup option, Veeam provides following restore options depends on the failure:
For this backup option, Veeam provides following restore options depends on the failure:
Use script as in a backup job.
Use script as in the backupjob
For more details about protection and restore use
Failure
Restore Option
Server Failed
Instant Server Restore
Database or Application Level Failure
Guest File Level Restore
Database Item Level Restore
Veeam Universal Application Item Restore
Failure
Restore Option
Database Restore
Instant File Level Restore
Failure
Restore Option
Server Failed
Instant Server Restore
Database or Application Level Failure
Guest File Level Restore
Database Item Level Restore
Veeam Universal Application Item Restore
Failure
Restore Option
Server Failed
Instant Server Restore
Database or Application Level Failure
Guest File Level Restore
Database Item Level Restore
Veeam Universal Application Item Restore