#!/bin/sh # # 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") # Defaults MYHOST="localhost" MYUSER="root" MYPW="" OUTFILE="$(hostname -s)-$(date "+%Y%m%d-%H%M%S").sql" STATS="" COMPRESS="" Help() { echo "$NAME [ ...]" echo echo " --host|-h " echo " MySQL server hostname [\"$MYHOST\"]." echo " --user|-u " echo " MySQL server user [\"$MYUSER\"]." echo " --password|-p " echo " MySQL server password [\"$MYPW\"]". echo " --outfile|-o " echo " Path and name of SQL dump file [\"$OUTFILE\"]." echo " --outdir|-d " 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 } Usage() { Help 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!" exit 1 fi done while [ $# -gt 0 ]; do case "$1" in "--host"|"-h") [ $# -ge 2 ] || Usage MYHOST="$2" shift 2 ;; "--user"|"-u") [ $# -ge 2 ] || Usage MYHOST="$2" shift 2 ;; "--password"|"-p") [ $# -ge 2 ] || Usage MYPW="$2" shift 2 ;; "--outfile"|"-o") [ $# -ge 2 ] || Usage OUTFILE="$2" shift 2 ;; "--outdir"|"-d") [ $# -ge 2 ] || Usage 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 ;; "--help") Help exit 0 ;; *) Usage esac done echo "Dumping MySQL server on \"$MYHOST\" (user \"$MYUSER\"):" echo "Started: $(date)" umask 0077 [ -n "$MYPW" ] && PWSWITCH="--password='$MYPW'" || PWSWITCH="" echo "Getting list of databases from server ..." DATABASES=$( mysql -h "$MYHOST" -u "$MYUSER" $PWSWITCH -e 'show databases' \ | grep "^\| " | cut -d' ' -f2 | grep -v "Database" \ | grep -v "information_schema" \ | grep -v "performance_schema" \ | grep -v "mysql" ) if [ $? -ne 0 ]; then echo "Failed to get list of databases! Aborting!" ErrorNotice exit 1 fi echo "Will dump the following databases:" for d in $DATABASES; do echo " - $d" done echo "Dumping SQL data to file \"$OUTFILE\" ..." echo "Start date: $(date)" CMD="mysqldump \ -h $MYHOST \ -u $MYUSER \ $PWSWITCH \ --master-data=1 \ --databases $DATABASES" $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 if [ -n "$STATS" ]; then echo "SQL dump file" for f in "$OUTFILE"*; do ls -lh "$f"* done echo 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