Linux MySQL/MariaDB Backup Script

Discussions specific to VMware vSphere hypervisor

Linux MySQL/MariaDB Backup Script

Veeam Logoby bslit » Wed May 16, 2018 9:38 am

Case ID# 03002513

We are having troubles with the MySQL/MariaDB Script.
We followed the instructions on the Veeam White Paper form: https://www.veeam.com/wp-consistent-protection-mysql-mariadb.html
We are using the Hot backup – Database freezing method with the following Scripts.
Please note, we slightly modified the script with the –h option at the mysql command as we are connecting with SSL and need to connect with the hostname instead of localhost. The connection works fine, this isn’t a problem.
Pre-freeze script:
Code: Select all
#!/bin/bash -xv
# 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="-u root -p"
#defaults_file="/etc/my.cnf"
defaults_file="/etc/my.cnf.d/veeambackup-client.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 -h "HOSTNAME.DOMAIN.EXAMPLE" --execute="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:
Code: Select all
#!/bin/bash -xv
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
When running the scripts manually, not through veeam, (first pre-freeze and then post-thaw afterwards) we get the following error message:
Code: Select all
#!/bin/bash -xv
lock_file=/tmp/mysql_tables_read_lock
+ lock_file=/tmp/mysql_tables_read_lock
###
mysql_pid=$(cat $lock_file)
++ cat /tmp/mysql_tables_read_lock
+ mysql_pid=14023
echo "$0 sending sigterm to $mysql_pid" | logger
+ echo './post-thaw_mysqldump-script.sh sending sigterm to 14023'
+ logger
pkill -9 -P $mysql_pid
+ pkill -9 -P 14023
ERROR at line 1: Unknown command '\ '.
rm -f $lock_file
+ rm -f /tmp/mysql_tables_read_lock
exit 0
+ exit 0

We know the error has it’s source in the pre-freeze script at the line:
mysql $opts -h " HOSTNAME.DOMAIN.EXAMPLE " --execute="FLUSH TABLES WITH READ LOCK; system touch $lock_file; system nohup sleep $sleep_time; system echo\ lock released|logger;" > /dev/null & mysql_pid=$!
If we were to omit the backslash after echo and just leave the line as followed, we wouldn’t get an error:
mysql $opts -h " HOSTNAME.DOMAIN.EXAMPLE " --execute="FLUSH TABLES WITH READ LOCK; system touch $lock_file; system nohup sleep $sleep_time; system echo lock released|logger;" > /dev/null & mysql_pid=$!
Question:
Is there a typo in the white paper and shouldn’t there be a backslash after the echo command? So the command would look like:
… system echo lock released|logger; …
What’s the purpose of the command if the backslash is needed?
… system echo\ lock released|logger; …
How can we fix the issue if the backslash is needed?

System:
CentOS Linux release 7.4.1708 (core) (Compatibility: ESXi 6.5 and higher (VM-Version 13))
MariaDB 5.5.56
Hypervisor: VMware ESXi 6.5.0 Build 7119157
bslit
Lurker
 
Posts: 2
Liked: never
Joined: Fri Feb 06, 2015 9:10 am

Re: Linux MySQL/MariaDB Backup Script

Veeam Logoby PTide » Wed May 16, 2018 12:03 pm 1 person likes this post

Hi,

To the best of my knowledge there should be no backslash so it's probably a typo. Have you tried to omit it? It won't do harm - the piece of code is just just calling bash command "echo" to pipe "lock released" message to logger so it writes it to the system log.

Thanks
PTide
Veeam Software
 
Posts: 3798
Liked: 315 times
Joined: Tue May 19, 2015 1:46 pm

Re: Linux MySQL/MariaDB Backup Script

Veeam Logoby bslit » Thu May 17, 2018 6:49 am

PTide wrote:Hi,

To the best of my knowledge there should be no backslash so it's probably a typo. Have you tried to omit it? It won't do harm - the piece of code is just just calling bash command "echo" to pipe "lock released" message to logger so it writes it to the system log.

Thanks



Hello,

Firstly thank you for the quick reply.

Yes we tried to omit it already before posting here. The scripts ran without errors when omitting the backslash, so this raised the question if the backslash just was a typo in the white paper or if really the command "echo\" was meant, which as you know has a different usage. So thank you for clearing things, we will omit the backslash in the script.


Cheers
bslit
Lurker
 
Posts: 2
Liked: never
Joined: Fri Feb 06, 2015 9:10 am

Re: Linux MySQL/MariaDB Backup Script

Veeam Logoby pdimarco » Thu May 17, 2018 12:44 pm

Hi,

Please ignore this backslash if you copy/past the script. It is a common way to tell you that the carriage return should be ignored in the document you are reading.

You can dowlnload the scripts on our github: https://github.com/VeeamHub/applications/tree/master/mysql

Cheers
Pascal
pdimarco
Veeam Software
 
Posts: 204
Liked: 15 times
Joined: Tue Sep 02, 2014 7:25 am
Full Name: Pascal DiMarco


Return to VMware vSphere



Who is online

Users browsing this forum: Google Feedfetcher and 10 guests