X-Git-Url: https://arthur.barton.de/gitweb/?p=MkMySqlDump.git;a=blobdiff_plain;f=bin%2Fmkmysqldump;h=6ed980cdab1a2759b97ed0e66b5039e29dee2e6a;hp=9bab3665e67e6b36c73005286c49b81294645d64;hb=f55aed8e92a0ddecaa673b7085135e37307b8f61;hpb=3eee654937e4f5f2d740cc54e93f8990db742bc3 diff --git a/bin/mkmysqldump b/bin/mkmysqldump index 9bab366..6ed980c 100755 --- a/bin/mkmysqldump +++ b/bin/mkmysqldump @@ -3,6 +3,12 @@ # mkmysqldump -- dump MySQL (master) data # Copyright (c)2015 Alexander Barton # +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# Please read the file COPYING, README and AUTHORS for more information. +# NAME=$(basename "$0") @@ -12,6 +18,7 @@ MYUSER="root" MYPW="" OUTFILE="$(hostname -s)-$(date "+%Y%m%d-%H%M%S").sql" STATS="" +COMPRESS="" Help() { echo "$NAME [ ...]" @@ -28,6 +35,12 @@ Help() { echo " Directory for automatically named dump files." echo " --summary|-s" echo " Display file and file system status summary." + echo " --gzip" + echo " Compress SQL dump file using gzip(1)" + echo " --bzip2" + echo " Compress SQL dump file using bzip2(1)" + echo " --xz" + echo " Compress SQL dump file using xz(1)" echo } @@ -36,6 +49,12 @@ Usage() { exit 2 } +ErrorNotice() { + echo + echo "-----> THERE HAVE BEEN ERRORS! <-----" + echo +} + for cmd in mysql mysqldump; do if ! which "$cmd" >/dev/null 2>&1; then echo "$NAME: \"$cmd\" command not found!" @@ -52,7 +71,7 @@ while [ $# -gt 0 ]; do ;; "--user"|"-u") [ $# -ge 2 ] || Usage - MYHOST="$2" + MYUSER="$2" shift 2 ;; "--password"|"-p") @@ -70,6 +89,18 @@ while [ $# -gt 0 ]; do OUTFILE="$2/$(hostname -s)-$(date "+%Y%m%d-%H%M%S").sql" shift 2 ;; + "--gzip") + COMPRESS="gzip -v" + shift + ;; + "--bzip2") + COMPRESS="bzip2 -v" + shift + ;; + "--xz") + COMPRESS="xz -v" + shift + ;; "--summary"|"-s") STATS=1 shift @@ -84,6 +115,9 @@ while [ $# -gt 0 ]; do done echo "Dumping MySQL server on \"$MYHOST\" (user \"$MYUSER\"):" +echo +echo "Started: $(date)" + umask 0077 [ -n "$MYPW" ] && PWSWITCH="--password='$MYPW'" || PWSWITCH="" @@ -98,33 +132,63 @@ DATABASES=$( ) if [ $? -ne 0 ]; then echo "Failed to get list of databases! Aborting!" + ErrorNotice exit 1 fi +echo "Checking slave status ..." +Slave_IO_Running=""; Slave_SQL_Running="" +eval "$(echo 'SHOW SLAVE STATUS\G' | mysql -h "$MYHOST" -u "$MYUSER" $PWSWITCH | sed -n '/Running/p' | sed 's/: /=/g')" +if [ -n "$Slave_IO_Running" -o -n "$Slave_SQL_Running" ]; then + if [ "$Slave_IO_Running" = "Yes" -a "$Slave_SQL_Running" = "Yes" ]; then + echo "Server is running as MySQL slave, replication is Ok." + else + echo "Server is running as MySQL slave, but replication FAILED!" + ErrorNotice + exit 1 + fi +else + echo "Server is not running as MySQL slave. Ok." +fi + +echo "Will dump the following databases:" +for d in $DATABASES; do + echo " - $d" +done + echo "Dumping SQL data to file \"$OUTFILE\" ..." +echo "Beginning dump: $(date)" CMD="mysqldump \ -h $MYHOST \ -u $MYUSER \ $PWSWITCH \ - --lock-all-tables \ + --master-data=1 \ --databases $DATABASES" - -echo "Start: $(date)" -echo $CMD $CMD >"$OUTFILE"; r=$? +if [ -n "$COMPRESS" -a -s "$OUTFILE" -a $r -eq 0 ]; then + echo "Dump done: $(date)" + echo "Compressing dump file ($COMPRESS) ..." + $COMPRESS "$OUTFILE" +fi echo "End: $(date)" +echo -[ $r -eq 0 ] \ - && echo "Dump command exited with code 0, success." \ - || echo "Dump command FAILED with code $r!" >&2 - -if [ -e "$OUTFILE" -a -n "$STATS" ]; then - echo +if [ -n "$STATS" ]; then echo "SQL dump file" - ls -lh "$OUTFILE" + for f in "$OUTFILE"*; do + ls -lh "$f"* + done echo - df -h "$OUTFILE" + df -h "$(dirname "$OUTFILE")" echo fi +if [ $r -eq 0 ]; then + echo "Dump command exited with code 0, success." +else + echo "Dump command FAILED with code $r!" >&2 + ErrorNotice +fi + +echo exit $r