-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongobr.sh
198 lines (166 loc) · 5.12 KB
/
mongobr.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
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
#!/usr/bin/env bash
# This script will backup and restore a mongo db
set -e
# Variables
ACTION=""
SHARD1=1
SHARD2=1
SHARD3=1
CONFIG=1
CONSTELLATION=0
# Functions
function show_help {
echo "Usage: mongobr.sh [<args>]"
echo " -h\-? Show help"
echo " -a Action to perform e.g -a backup | -a restore"
echo " -o Backup store path e.g -o /tmp/dump"
echo " -s Backup or Restore will be performed in a star. Default is constellation"
echo " -1 Restore only shard 1 db"
echo " -2 Restore only shard 2 db"
echo " -3 Restore only shard 3 db"
echo " -c Restore only config db"
echo " -x Restore all shards and config dbs"
}
function start_balancer {
echo "=> Start mongo balancer"
mongo --port 27017 --quiet --eval "sh.setBalancerState(true)"
}
function stop_balancer {
echo "=> Stop mongo balancer"
mongo --port 27017 --quiet --eval "sh.stopBalancer()"
}
function restore_star {
echo "=> Restore star mongo db"
mongorestore --quiet --gzip $OUTPUT/
}
function restore_shard {
shard_to_restore=$1
shard_name=$2
shard_host=$3
echo "=> Restore shard: $shard_host"
mongorestore --gzip --oplogReplay --host $shard_host $OUTPUT/$shard_name
}
function restore_config {
config=$1
echo "=> Restore config: $config"
# BUG: Failed: config.version: error dropping collection: cannot drop config.version document while in --configsvr mode
# https://jira.mongodb.org/browse/SERVER-28796.
mongorestore --drop --gzip --nsExclude=config.version --host $config $OUTPUT/config
#mongorestore --gzip --host $config $OUTPUT/config
}
function backup_star {
echo "=> Create backup"
mongodump --gzip -o $OUTPUT/
}
function backup_constellation {
echo "=> Get shard 1 host"
shard1=$(mongo --port 27017 --quiet --eval "db.adminCommand({ listShards: 1 }).shards[0]['host']")
echo "=> Shard 1: $shard1"
echo "=> Create shard 1 backup"
mongodump --host $shard1 --oplog --gzip -o $OUTPUT/shard1
echo "=> Get shard 2 host"
shard2=$(mongo --port 27017 --quiet --eval "db.adminCommand({ listShards: 1 }).shards[1]['host']")
echo "=> Shard 2: $shard2"
echo "=> Create shard 2 backup"
mongodump --host $shard2 --oplog --gzip -o $OUTPUT/shard2
echo "=> Get shard 3 host"
shard3=$(mongo --port 27017 --quiet --eval "db.adminCommand({ listShards: 1 }).shards[2]['host']")
echo "=> Shard 3: $shard3"
echo "=> Create shard 3 backup"
mongodump --host $shard3 --oplog --gzip -o $OUTPUT/shard3
echo "=> Get config host"
config=$(mongo --port 27017 --quiet --eval "db.serverStatus().sharding.configsvrConnectionString")
echo "=> Config: $config"
mongodump --host $config --oplog --gzip -o $OUTPUT/config
}
# Arguments
while getopts "h?x123ca:o:" opt; do
case "$opt" in
h|\?)
show_help
exit 0
;;
a)
ACTION=$OPTARG
;;
o)
OUTPUT=$OPTARG
;;
1)
SHARD1=0
;;
2)
SHARD2=0
;;
3)
SHARD3=0
;;
c)
CONFIG=0
;;
s)
CONSTELLATION=1
;;
x)
SHARD1=0
SHARD2=0
SHARD3=0
CONFIG=0
;;
esac
done
# Validations
if [[ "$ACTION" != "backup" ]] && [[ "$ACTION" != "restore" ]]; then
echo "=> Error: -a argument is not valid. It must be -a backup or -a restore"
exit 1
fi
if [ ! -d "$OUTPUT" ]; then
echo "=> Error: Invalid directory used with -o"
exit 1
fi
# Check if this is run as root
if [[ $(id -u) -ne 0 ]]; then
echo "=> Error: Script must be executed as root"
exit 1
fi
# Print information
echo "=> Stronglink Version: $(cat /usr/local/radium/etc/VERSION)"
echo "=> Backup Output: $OUTPUT"
# Run backup action
if [[ $ACTION == "backup" ]]; then
if [ $CONSTELLATION -eq 0 ]; then
echo "=> Start constellation backup"
# stop_balancer
backup_constellation
# start_balancer
else
echo "=> Start star backup"
backup_star
fi
elif [[ $ACTION == "restore" ]]; then
if [ $CONSTELLATION -eq 0 ]; then
echo "=> Start constellation restore"
host1=$(mongo --port 27017 --quiet --eval "db.adminCommand({ listShards: 1 }).shards[0]['host']")
host2=$(mongo --port 27017 --quiet --eval "db.adminCommand({ listShards: 1 }).shards[1]['host']")
host3=$(mongo --port 27017 --quiet --eval "db.adminCommand({ listShards: 1 }).shards[2]['host']")
config=$(mongo --port 27017 --quiet --eval "db.serverStatus().sharding.configsvrConnectionString")
systemctl stop mongos
if [ $SHARD1 -eq 0 ]; then
restore_shard 0 "shard1" $host1
fi
if [ $SHARD2 -eq 0 ]; then
restore_shard 1 "shard2" $host2
fi
if [ $SHARD3 -eq 0 ]; then
restore_shard 2 "shard3" $host3
fi
if [ $CONFIG -eq 0 ]; then
restore_config $config
fi
systemctl start mongos
systemctl restart mongod*
else
echo "=> Start star restore"
restore_star
fi
fi