-
Notifications
You must be signed in to change notification settings - Fork 1
/
mysql_drop_databases.sh
59 lines (47 loc) · 1.17 KB
/
mysql_drop_databases.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
# -----------------------------------------------------------------------------
# Name : mysql_drop_databases.sh
# Description : drop mysql databases
# Author : qiaolei
# Date : 2014/10/13
# -----------------------------------------------------------------------------
#!/bin/sh
usage()
{
echo "Usage: $0 <username> <password> <databases>"
echo "databases are seperated by comma(,), such as db1,db2,db3"
}
if [ $# -ne 3 ]
then
usage
exit 1
fi
LOG_FILE=/tmp/mysql_scripts.log
if [ ! -f $LOG_FILE ];then
touch $LOG_FILE
fi
TIMESTAMP=`date +%Y%m%d%H%M%S`
echo "@$TIMESTAMP: {$0 $*}" | tee -a $LOG_FILE
USERNAME=$1
PASSWORD=$2
DATABASES=($(echo $3 | tr ',' ' ' | tr -s ' '))
NUM_OF_DBS=${#DATABASES[@]}
for ((i=0;i<$NUM_OF_DBS;i++));do
echo "dropping database ${DATABASES[$i]}..." | tee -a $LOG_FILE
if [ ! $PASSWORD = "NULL" ];then
mysqladmin -f -u $USERNAME -p$PASSWORD drop ${DATABASES[$i]}
else
mysqladmin -f -u $USERNAME drop ${DATABASES[$i]}
fi
done
RC=$?
CODE=-1
MSG=""
if [ $RC -eq 0 ];then
CODE=$RC
MSG="OK"
else
CODE=$RC
MSG="KO"
fi
RESULT=`printf "%s%s%s%s%s%s%s" "{" "\"result\":" "\"$CODE\"" "," "\"msg\":" "\"$MSG\"" "}"`
echo $RESULT