3 # mkmysqldump -- dump MySQL (master) data
4 # Copyright (c)2015-2017 Alexander Barton <alex@barton.de>
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10 # Please read the file COPYING, README and AUTHORS for more information.
19 OUTFILE="$(hostname -s)-$(date "+%Y%m%d-%H%M%S").sql"
24 echo "$NAME [<options> ...]"
26 echo " --host|-h <host>"
27 echo " MySQL server hostname [\"$MYHOST\"]."
28 echo " --user|-u <user>"
29 echo " MySQL server user [\"$MYUSER\"]."
30 echo " --password|-p <password>"
31 echo " MySQL server password [\"$MYPW\"]".
32 echo " --outfile|-o <file>"
33 echo " Path and name of SQL dump file [\"$OUTFILE\"]."
34 echo " --outdir|-d <directory>"
35 echo " Directory for automatically named dump files."
37 echo " Display file and file system status summary."
39 echo " Compress SQL dump file using gzip(1)"
41 echo " Compress SQL dump file using bzip2(1)"
43 echo " Compress SQL dump file using xz(1)"
54 echo "-----> THERE HAVE BEEN ERRORS! <-----"
58 # Make sure the environment is "sane", for example, mysql(1) depends on the
59 # HOME environment variable to be set.
60 [ -n "$HOME" ] || HOME=$( getent passwd "${LOGNAME:-root}" | cut -d: -f6 )
63 for cmd in mysql mysqldump; do
64 if ! which "$cmd" >/dev/null 2>&1; then
65 echo "$NAME: \"$cmd\" command not found!"
70 while [ $# -gt 0 ]; do
94 OUTFILE="$2/$(hostname -s)-$(date "+%Y%m%d-%H%M%S").sql"
122 echo "Dumping MySQL server on \"$MYHOST\" (user \"$MYUSER\"):"
124 echo "Started: $(date)"
128 [ -n "$MYPW" ] && PWSWITCH="--password=$MYPW" || PWSWITCH=""
130 echo "Getting list of databases from server ..."
132 mysql -h "$MYHOST" -u "$MYUSER" $PWSWITCH -e 'show databases' \
133 | grep "^\| " | cut -d' ' -f2 | grep -v "Database" \
134 | grep -v "information_schema" \
135 | grep -v "performance_schema" \
138 if [ $? -ne 0 ]; then
139 echo "Failed to get list of databases! Aborting!"
144 echo "Checking slave status ..."
145 Slave_IO_Running=""; Slave_SQL_Running=""
146 eval "$(echo 'SHOW SLAVE STATUS\G' | mysql -h "$MYHOST" -u "$MYUSER" $PWSWITCH | sed -n '/Running/p' | sed 's/: /=/g')"
147 if [ -n "$Slave_IO_Running" -o -n "$Slave_SQL_Running" ]; then
148 if [ "$Slave_IO_Running" = "Yes" -a "$Slave_SQL_Running" = "Yes" ]; then
149 echo "Server is running as MySQL slave, replication is Ok."
151 echo "Server is running as MySQL slave, but replication FAILED!"
156 echo "Server is not running as MySQL slave. Ok."
159 echo "Will dump the following databases:"
160 for d in $DATABASES; do
164 echo "Dumping SQL data to file \"$OUTFILE\" ..."
165 echo "Beginning dump: $(date)"
171 --databases $DATABASES"
172 $CMD >"$OUTFILE"; r=$?
173 if [ -n "$COMPRESS" -a -s "$OUTFILE" -a $r -eq 0 ]; then
174 echo "Dump done: $(date)"
175 echo "Compressing dump file ($COMPRESS) ..."
176 $COMPRESS "$OUTFILE" 2>&1
177 [ $? -eq 0 ] || echo "Error compressing dump file!" >&2
182 if [ -n "$STATS" ]; then
184 for f in "$OUTFILE"*; do
188 df -h "$(dirname "$OUTFILE")"
192 if [ $r -eq 0 ]; then
193 echo "Dump command exited with code 0, success."
196 echo "Dump command FAILED with code $r!" >&2