MySQL
MySQL
Veeam supports backup and restore of MySQL databases.
Backup Options:
The following options are supported to backup MySQL databases:
HotBackup Database Online Dump.
HotBackup Database Freeze.
ColdBackup Database Shutdown.
HotBackup Database Online Dump:
There are multiple options available regarding Database Online Dump, one of the option is to use Veeam Pre & Post Thaw Scripts 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:
Database Online Dump During Backup Operations:
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
Use script as pre-freeze script in a backup job.
Post-Thaw Scripts 1. Use Editor 2. Copy the below in the editor:
Save file as PostThaw.sh.
Use script as Post-Thaw script in the backupjob
**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
Set these variables
MyUSER="" # DB_USERNAME MyPASS="" # DB_PASSWORD MyHOST="" # DB_HOSTNAME
Backup Dest directory
DEST="" # /home/username/backups/DB
Email for notifications
EMAIL=""
How many days old files must be to be removed
DAYS=3
Linux bin paths
MYSQL="$(which mysql)" MYSQLDUMP="$(which mysqldump)" GZIP="$(which gzip)"
Get date in dd-mm-yyyy format
NOW="$(date +"%d-%m-%Y_%s")"
Create Backup sub-directories
MBD="$DEST/$NOW/mysql" install -d $MBD
DB skip list
SKIP="information_schema another_one_db"
Get all databases
DBS="$($MYSQL -h $MyHOST -u $MyUSER -p$MyPASS -Bse 'show databases')"
Archive database dumps
for db in $DBS do skipdb=-1 if [ "$SKIP" != "" ]; then for i in $SKIP do [ "$db" == "$i" ] && skipdb=1 || : done fi
done
Archive the directory, send mail and cleanup
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
Remove old files
find $DEST -mtime +$DAYS -exec rm -f {} \;
!/bin/bash
config:
when running on debian we can use existing debian-sys-maint account using defaults file
otherwise, specify username and password below using use_credentials
use_credentials="-uroot -p"
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
check if mysql is running
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
!/bin/bash
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
!/bin/bash
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
check if mysql is running
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
!/bin/bash
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
check if mysql is running
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.
Restore:
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.
Database Online Dump During Backup Operations:
For this backup option, Veeam provides following restore options depends on the failure:
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 |
Online Dump to Staging server
In this backup job, Veeam provides following restore options
Failure | Restore Option |
Database Restore | Instant File Level Restore |
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.
HotBackup Database Freeze.
For this backup option, Veeam provides following restore options depends on the failure:
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 |
ColdBackup Database Shutdown.
For this backup option, Veeam provides following restore options depends on the failure:
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 |
For more details about protection and restore use MySQL Protection Whitepaper
Last updated