-
Notifications
You must be signed in to change notification settings - Fork 1
/
mysql_backup.sh
64 lines (51 loc) · 1.34 KB
/
mysql_backup.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
# -----------------------------------------------------------------------------
# Name : mysql_backup.sh
# Description : backup the specified database
# 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[@]}
BACKUP_DIR=/var/lib/mysql/auto_backup
if [ ! -d $BACKUP_DIR ];then
mkdir -p $BACKUP_DIR
fi
for ((i=0;i<$NUM_OF_DBS;i++));do
echo "backing up databse ${DATABASES[$i]}..." | tee -a $LOG_FILE
if [ ! $PASSWORD = "NULL" ];then
mysqldump -u $USERNAME -p$PASSWORD ${DATABASES[$i]} > ${BACKUP_DIR}/${DATABASES[$i]}-${TIMESTAMP}.sql
else
mysqldump -u $USERNAME ${DATABASES[$i]} > ${BACKUP_DIR}/${DATABASES[i]}-${TIMESTAMP}.sql
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