-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.sh
executable file
·45 lines (37 loc) · 828 Bytes
/
db.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
#!/bin/bash
USER_NAME="root"
PASSWORD="kukid123#"
HOST="localhost"
BACKUP_PATH="/tmp/shenma_db_backup"
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
GZIP="$(which gzip)"
GUNZIP="$(which gunzip)"
function log {
echo "****** $1"
echo ""
}
function backup_db {
rm -rf $BACKUP_PATH
mkdir -p $BACKUP_PATH
log "Start to backup '$1' db."
FILE="$BACKUP_PATH/$1_backup.gz"
set_hostname $2
$MYSQLDUMP -h $HOST -u $USER_NAME -p$PASSWORD $1 | $GZIP -9 > $FILE
log "finished to backup db in $FILE"
}
function restore_db {
FILE="$BACKUP_PATH/$1_backup.gz"
set_hostname $2
log "Will restore db from '$FILE'"
$GUNZIP < $FILE | $MYSQL -h $HOST -u $USER_NAME -p$PASSWORD $1
log "finished to restore db."
}
function set_hostname {
if [ -n "$1" ]; then
HOST=$1
fi
}
if [ -n "$1" ]; then
eval $1
fi