-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongoctrl
executable file
·133 lines (129 loc) · 2.99 KB
/
mongoctrl
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
#!/usr/bin/env bash
#----------------------------
# Usage
function usage {
cat <<EOF
Usage:
mongoctrl <operation>
Operations:
help : This message.
start : Start mongod
stop : Stop mongod
Examples:
$ mongoctrl start
$ mongoctrl stop
EOF
exit $1;
}
#----------------------------
# Arguments
if [ "${MONGO_HOME}" = "" ]; then
MONGO_HOME=`dirname $0`
fi
OP=$1
#----------------------------
# Host specialized
if [ "$MONGO_CONF" = "" ]; then
MONGO_CONF=${MONGO_HOME}/conf/mongod.conf
fi
PIDFILE=`grep pidfilepath ${MONGO_CONF} | sed -e 's/^ *pidfilepath *= *//'`
MONGOD=${MONGO_HOME}/bin/mongod
MONGOD_IP=`grep bind_ip ${MONGO_CONF} | sed -e 's/^ *bind_ip *= *//' | tr -d [:space:] `
#MONGOD_IP=127.0.0.1
MONGOD_PORT=`grep port ${MONGO_CONF} | sed -e 's/^ *port *= *//'`
MONGO=${MONGO_HOME}/bin/mongo
MONGO_DATA=`grep dbpath ${MONGO_CONF} | sed -e 's/^ *dbpath *= *//'`
MONGO_LOCK=${MONGO_DATA}/mongod.lock
MONGODUMP=${MONGO_HOME}/bin/mongodump
#----------------------------
# Check
function check () {
if [ -f ${PIDFILE} ]; then
ps vp `cat ${PIDFILE}` | grep mongod > /dev/null 2>&1
if [ $? = 0 ]; then
cat ${PIDFILE} | xargs echo "mongod process running... "
return 0;
fi
cat ${PIDFILE} | xargs echo "mongod process not running... "
fi
echo "mongod process not running... "
return 1;
}
#----------------------------
# Operation dispatcher. ( help /start / stop / rotate / status / locate )
case ${OP} in
help)
usage 0
;;
start)
check
if [ $? = 0 ];then
echo mongod already alived !
exit 0;
fi
${MONGOD} --config ${MONGO_CONF}
exit $?
;;
stop)
check
if [ $? = 1 ];then
echo mongod not alived !
exit 0;
fi
kill -15 `cat ${PIDFILE}`
if [ $? != 0 ]; then
return 1;
fi
# wait 30 sec
for i in {1..30}
do
check;
if [ $? = 1 ] ;then
exit 0;
fi
sleep 1;
done
echo "process cannot terminate ! so send kill -9 !! "
kill -9 `cat $PIDFILE`
exit $?
;;
rotate)
check
if [ $? = 1 ];then
echo mongod not alived !
exit 0;
fi
kill -SIGUSR1 `cat ${PIDFILE}`
;;
backup)
check
if [ $? = 1 ];then
echo mongod not alived !
exit 0;
fi
DUMP_NAME=dump.`date +'%Y%m%d-%H%M%S'`
DUMP_DIR=${MONGO_HOME}/backup/${DUMP_NAME}
${MONGODUMP} --directoryperdb -o ${DUMP_DIR}
mkdir -p ${MONGO_HOME}/backup/
cd ${MONGO_HOME}/backup/
tar czvf ${DUMP_DIR}.tar.gz ${DUMP_NAME}
rm -rf ${DUMP_DIR}
;;
status)
${MONGO} ${MONGOD_IP}:${MONGOD_PORT} <<<'rs.status()'
exit $?
;;
run)
${MONGO} ${MONGOD_IP}:${MONGOD_PORT} < $2
exit $?
;;
locate)
echo ${MONGOD_IP}:${MONGOD_PORT}
exit $?
;;
*)
echo "USAGE : (help|start|repair|stop|rotate|status|locate)"
exit 1;
;;
esac
exit $?