-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysqldumpassistant
executable file
·256 lines (197 loc) · 6.99 KB
/
mysqldumpassistant
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
if [ -z ${MYSQL_DUMPS_DIR+x} ]; then
read -p "Please provide the full path where your MySQL dump files are located: " MYSQL_DUMPS_DIR
export MYSQL_DUMPS_DIR
fi
export MYSQL_DUMPS_BLACK='\033[0;30m'
export MYSQL_DUMPS_RED='\033[0;31m'
export MYSQL_DUMPS_GREEN='\033[0;32m'
export MYSQL_DUMPS_BROWN='\033[0;33m'
export MYSQL_DUMPS_BLUE='\033[0;34m'
export MYSQL_DUMPS_PURPLE='\033[0;35m'
export MYSQL_DUMPS_CYAN='\033[0;36m'
export MYSQL_DUMPS_GRAY='\033[1;30m'
export MYSQL_DUMPS_LRED='\033[1;31m'
export MYSQL_DUMPS_LGREEN='\033[1;32m'
export MYSQL_DUMPS_YELLOW='\033[1;33m'
export MYSQL_DUMPS_LBLUE='\033[1;34m'
export MYSQL_DUMPS_LPURPLE='\033[1;35m'
export MYSQL_DUMPS_LCYAN='\033[1;36m'
export MYSQL_DUMPS_WHITE='\033[1;37m'
export MYSQL_DUMPS_NC='\033[0m'
#-----LISTS ALL MYSQL DATABASES-----#
alias ldb="mysql --user=root --execute=\"SHOW DATABASES\G\" | sed -e 's#^Database: ##' | awk 'NR % 2 == 0 && !/information_schema|mysql|sys|performance_schema/ { print }';"
#-----CLEANS A MYSQL DATABASE-----#
function cdb {
echo -e "${MYSQL_DUMPS_GREEN}##--CLEAN DATABASE--##${MYSQL_DUMPS_NC}"
if [ -z ${1+x} ]; then
prompt="${MYSQL_DUMPS_YELLOW}Please select a MySQL database:${MYSQL_DUMPS_NC}"
options=( `ldb` )
echo -e ${prompt};
select schema in "${options[@]}" "Quit" ; do
if (( REPLY == 1 + ${#options[@]} )) ; then
echo -e "${MYSQL_DUMPS_LBLUE}Bye bye!${MYSQL_DUMPS_NC}"
return
elif (( REPLY > 0 && REPLY <= ${#options[@]} )) ; then
mysql --user=root --execute="DROP DATABASE ${schema}; CREATE SCHEMA ${schema} DEFAULT CHARACTER SET utf8;";
break
else
echo -e "${MYSQL_DUMPS_RED}This is not an option.${MYSQL_DUMPS_NC}"
fi
done
else
schema=$1
mysql --user=root --execute="DROP DATABASE ${schema}; CREATE SCHEMA ${schema} DEFAULT CHARACTER SET utf8;";
fi
if [ $? -eq 0 ]; then
echo -e "You successfully cleaned the database ${MYSQL_DUMPS_PURPLE}${schema}${MYSQL_DUMPS_NC}."
fi
}
#-----DROPS A MYSQL DATABASE-----#
function ddb {
echo -e "${MYSQL_DUMPS_GREEN}##--DROP DATABASE--##${MYSQL_DUMPS_NC}"
if [ -z ${1+x} ]; then
prompt="${MYSQL_DUMPS_YELLOW}Please select a MySQL database:${MYSQL_DUMPS_NC}"
options=( `ldb` )
echo -e ${prompt};
select schema in "${options[@]}" "Quit" ; do
if (( REPLY == 1 + ${#options[@]} )) ; then
echo -e "${MYSQL_DUMPS_LBLUE}Bye bye!${MYSQL_DUMPS_NC}"
return
elif (( REPLY > 0 && REPLY <= ${#options[@]} )) ; then
mysql --user=root --execute="DROP DATABASE ${schema};";
break
else
echo -e "${MYSQL_DUMPS_RED}This is not an option.${MYSQL_DUMPS_NC}"
fi
done
else
schema=$1
mysql --user=root --execute="DROP DATABASE ${schema};";
fi
if [ $? -eq 0 ]; then
echo -e "You successfully dropped the database ${MYSQL_DUMPS_PURPLE}${schema}${MYSQL_DUMPS_NC}."
fi
}
#-----EXPORTS A MYSQL DATABASE-----#
function edb {
echo -e "${MYSQL_DUMPS_GREEN}##--EXPORT DATABASE--##${MYSQL_DUMPS_NC}"
if [ -z ${1+x} ]; then
prompt="${MYSQL_DUMPS_YELLOW}Please select a MySQL database:${MYSQL_DUMPS_NC}"
options=( `ldb` )
echo -e ${prompt};
select opt in "${options[@]}" "Quit" ; do
if (( REPLY == 1 + ${#options[@]} )) ; then
echo -e "${MYSQL_DUMPS_LBLUE}Bye bye!${MYSQL_DUMPS_NC}"
return
elif (( REPLY > 0 && REPLY <= ${#options[@]} )) ; then
schema=$opt
break
else
echo -e "${MYSQL_DUMPS_RED}This is not an option.${MYSQL_DUMPS_NC}"
fi
done
else
schema=$1
fi
dbtest=`mysql --user=root --execute="SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = \"$schema\""`;
if [ -z "${dbtest}" ]; then
echo -e "${MYSQL_DUMPS_RED}Sorry, this database doesn't exist.${MYSQL_DUMPS_NC}";
return
fi
if [ -z ${2+x} ]; then
dateTime=`date +%d-%m-%Y_%H:%M:%S`
dumpFilePath="${MYSQL_DUMPS_DIR}/mysql_${schema}_dump_${dateTime}.sql"
else
dumpFilePath=$2
fi
mysqldump --user=root -p ${schema} > ${dumpFilePath}
if [ $? -eq 0 ]; then
echo -e "The dump ${MYSQL_DUMPS_CYAN}${dumpFilePath}${MYSQL_DUMPS_NC} was successfully exported from ${MYSQL_DUMPS_PURPLE}${schema}${MYSQL_DUMPS_NC}."
fi
}
#-----IMPORTS A MYSQL DATABASE-----#
function idb {
echo -e "${MYSQL_DUMPS_GREEN}##--IMPORT DATABASE--##${MYSQL_DUMPS_NC}"
if [ -z ${1+x} ]; then
prompt="${MYSQL_DUMPS_YELLOW}Please select a MySQL database:${MYSQL_DUMPS_NC}"
options=( `ldb` )
echo -e ${prompt};
select opt in "${options[@]}" "Quit" ; do
if (( REPLY == 1 + ${#options[@]} )) ; then
echo -e "${MYSQL_DUMPS_LBLUE}Bye bye!${MYSQL_DUMPS_NC}"
return
elif (( REPLY > 0 && REPLY <= ${#options[@]} )) ; then
schema=$opt
break
else
echo -e "${MYSQL_DUMPS_RED}This is not an option.${MYSQL_DUMPS_NC}"
fi
done
else
schema=$1
fi
dbtest=`mysql --user=root --execute="SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = \"$schema\""`;
if [ -z "${dbtest}" ]; then
echo -e "${MYSQL_DUMPS_RED}Sorry, this database doesn't exist.${MYSQL_DUMPS_NC}";
return
fi
if [ -z ${2+x} ]; then
prompt="${MYSQL_DUMPS_YELLOW}Please select a dump to import:${MYSQL_DUMPS_NC}"
options=( $(find ${MYSQL_DUMPS_DIR} -type f -maxdepth 1 -exec basename {} \;) )
printf "\n";
echo -e ${prompt};
select file in "${options[@]}" "Quit" ; do
if (( REPLY == 1 + ${#options[@]} )) ; then
echo -e "${MYSQL_DUMPS_LBLUE}Bye bye!${MYSQL_DUMPS_NC}"
return
elif (( REPLY > 0 && REPLY <= ${#options[@]} )) ; then
cdb ${schema};
mysql --user=root -p ${schema} < ${MYSQL_DUMPS_DIR}/${file};
break
else
echo -e "${MYSQL_DUMPS_RED}This is not an option.${MYSQL_DUMPS_NC}"
fi
done
else
file=$2
cdb ${schema};
mysql --user=root -p ${schema} < ${file};
fi
if [ $? -eq 0 ]; then
echo -e "The dump ${MYSQL_DUMPS_CYAN}${file}${MYSQL_DUMPS_NC} was successfully imported to ${MYSQL_DUMPS_PURPLE}${schema}${MYSQL_DUMPS_NC}."
fi
}
#-----CREATES A MYSQL DATABASE-----#
function ndb {
echo -e "${MYSQL_DUMPS_GREEN}##--NEW DATABASE--##${MYSQL_DUMPS_NC}"
if [ -z ${1+x} ]; then
read -p "Please provide a name for the new MySQL database: " schema
else
schema=$1
fi
mysql --user=root --execute="CREATE SCHEMA ${schema} DEFAULT CHARACTER SET utf8;";
if [ $? -eq 0 ]; then
echo -e "You successfully created the database ${MYSQL_DUMPS_PURPLE}${schema}${MYSQL_DUMPS_NC}."
fi
}
#-----DELETES A MYSQL DUMP-----#
function rmd {
echo -e "${MYSQL_DUMPS_GREEN}##--DELETE DUMP--##${MYSQL_DUMPS_NC}"
prompt="${MYSQL_DUMPS_YELLOW}Please select a file to delete from your dumps folder:${MYSQL_DUMPS_NC}"
options=( $(find ${MYSQL_DUMPS_DIR} -type f -maxdepth 1 -exec basename {} \;) )
echo -e ${prompt};
select file in "${options[@]}" "Quit" ; do
if (( REPLY == 1 + ${#options[@]} )) ; then
echo -e "${MYSQL_DUMPS_LBLUE}Bye bye!${MYSQL_DUMPS_NC}"
return
elif (( REPLY > 0 && REPLY <= ${#options[@]} )) ; then
rm ${MYSQL_DUMPS_DIR}/${file};
break
else
echo -e "${MYSQL_DUMPS_RED}This is not an option.${MYSQL_DUMPS_NC}"
fi
done
if [ $? -eq 0 ]; then
echo -e "The dump ${MYSQL_DUMPS_CYAN}${file}${MYSQL_DUMPS_NC} was successfully deleted."
fi
}