-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql-dump.sh
executable file
·105 lines (93 loc) · 2.14 KB
/
mysql-dump.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/bin/bash
# mysql-dump.sh: mysqldump wrapper, with optional compressed network transfer
# Copyright 2015 David Ulrich
#
# 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.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
if [ $# -eq 0 ]; then
echo "USAGE: ./mysql-dump.sh -h SSH_HOST -s SSH_USER -[bgr] [-[iI]] CLIENT"
echo "LINK : ./mysql-dump.sh -[bgr] [-[iI]] -L URL"
exit 2
fi
zip=""
unzip=""
ext=""
import=0
luser="root"
ruser="root"
host=""
suser=""
while getopts ":bgh:iIL:rs:u:U:" opt; do
case $opt in
b) # bzip compression
zip="bzip2"
unzip="bunzip2 -k"
ext=".bz2"
;;
g) # gzip compression
zip="gzip -v -9"
unzip="gunzip"
ext=".gz"
;;
h) # ssh host
host="$OPTARG"
;;
i) # import mode
import=1
;;
I) # import only mode
import=2
;;
L)
link="$OPTARG"
;;
r) # no compression
zip="cat"
unzip="cat"
ext=""
;;
s) # ssh user
suser="$OPTARG"
;;
u) # remote mysql user
ruser="$OPTARG"
;;
U) # local mysql user
luser="$OPTARG"
;;
\?)
echo "Invalid option: -$OPTARG"
exit 1
;;
:)
echo "Option -$OPTARG requires an argument"
exit 1
;;
esac
done
shift $(($OPTIND - 1))
if [ "$zip" = "" ]; then
echo "A compression option [one of -b -g -r] is required"
exit 1
fi
dump="mysqldump -u \"$ruser\" -p \"$1\" | \"$zip\""
if [ "$host" != "" ] && [ "$suser" != "" ]; then
ssh $suser@$host "eval $dump" > "$1.sql$ext"
elif [ "$link" != "" ]; then
wget "$link" -O "$1.sql$ext"
elif [ $import -ne 2 ]; then
eval $dump > "$1.sql$ext"
fi
if [ $import -gt 0 ]; then
$unzip < "$1.sql$ext" | mysql -A -D "$1" -u "$luser" -p
fi