-
Notifications
You must be signed in to change notification settings - Fork 12
/
mongosback.sh
executable file
·308 lines (270 loc) · 8.96 KB
/
mongosback.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/bin/bash
## \ /.| _||_ ._|_
## \/\/ ||(_||_)| |
################################################
# mongosback - Russ Thompson (russ @ linux.com)
set -o errtrace
while getopts f: OPT
do
case $OPT in
f) CONFIG="$OPTARG"
esac
done
if [ ! -z "$CONFIG" ] ; then
. $CONFIG
elif [ -f "mongosback.conf" ]; then
. ./mongosback.conf
else
logger -s "mongosback unable to read the configuration file, exiting prematurely"
exit 1
fi
# Check that IOnice is available, echo to stdout if not.
IO_NICE=`which ionice`
if [ -z "$IO_NICE" ] ; then
echo "WARNING: ionice binary not found, ensure performance throttling set to normal"
fi
function log {
echo -e "$(date +%D" "%T) $1" >> $LOG_FILE
if [ $SYSLOG -eq 1 ] ; then
logger "mongosback - $1"
fi
}
function error_trap {
LINE_RETURN=$1
FUNC_RETURN=$2
ERROR_RETURN=$3
COMMAND_RETURN=$4
logger "mongosback - error in function - $FUNC_RETURN - line $LINE_RETURN - error $ERROR_RETURN"
echo -e "$(date +%D" "%T) ERROR: $FUNC_RETURN - line: $LINE_RETURN - code: $ERROR_RETURN - command: $BASH_COMMAND" | tee -a $LOG_FILE
if [ -f "$PID_FILE" ] ; then rm -f $PID_FILE ; fi
exit 1
}
if [ -f $PID_FILE ] ; then
ERR_RETURN="pid existance : $LINENO"
logger "mongosback - exiting prematurely, pid file exists @ $PID_FILE"
echo -e "mongosback - exiting prematurely, pid file exists @ $PID_FILE" | tee -a $LOG_FILE
error_trap
fi
function do_compress {
if [ $COMPRESS -eq "1" ] ; then
log "starting compression function..."
if [ -f "$COMPRESSED_NAME" ] ; then
$PERFORMANCE_THROTTLING gzip -f $COMPRESSION_LEVEL $COMPRESSED_NAME
COMPRESSED_NAME="$COMPRESSED_NAME.gz"
else
log "ERROR-> $DO_BACKUP does not exst."
error_trap
fi
fi
}
function do_archive {
if [ $DAYS_TO_KEEP -gt "1" ] ; then
log "performing archiving functions..."
TO_DELETE=`find $BACKUP_PATH -name "*dump.tar*" -type f -mtime +$DAYS_TO_KEEP`
if [ ! -z "$TO_DELETE" ] ; then
log "deleting files older than $DAYS_TO_KEEP days:"
find $BACKUP_PATH -name "*dump.tar*" -type f -mtime +$DAYS_TO_KEEP -exec du -hs {} \; >> $LOG_FILE
find $BACKUP_PATH -name "*dump.tar*" -type f -mtime +$DAYS_TO_KEEP -exec rm {} \;
fi
else
find $BACKUP_PATH -name "dump.tar" -exec rm {} \;
fi
}
function select_slave {
log "trying to select slave for backup..."
}
function prepare_job {
log "mongosback - ALIVE - preparing mongo backup job..."
if [ $COMPRESSION_LEVEL == "fast" ] ; then
COMPRESSION_LEVEL="-1"
elif [ $COMPRESSION_LEVEL == "normal" ] ; then
COMPRESSION_LEVEL="-5"
elif [ $COMPRESSION_LEVEL == "best" ] ; then
COMPRESSION_LEVEL="-9"
else
COMPRESSION_LEVEL="-5"
fi
if [ $PERFORMANCE_THROTTLING == "idle" ] ; then
PERFORMANCE_THROTTLING="ionice -c3 nice -n 15"
elif [ $PERFORMANCE_THROTTLING == "low" ] ; then
PERFORMANCE_THROTTLING="ionice -c2 -n7 nice -n 5"
elif [ $PERFORMANCE_THROTTLING == "normal" ] ; then
PERFORMANCE_THROTTLING="nice -n 0"
elif [ $PERFORMANCE_THROTTLING == "high" ] ; then
PERFORMANCE_THROTTLING="ionice -c2 -n1 nice -n -5"
else
PERFORMANCE_THROTTLING="nice -n 0"
fi
if [ $COMPRESS -eq "0" ] ; then
COMPRESSED_NAME=`$(echo $COMPRESSED_NAME | sed 's/.gz//g')`
fi
if [ -z "$MONGO_DUMP" ] ; then
MONGO_DUMP=`which mongodump`
fi
if [ -n "$MONGO_USER" ] ; then
MONGO_DUMP_OPTIONS="$(echo $MONGO_DUMP_OPTIONS) -u $MONGO_USER"
fi
if [ -n "$MONGO_PASS" ] ; then
MONGO_DUMP_OPTIONS="$(echo $MONGO_DUMP_OPTIONS) -p $MONGO_PASS"
fi
if [ -z "$DO_BACKUP" ] ; then
DO_BACKUP="full"
fi
COMPRESSED_NAME="$(echo $DO_BACKUP)_$(date +%m_%d_%Y_%H-%M-%S)_dump.tar"
}
function rawd_backup {
log "performing raw backup"
}
function perform_backup {
lock_writes
cd $BACKUP_PATH &> /dev/null
if [ "$DO_BACKUP" == "full" ] ; then
cd $BACKUP_PATH
log "starting mongodump (this will take a while)...."
$PERFORMANCE_THROTTLING $MONGO_DUMP -h $MONGO_HOST_PORT $MONGO_DUMP_OPTIONS &> /dev/null ; RETURN=$?
if [ $RETURN -ne 0 ] ; then
log "ERROR -> mongo_dump failed to execute properly: err $?"
log "mongosback terminating early"
error_trap
fi
if [ $SIMPLE_BACKUP = "0" ] ; then
$PERFORMANCE_THROTTLING tar --remove-files -cf $COMPRESSED_NAME dump/
fi
else
log "starting mongodump (this will take a while)...."
$PERFORMANCE_THROTTLING $MONGO_DUMP -h $MONGO_HOST_PORT -d $DO_BACKUP -o $BACKUP_PATH $MONGO_DUMP_OPTIONS &> /dev/null ; RETURN=$?
if [ $RETURN -ne 0 ] ; then
log "ERROR -> mongo_dump failed to execute properly: err $?"
log "mongosback terminating early"
error_trap
fi
if [ $SIMPLE_BACKUP = "0" ] ; then
$PERFORMANCE_THROTTLING tar --remove-files -cf $COMPRESSED_NAME $DO_BACKUP
fi
fi
log "mongodump successful, beginning post-dump operations"
unlock_writes
}
function lock_writes {
if [ $LOCK_WRITES -eq "1" ] ; then
if [ -z "$MONGO_USER" ] || [ -z "$MONGO_PASS" ] ; then
LOCKER=`mongo admin --host $MONGO_HOST_PORT --eval "printjson(db.fsyncLock())" | grep "ok" | cut -d":" -f2`
if [ "$LOCKER" -ne "1" ] ; then
log "error encountered during lock function"
fi
else
if [ -n "$MONGO_USER" ] ; then OPT_ARG="-u $MONGO_USER" ; fi
if [ -n "$MONGO_PASS" ] ; then OPT_ARG="$(echo $OPT_ARG) -p $MONGO_PASS" ; fi
echo "db.fsyncLock()" | mongo admin --host $MONGO_HOST_PORT $OPT_ARG
LOCKER=`mongo admin --host $MONGO_HOST_PORT $OPT_ARG --eval "printjson(db.fsyncLock())" | grep "ok" | cut -d":" -f2`
if [ "$LOCKER" -ne "1" ] ; then
log "error encountered during lock function"
fi
fi
fi
}
function unlock_writes {
if [ $LOCK_WRITES -eq "1" ] ; then
if [ -z "$MONGO_USER" ] || [ -z "$MONGO_PASS" ] ; then
UNLOCKER=`mongo admin --host $MONGO_HOST_PORT --eval "printjson(db.fsyncUnlock())"`
else
if [ -n "$MONGO_USER" ] ; then OPT_ARG="-u $MONGO_USER" ; fi
if [ -n "$MONGO_PASS" ] ; then OPT_ARG="$(echo $OPT_ARG) -p $MONGO_PASS" ; fi
echo "db.fsyncLock()" | mongo --host $MONGO_HOST_PORT $OPT_ARG
UNLOCKER=`mongo admin --host $MONGO_HOST_PORT $OPT_ARG --eval "printjson(db.fsyncUnlock())"`
fi
fi
}
function s3_export {
if [ $S3_EXPORT == "1" ] ; then
log "performing S3 export to bucket: $S3_BUCKET_NAME"
s3cmd put $COMPRESSED_NAME s3://$S3_BUCKET_NAME/$S3_BUCKET_PATH/$COMPRESSED_NAME
fi
}
function ftp_export {
if [ $FTP_EXPORT == "1" ] ; then
log "performing FTP export functions..."
ftp -n $FTP_HOST << EOF
quote USER "$FTP_USER"
quote PASS "$FTP_PASS"
cd $FTP_PATH
put $COMPRESSED_NAME
quit
EOF
if [ $? -eq "1" ] ; then
log "ERROR-> during ftp upload"
error_trap
fi
fi
}
function scp_export {
if [ $SCP_EXPORT == "1" ] ; then
log "performing SCP export functions..."
scp -P $SCP_PORT $COMPRESSED_NAME $SCP_USER@$SCP_HOST:$SCP_PATH
fi
}
function send_mail {
if [ $EMAIL_REPORT -eq "1" ] ; then
log "sending email notifications..."
MB_HOST=`echo $MONGO_HOST_PORT | awk -F":" '{print $1}'`
echo "backup time: $TIME_DIFF minutes" >> mail.tmp
echo -e "disk usage: (size : usage : avail : percent : parent) \n $(df -h $BACKUP_PATH | tail -n 1)" >> mail.tmp
if [ $SIMPLE_BACKUP -eq "0" ] ; then
MB_ALL=`find $COMPRESS_STORE -name "*dump.tar*" -type f -exec du -hs {} \; | sort -n`
MB_SIZE=`du -hs $COMPRESSED_NAME | awk '{print $1}'`
echo "backup host: $MB_HOST" >> mail.tmp
echo "backup size: $MB_SIZE" >> mail.tmp
echo -e "Backups on hand\n$MB_ALL" >> mail.tmp
else
MB_SIZE=`du -hs dump/ | awk '{print $1}'`
fi
EMAIL_SUBJECT="mongosback - success - host: $MB_HOST : size: $MB_SIZE : runtime: $TIME_DIFF minutes"
MAILER="$(which mail)"
if [ ! -f "$MAILER" ] ; then
MAILER="$(whereis -b mail | awk '{print $2}')"
fi
$MAILER -s "$EMAIL_SUBJECT" "$EMAIL_ADDRESS" < mail.tmp
if [ $? -eq "0" ] ; then
log "email notification successfully sent..."
fi
rm -f mail.tmp
fi
}
function finish_up {
END_TIME=$(date +%s)
TIME_DIFF=$(( $END_TIME - $START_TIME ))
TIME_DIFF=`echo $(($TIME_DIFF / 60 ))`
log "finished mongodump in $TIME_DIFF minutes..."
send_mail
rm -f $PID_FILE
exit 0
}
# Create some separation in the log for easier reading.
echo "--------------------------------------------------------------------------------" >> $LOG_FILE
# Define error traps
trap 'error_trap $LINENO $FUNCNAME $? $BASH_COMMAND' ERR SIGHUP SIGINT SIGTERM
echo "$$" > $PID_FILE
START_TIME=$(date +%s)
if [ $LVM_SNAPSHOTS = "1" ] ; then
prepare_job
fi
if [ $RAW_BACKUP = "1" ] ; then
prepare_job
rawd_backup
fi
if [ $SIMPLE_BACKUP = "0" ] ; then
prepare_job
perform_backup
do_compress
do_archive
ftp_export
s3_export
scp_export
finish_up
else
prepare_job
perform_backup
ftp_export
scp_export
finish_up
fi