Consistent Protection Mysql Mariadb
Consistent Protection Mysql Mariadb
of MySQL/MariaDB
with Veeam
Pascal Di Marco
Solutions Architect
© 2018 Veeam Software. Confidential information. All rights reserved. All trademarks are the property of their respective owners.
Consistent protection of MySQL/MariaDB with Veeam
Contents
Executive summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Target audience. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Job configuration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Sample scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Recovery. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Guest recovery. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
© 2018 Veeam Software. Confidential information. All rights reserved. All trademarks are the property of their respective owners. 1
Consistent protection of MySQL/MariaDB with Veeam
Executive summary
To answer the Availability challenge, modern tools should not only consider safely transferring data blocs from one container
to another . Application consistency is also a key point that allows for the best possible combination of safety and Recovery
Time Objectives (RTOs) to keep business-critical operations online.
This guide will outline how to achieve application consistency of MySQL and MariaDB databases with Veeam® Backup &
Replication™, version 8 to version 9 .5.
Target audience
Making today’s virtual datacenter available requires more and more competencies beyond the backup tools themselves.
Using modern and efficient software such as Veeam Backup & Replication necessitates a minimum amount of knowledge about
the protected applications to ensure the best possible protection level. This publication is especially intended to be read by
a technical audience, such as backup administrators or systems engineers.
Introduction
As an agentless solution, Veeam Backup and Replication can be used to enforce application consistency, either via Microsoft
Volume Shadow Copy Service (VSS) integration, or integration with third-party tools.
When these mechanisms are unavailable, either because the application is running on a Linux guest, or because of a non-
integrated application, Veeam Backup & Replication, since version 8, offers the ability to leverage pre-freeze and post-thaw
scripting, effectively allowing consistent backup or replication for any application.
© 2018 Veeam Software. Confidential information. All rights reserved. All trademarks are the property of their respective owners. 2
Consistent protection of MySQL/MariaDB with Veeam
To protect a MySQL or MariaDB instance, which typically run on a Linux guest, a shell script is required, usually prefixed as . sh.
These can be located anywhere on the Veeam Backup & Replication server.
© 2018 Veeam Software. Confidential information. All rights reserved. All trademarks are the property of their respective owners. 3
Consistent protection of MySQL/MariaDB with Veeam
Scripts are stored on the Veeam Backup & Replication server and uploaded to the guest using SSH port 22.
The Guest Interaction Proxy, as a Windows-only guest feature, will not be used for Linux guests.
© 2018 Veeam Software. Confidential information. All rights reserved. All trademarks are the property of their respective owners. 4
Consistent protection of MySQL/MariaDB with Veeam
If the network connection between the Veeam Backup & Replication server and the Linux guest is unavailable, the script upload
process and guest login will failover to the VMware VIX communication channel. Because both mechanisms require that
VMware Tools be up and running on the guest, it is suggested the Veeam ONE™ VM Configuration Assessment report be used
to detect whether VMware Tools are or are not installed the guest.
Depending of which method will be used to upload the scripts, the guest directory used may differ. If SSH is used, scripts
will be temporarily stored as /tmp/<UID>_scriptname. If VMware VIX is used, scripts will then be temporarily stored in
/tmp/vmware-root/scriptname.
© 2018 Veeam Software. Confidential information. All rights reserved. All trademarks are the property of their respective owners. 5
Consistent protection of MySQL/MariaDB with Veeam
Create and edit the user account. As a best practice, it is recommended to use the description field and clearly indicate
the target guest for future management easiness.
© 2018 Veeam Software. Confidential information. All rights reserved. All trademarks are the property of their respective owners. 6
Consistent protection of MySQL/MariaDB with Veeam
For security reasons, when Veeam Backup & Replication backs up its own configuration for disaster recovery purposes into
a BCO configuration file, passwords are not saved by default. This can be modified by enabling configuration backup file
encryption: Open the main menu, select the Configuration Backup dialog and select the encrypt configuration backup option.
The Veeam Documentation Center can be consulted for more on Veeam configuration backup here: https://helpcenter.veeam.
com/backup/vsphere/vbr_config.html.
Job configuration
Veeam Backup & Replication gives you the ability to execute pre- and post-scripts on a per-guest basis. This means that the guests
running the databases can be part of any backup or replication job.
In the job creation Wizard, during the Guest Processing step, select the Enable application-aware image processing option.
© 2018 Veeam Software. Confidential information. All rights reserved. All trademarks are the property of their respective owners. 7
Consistent protection of MySQL/MariaDB with Veeam
Then, click on the Applications button to customize the application-aware image processing options. Select each MySQL and
MariaDB server, which will use the scripts created and click Edit.
Depending on the job configuration, it might optionally be necessary to manually select which guests are eligible for application
aware processing.
For example, with a job configured to back up a whole datastore, the default behavior will be to execute pre-freeze and post-thaw
shell scripts on all Linux guests processed by the job, regardless of whether they are hosting the concerned application or not.
To avoid such a situation, it is possible to manually select which guests to apply the application-aware processing tasks to, while also
making script selection granular for each and every guest.
In the Application Aware Processing Option window, click on the Add button, and select each necessary guest. It might probably
be required to select Show full hierarchy beside the container-type selection area.
© 2018 Veeam Software. Confidential information. All rights reserved. All trademarks are the property of their respective owners. 8
Consistent protection of MySQL/MariaDB with Veeam
Once the Processing settings window is open, select the Scripts tab and browse for the pre-freeze and post-thaw in question.
Select Bash shell script files (*.sh) under the file types you wish to show.
© 2018 Veeam Software. Confidential information. All rights reserved. All trademarks are the property of their respective owners. 9
Consistent protection of MySQL/MariaDB with Veeam
Sample scripts
MySQL and MariaDB can be backed up in many ways. The following three methods, however, are most commonly used.
Two of the following methods will use application commands to dump database contents or freeze writes:
1. Hot Backup — Database Dump
2. Hot Backup — Database Freeze
These commands must be executed by users with RELOAD privileges on the databases. In the following example, the user “user”
with password “Password” has been granted proper privileges on the MySQL database.
The third option does not require MySQL user permissions, but does require permission to start and stop application services
through init.d or systemctl:
3. Cold Backup — Database Shutdown
There are two authentication options available for the provided scripts:
Option 1: Hardcode the username and password in the script: -u user -p Password
Option 2: Use the MySQL default configuration file to grant users access to MySQL commands.
Usually the preferred method is to use the configuration file, because keeping the password within the same Linux installation
is likely to be more secure. Depending on the MySQL install, the default file might be /etc/my.cnf or /etc/mysql/debian.cnf,
or it may not even exist.
If the default file does not exist, it can be created based on default templates typically residing in /usr/share/mysql/my-*.cnf.
In this configuration file, specify the user with required privileges on the database as follows:
vi /etc/my.cnf
[client]
user = user
password = Password
port = 3306
socket = /var/lib/mysql/mysql.sock
Depending on the option chosen, uncomment and modify use_credentials or default_file values in the pre-freeze scripts below.
© 2018 Veeam Software. Confidential information. All rights reserved. All trademarks are the property of their respective owners. 10
Consistent protection of MySQL/MariaDB with Veeam
The dump will be deleted by post-thaw script after the guest snapshot has been successful.
Advantages
The database will stay online and read-write, while a separate and independent file will be generated.
Disadvantages
This method is a bit more complex to set up than orchestrating a database shutdown.
The process of dumping the databases might be time consuming and affect backup windows. It also requires extra space.
To avoid over-filling issues such as a full root filesystem or inconsistent backups, it is strongly recommended to store the dump
files on a dedicated and monitored filesystem.
This method will not provide the shortest possible RTO because the restore process can require that you recreate databases
from the dump files.
Pre-freeze script
#!/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"
dump_file="/tmp/mysql_dump.sql"
database="--all-databases"
if [ -f $defaults_file ]
then
opts="--defaults-file=$defaults_file"
elif [ -n $use_credentials ]
then
opts="$opts $use_credentials"
else
echo "$0 : error, no mysql authentication method set" | logger
exit 1
fi
opts="$opts $database"
echo "$0 executing mysqldump" | logger
mysqldump $opts >$dump_file 2>/dev/null
if [ $? -ne 0 ]
then
echo "$0 : mysqldump failed" | logger
exit 2
else
echo "$0 : mysqldump suceeded" | logger
sync;sync
fi
© 2018 Veeam Software. Confidential information. All rights reserved. All trademarks are the property of their respective owners. 11
Consistent protection of MySQL/MariaDB with Veeam
Post-thaw script
#!/bin/bash
dump_file="/tmp/mysql_dump.sql"
if [ -f $dump_file ]
then
echo "$0 deleting mysql dump file $dump_file" | logger
rm -f $dump_file > /dev/null 2>&1
exit 0
else
echo "$0 could not locate mysql dump file $dump_file" | logger
exit 1
fi
Advantages
The database will stay online, but read only. No additional storage is required.
This method provides a short RTO because no further action than booting the restored guest is required.
Disadvantages
The database is partially unavailable during VM snapshot creation. A timeout has to be set to force the release of the read-only
state, even if the snapshot has not completed within the allotted time period. In the sample provided, the timeout was set to
300 seconds, but might be modified depending the database size and activity.
© 2018 Veeam Software. Confidential information. All rights reserved. All trademarks are the property of their respective owners. 12
Consistent protection of MySQL/MariaDB with Veeam
Pre-freeze script
#!/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
Post-thaw script
#!/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
© 2018 Veeam Software. Confidential information. All rights reserved. All trademarks are the property of their respective owners. 13
Consistent protection of MySQL/MariaDB with Veeam
These scripts will shut down and restart the MySQL service using init.d or systemctl commands, depending on the database
packages. The account calling these scripts should have enough permissions to start and stop MySQL processes.
Advantages
This is easy to set up and requires no extra space.
This method will also provide a short RTO, since no further action, other than booting
the restored guest, is required.
Disadvantages
The databases will be totally unavailable while the guest snapshot is created.
#!/bin/bash
timeout=300
if [ -f /var/run/mariadb/mariadb.pid ]
then
mysql_pid=$(cat /var/run/mariadb/mariadb.pid) >/dev/null 2>&1
else
echo "$0 : MariaDB not started or bad MariaDB pid file location" | logger
exit 1
fi
echo "$0 : Processing pre-freeze backup script" | logger
systemctl stop mariadb & > /dev/null 2>&1
c=0
while [ true ]
do
if [ $c -gt $timeout ]
then
echo "$0 : timed out, MariaDB shutdown failed" | logger
exit 2
fi
# check if MariaDB is running
if [ -f /var/run/mariadb/mariadb.pid ]
then
echo "$0 : Waiting 5 more seconds for MariaDB shutdown" | logger
sleep 5
c=$((c+5))
else
echo "$0 : MariaDB stopped" | logger
sync;sync
break
fi
done
© 2018 Veeam Software. Confidential information. All rights reserved. All trademarks are the property of their respective owners. 14
Consistent protection of MySQL/MariaDB with Veeam
#!/bin/bash
timeout=300
echo "$0 : processing post-thaw backup script" | logger
if [ -f /var/run/mariadb/mariadb.pid ]
then
MariaDB_pid=$(cat /var/run/mariadb/mariadb.pid) >/dev/null 2>&1
echo "$0 : MariaDB already started with PID $MariaDB_pid " | logger
exit 1
fi
systemctl start mariadb & > /dev/null 2>&1
c=0
while [ true ]
do
if [ $c -gt $timeout ]
then
echo "$0 : timed out, MariaDB startup failed" | logger
exit 2
fi
# check if MariaDB is running
if [ -f /var/run/mariadb/mariadb.pid ]
then
MariaDB_pid =$(cat /var/run/mariadb/mariadb.pid) >/dev/null 2>&1
echo "$0 : MariaDB started with pid $MariaDB_pid " | logger
break
else
echo "$0 : Waiting 5 more seconds for MariaDB startup"
sleep 5
c=$((c+5))
fi
done
© 2018 Veeam Software. Confidential information. All rights reserved. All trademarks are the property of their respective owners. 15
Consistent protection of MySQL/MariaDB with Veeam
#!/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
© 2018 Veeam Software. Confidential information. All rights reserved. All trademarks are the property of their respective owners. 16
Consistent protection of MySQL/MariaDB with Veeam
Recovery
Depending on the backup method used and the type of outage that occurs, either at the guest or application level, the recovery
process may differ from a single file transfer to a full guest recovery.
Guest recovery
Because the cold backup and freeze method will leave the database consistent and able to startup without additional operation,
restoring the VM from the backup files is the only operation to perform.
Assuming that the Veeam Backup Repository can sustain an intensive I/O pattern (especially random read), the guest recovery
might benefit from the Veeam Instant VM Recovery feature, which allows you to boot up the guest directly from the Veeam
Backup Repository within minutes, and to live migrate it later on. Using deduplication appliances, very slow spinning drives,
or low-end NAS appliances might make the Instant VM Recovery feature slower than a full guest restore.
To access the Instant VM Recovery wizard, select the Jobs menu on the left pane of the Backup and Replication main view,
then launch the Restore wizard on the top ribbon. Select the proper guest and restore point to restore.
© 2018 Veeam Software. Confidential information. All rights reserved. All trademarks are the property of their respective owners. 17
Consistent protection of MySQL/MariaDB with Veeam
Instant VM Recovery allows you to instantly boot the guest at its original location, or at any user-defined environment.
Booting the guest in a separate environment might be useful for testing purposes to modify the name of the guest
or if the original location is unavailable.
Refer to Veeam User Guide for more about Veeam Instant VM Recovery here:
https://helpcenter.veeam.com/backup/vsphere/instant_recovery.html.
© 2018 Veeam Software. Confidential information. All rights reserved. All trademarks are the property of their respective owners. 18
Consistent protection of MySQL/MariaDB with Veeam
Once the Instant VM Recovery process ends, the guest is booted from Veeam V-Power NFS data store and left running. This
situation must be considered temporary because the guest should be migrated to production storage as soon as possible using:
• Live VMware storage vMotion (if licensed) with no downtime
• Planned VMware cold vMotion with downtime
• Planned Veeam Quick Migration with limited downtime
Refer to the Veeam User Guide for more about Veeam quick migration here:
https://helpcenter.veeam.com/backup/vsphere/quick_migration.html
Then, the additional operation of injecting the dump file into the database is necessary, using file redirection.
For example, if a whole set of databases has been dumped into a single file, the following command line should be used:
If the guest is hosting more than one database, each of which being dumped in a separate file, it is necessary to recreate the
database prior to injecting the dump file:
© 2018 Veeam Software. Confidential information. All rights reserved. All trademarks are the property of their respective owners. 19
Consistent protection of MySQL/MariaDB with Veeam
Veeam U-AIR is a unique Veeam feature that relies on V-Power NFS and Virtual Lab features. It allows you to boot the backed-up
guest directly from the Veeam Backup files and into an isolated network.
Connectivity between the recovery guest and the production network is provided by an automatically deployed virtual
appliance in charge of NAT.
© 2018 Veeam Software. Confidential information. All rights reserved. All trademarks are the property of their respective owners. 20
Consistent protection of MySQL/MariaDB with Veeam
Founded in 2006, Veeam currently has 51,000 ProPartners and more than 267,500 customers worldwide. Veeam‘s
global headquarters are located in Baar, Switzerland, and the company has offices throughout the world. To learn more,
visit http://www.veeam.com.
© 2018 Veeam Software. Confidential information. All rights reserved. All trademarks are the property of their respective owners. 21
Consistent protection of MySQL/MariaDB with Veeam
© 2018 Veeam Software. Confidential information. All rights reserved. All trademarks are the property of their respective owners. 22