3 # mkmysqldump -- dump MySQL (master) data
4 # Copyright (c)2015 Alexander Barton <alex@barton.de>
13 OUTFILE="$(hostname -s)-$(date "+%Y%m%d-%H%M%S").sql"
18 echo "$NAME [<options> ...]"
20 echo " --host|-h <host>"
21 echo " MySQL server hostname [\"$MYHOST\"]."
22 echo " --user|-u <user>"
23 echo " MySQL server user [\"$MYUSER\"]."
24 echo " --password|-p <password>"
25 echo " MySQL server password [\"$MYPW\"]".
26 echo " --outfile|-o <file>"
27 echo " Path and name of SQL dump file [\"$OUTFILE\"]."
28 echo " --outdir|-d <directory>"
29 echo " Directory for automatically named dump files."
31 echo " Display file and file system status summary."
33 echo " Compress SQL dump file using gzip(1)"
35 echo " Compress SQL dump file using bzip2(1)"
37 echo " Compress SQL dump file using xz(1)"
46 for cmd in mysql mysqldump; do
47 if ! which "$cmd" >/dev/null 2>&1; then
48 echo "$NAME: \"$cmd\" command not found!"
53 while [ $# -gt 0 ]; do
77 OUTFILE="$2/$(hostname -s)-$(date "+%Y%m%d-%H%M%S").sql"
105 echo "Dumping MySQL server on \"$MYHOST\" (user \"$MYUSER\"):"
106 echo "Started: $(date)"
109 [ -n "$MYPW" ] && PWSWITCH="--password='$MYPW'" || PWSWITCH=""
111 echo "Getting list of databases from server ..."
113 mysql -h "$MYHOST" -u "$MYUSER" $PWSWITCH -e 'show databases' \
114 | grep "^\| " | cut -d' ' -f2 | grep -v "Database" \
115 | grep -v "information_schema" \
116 | grep -v "performance_schema" \
119 if [ $? -ne 0 ]; then
120 echo "Failed to get list of databases! Aborting!"
124 echo "Dumping SQL data to file \"$OUTFILE\" ..."
130 --databases $DATABASES"
132 echo "Start date: $(date)"
134 $CMD >"$OUTFILE"; r=$?
135 if [ -n "$COMPRESS" -a -s "$OUTFILE" -a $r -eq 0 ]; then
136 echo "Dump done: $(date)"
137 echo "Compressing dump file ($COMPRESS) ..."
143 if [ -n "$STATS" ]; then
145 for f in "$OUTFILE"*; do
149 df -h "$(dirname "$OUTFILE")"
154 && echo "Dump command exited with code 0, success." \
155 || echo "Dump command FAILED with code $r!" >&2