]> arthur.barton.de Git - MkMySqlDump.git/blob - bin/mkmysqldump
Correctly set password variable fro mysql[_dump]
[MkMySqlDump.git] / bin / mkmysqldump
1 #!/bin/sh
2 #
3 # mkmysqldump -- dump MySQL (master) data
4 # Copyright (c)2015 Alexander Barton <alex@barton.de>
5 #
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.
11 #
12
13 NAME=$(basename "$0")
14
15 # Defaults
16 MYHOST="localhost"
17 MYUSER="root"
18 MYPW=""
19 OUTFILE="$(hostname -s)-$(date "+%Y%m%d-%H%M%S").sql"
20 STATS=""
21 COMPRESS=""
22
23 Help() {
24         echo "$NAME [<options> ...]"
25         echo
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."
36         echo "  --summary|-s"
37         echo "      Display file and file system status summary."
38         echo "  --gzip"
39         echo "      Compress SQL dump file using gzip(1)"
40         echo "  --bzip2"
41         echo "      Compress SQL dump file using bzip2(1)"
42         echo "  --xz"
43         echo "      Compress SQL dump file using xz(1)"
44         echo
45 }
46
47 Usage() {
48         Help
49         exit 2
50 }
51
52 ErrorNotice() {
53         echo
54         echo "----->  THERE HAVE BEEN ERRORS!  <-----"
55         echo
56 }
57
58 for cmd in mysql mysqldump; do
59         if ! which "$cmd" >/dev/null 2>&1; then
60                 echo "$NAME: \"$cmd\" command not found!"
61                 exit 1
62         fi
63 done
64
65 while [ $# -gt 0 ]; do
66         case "$1" in
67           "--host"|"-h")
68                 [ $# -ge 2 ] || Usage
69                 MYHOST="$2"
70                 shift 2
71                 ;;
72           "--user"|"-u")
73                 [ $# -ge 2 ] || Usage
74                 MYUSER="$2"
75                 shift 2
76                 ;;
77           "--password"|"-p")
78                 [ $# -ge 2 ] || Usage
79                 MYPW="$2"
80                 shift 2
81                 ;;
82           "--outfile"|"-o")
83                 [ $# -ge 2 ] || Usage
84                 OUTFILE="$2"
85                 shift 2
86                 ;;
87           "--outdir"|"-d")
88                 [ $# -ge 2 ] || Usage
89                 OUTFILE="$2/$(hostname -s)-$(date "+%Y%m%d-%H%M%S").sql"
90                 shift 2
91                 ;;
92           "--gzip")
93                 COMPRESS="gzip -v"
94                 shift
95                 ;;
96           "--bzip2")
97                 COMPRESS="bzip2 -v"
98                 shift
99                 ;;
100           "--xz")
101                 COMPRESS="xz -v"
102                 shift
103                 ;;
104           "--summary"|"-s")
105                 STATS=1
106                 shift
107                 ;;
108           "--help")
109                 Help
110                 exit 0
111                 ;;
112           *)
113                 Usage
114         esac
115 done
116
117 echo "Dumping MySQL server on \"$MYHOST\" (user \"$MYUSER\"):"
118 echo
119 echo "Started: $(date)"
120
121 umask 0077
122
123 [ -n "$MYPW" ] && PWSWITCH="--password=$MYPW" || PWSWITCH=""
124
125 echo "Getting list of databases from server ..."
126 DATABASES=$(
127         mysql -h "$MYHOST" -u "$MYUSER" $PWSWITCH -e 'show databases' \
128          | grep "^\| " | cut -d' ' -f2 | grep -v "Database" \
129          | grep -v "information_schema" \
130          | grep -v "performance_schema" \
131          | grep -v "mysql"
132 )
133 if [ $? -ne 0 ]; then
134         echo "Failed to get list of databases! Aborting!"
135         ErrorNotice
136         exit 1
137 fi
138
139 echo "Checking slave status ..."
140 Slave_IO_Running=""; Slave_SQL_Running=""
141 eval "$(echo 'SHOW SLAVE STATUS\G' | mysql -h "$MYHOST" -u "$MYUSER" $PWSWITCH | sed -n '/Running/p' | sed 's/: /=/g')"
142 if [ -n "$Slave_IO_Running" -o -n "$Slave_SQL_Running" ]; then
143         if [ "$Slave_IO_Running" = "Yes" -a "$Slave_SQL_Running" = "Yes" ]; then
144                 echo "Server is running as MySQL slave, replication is Ok."
145         else
146                 echo "Server is running as MySQL slave, but replication FAILED!"
147                 ErrorNotice
148                 exit 1
149         fi
150 else
151         echo "Server is not running as MySQL slave. Ok."
152 fi
153
154 echo "Will dump the following databases:"
155 for d in $DATABASES; do
156         echo " - $d"
157 done
158
159 echo "Dumping SQL data to file \"$OUTFILE\" ..."
160 echo "Beginning dump: $(date)"
161 CMD="mysqldump \
162         -h $MYHOST \
163         -u $MYUSER \
164         $PWSWITCH \
165         --master-data=1 \
166         --databases $DATABASES"
167 $CMD >"$OUTFILE"; r=$?
168 if [ -n "$COMPRESS" -a -s "$OUTFILE" -a $r -eq 0 ]; then
169         echo "Dump done: $(date)"
170         echo "Compressing dump file ($COMPRESS) ..."
171         $COMPRESS "$OUTFILE"
172 fi
173 echo "End: $(date)"
174 echo
175
176 if [ -n "$STATS" ]; then
177         echo "SQL dump file"
178         for f in "$OUTFILE"*; do
179                 ls -lh "$f"*
180         done
181         echo
182         df -h "$(dirname "$OUTFILE")"
183         echo
184 fi
185
186 if [ $r -eq 0 ]; then
187         echo "Dump command exited with code 0, success."
188 else
189         echo "Dump command FAILED with code $r!" >&2
190         ErrorNotice
191 fi
192
193 echo
194 exit $r