-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmongodb
128 lines (106 loc) · 3.36 KB
/
mongodb
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
# -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
#
# mongodb handler script for backupninja
#
getconf tar /bin/tar
getconf mongodump /usr/bin/mongodump
getconf backupdir /var/backups/mongodb
getconf dumpopts ""
getconf dbhost localhost
getconf dbport 27017
getconf mongouser
getconf mongopass
getconf mongodb
getconf mongocollection
getconf vsname
# Decide if the handler should operate on a vserver or on the host.
# In the former case, check that $vsname exists and is running.
local usevserver=no
local vroot
if [ $vservers_are_available = yes ]; then
if [ -n "$vsname" ]; then
# does it exist ?
if ! vservers_exist "$vsname" ; then
fatal "The vserver given in vsname ($vsname) does not exist."
fi
# is it running ?
vservers_running $vsname || fatal "The vserver $vsname is not running."
# everything ok
info "Using vserver '$vsname'."
usevserver=yes
vroot="$VROOTDIR/$vsname"
else
info "No vserver name specified, actions will be performed on the host."
fi
fi
# create backup dirs, $vroot will be empty if no vsname was specified
# and we will instead proceed to operate on the host
[ -d $vroot$backupdir ] || mkdir -p $vroot$backupdir
[ -d $vroot$backupdir ] || fatal "Backup directory is '$vroot$backupdir'"
#######################################################################
## dumping mongodb
if [ ! -f $vroot$mongodump ]; then
fatal "$vroot$mongodump does not exists"
fi
if [ "$mongouser" -a "$mongopass" ]
then
info "using $mongouser for authentication"
AUTH="-u $mongouser -p $mongopass"
fi
if [ -n "$mongodb" ]
then
info "only dumping db $mongodb"
DB="-d $mongodb"
fi
if [ -n "$mongocollection" ]
then
info "only dumping collection $mongocollection"
COLLECTION="-c $mongocollection"
fi
if [ -n "$dumpopts" ]; then
EXTRA_OPTIONS="$dumpopts"
fi
if [ $usevserver = yes ]
then
info "dbhost: $dbhost, dbport: $dbport"
if [ ! -d $vroot$backupdir ]; then
fatal "$vroot$backupdir does not exists"
fi
if [ ! -w $vroot$backupdir ]; then
fatal "$vroot$backupdir is not writable"
fi
execdump="$VSERVER $vsname exec $mongodump -h $dbhost:$dbport -o $backupdir $AUTH $EXTRA_OPTIONS $DB $COLLECTION"
execrm="$VSERVER $vsname exec rm -rf $backupdir/*"
else
if [ ! -d $backupdir ]; then
fatal "$backupdir does not exists"
fi
if [ ! -w $backupdir ]; then
fatal "$backupdir is not writable"
fi
execdump="$mongodump -h $dbhost:$dbport -o $backupdir $AUTH $EXTRA_OPTIONS $DB $COLLECTION"
execrm="rm -rf $backupdir/*"
fi
if [ ! $test ]; then
outputrm=`su $user -c "$execrm" 2>&1`
coderm=$?
if [ "$coderm" == "0" ]; then
debug $outputrm
info "Successfully removed old dump (if exist)"
else
warning $outputrm
warning "Failed to remove old dump"
fi
outputdump=`su $user -c "$execdump" 2>&1`
codedump=$?
if [ "$codedump" == "0" ]; then
debug $outputdump
info "Successfully finished dump of all mongodb databases"
else
fatal $outputdump
fatal "Failed to dumping of mongodb databases"
fi
else
debug "Running backup of mongodb"
fi
return 0