]> arthur.barton.de Git - MkMySqlDump.git/blob - bin/mkmysqldump
mkmysqldump: Make sure "HOME" is set correctly
[MkMySqlDump.git] / bin / mkmysqldump
1 #!/bin/sh
2 #
3 # mkmysqldump -- dump MySQL (master) data
4 # Copyright (c)2015-2017 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 # 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 )
61 export HOME
62
63 for cmd in mysql mysqldump; do
64         if ! which "$cmd" >/dev/null 2>&1; then
65                 echo "$NAME: \"$cmd\" command not found!"
66                 exit 1
67         fi
68 done
69
70 while [ $# -gt 0 ]; do
71         case "$1" in
72           "--host"|"-h")
73                 [ $# -ge 2 ] || Usage
74                 MYHOST="$2"
75                 shift 2
76                 ;;
77           "--user"|"-u")
78                 [ $# -ge 2 ] || Usage
79                 MYUSER="$2"
80                 shift 2
81                 ;;
82           "--password"|"-p")
83                 [ $# -ge 2 ] || Usage
84                 MYPW="$2"
85                 shift 2
86                 ;;
87           "--outfile"|"-o")
88                 [ $# -ge 2 ] || Usage
89                 OUTFILE="$2"
90                 shift 2
91                 ;;
92           "--outdir"|"-d")
93                 [ $# -ge 2 ] || Usage
94                 OUTFILE="$2/$(hostname -s)-$(date "+%Y%m%d-%H%M%S").sql"
95                 shift 2
96                 ;;
97           "--gzip")
98                 COMPRESS="gzip -v"
99                 shift
100                 ;;
101           "--bzip2")
102                 COMPRESS="bzip2 -v"
103                 shift
104                 ;;
105           "--xz")
106                 COMPRESS="xz -v"
107                 shift
108                 ;;
109           "--summary"|"-s")
110                 STATS=1
111                 shift
112                 ;;
113           "--help")
114                 Help
115                 exit 0
116                 ;;
117           *)
118                 Usage
119         esac
120 done
121
122 echo "Dumping MySQL server on \"$MYHOST\" (user \"$MYUSER\"):"
123 echo
124 echo "Started: $(date)"
125
126 umask 0077
127
128 [ -n "$MYPW" ] && PWSWITCH="--password=$MYPW" || PWSWITCH=""
129
130 echo "Getting list of databases from server ..."
131 DATABASES=$(
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" \
136          | grep -v "mysql"
137 )
138 if [ $? -ne 0 ]; then
139         echo "Failed to get list of databases! Aborting!"
140         ErrorNotice
141         exit 1
142 fi
143
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."
150         else
151                 echo "Server is running as MySQL slave, but replication FAILED!"
152                 ErrorNotice
153                 exit 1
154         fi
155 else
156         echo "Server is not running as MySQL slave. Ok."
157 fi
158
159 echo "Will dump the following databases:"
160 for d in $DATABASES; do
161         echo " - $d"
162 done
163
164 echo "Dumping SQL data to file \"$OUTFILE\" ..."
165 echo "Beginning dump: $(date)"
166 CMD="mysqldump \
167         -h $MYHOST \
168         -u $MYUSER \
169         $PWSWITCH \
170         --master-data=1 \
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
178 fi
179 echo "End: $(date)"
180 echo
181
182 if [ -n "$STATS" ]; then
183         echo "SQL dump file"
184         for f in "$OUTFILE"*; do
185                 ls -lh "$f"*
186         done
187         echo
188         df -h "$(dirname "$OUTFILE")"
189         echo
190 fi
191
192 if [ $r -eq 0 ]; then
193         echo "Dump command exited with code 0, success."
194         echo
195 else
196         echo "Dump command FAILED with code $r!" >&2
197         ErrorNotice
198 fi
199
200 exit $r