-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongoBackup.sh
executable file
·290 lines (247 loc) · 7.19 KB
/
mongoBackup.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
#!/bin/bash
OPTS=$(getopt -o vc: --long verbose,config: -n 'parse-options' -- "$@")
getOptsExitCode=$?
if [ $getOptsExitCode != 0 ]; then
echo "Failed parsing options." >&2 ;
exit 1 ;
fi
eval set -- "$OPTS"
#localCopy=1
#localCopyPath="/var/tmp"
#localCopyDays=1
#localBackupDays=$(date +%Y%m%d%H%M -d "$localCopyDays day ago")
verbose=0
yearCopy=0
while true; do
case "$1" in
-c | --config ) configFile="$2"; shift ;;
-v | --verbose ) verbose=1; shift ;;
-y | --year-copy ) yearCopy=1; shift ;;
-- ) shift; break ;;
* ) break ;;
esac
done
dateTs=$(date +%s)
ownScriptName=$(basename "$0" | sed -e 's/.sh$//g')
scriptLog="/var/log/$ownScriptName.log"
nagiosLog="/var/log/$ownScriptName.nagios"
lastRun="/var/log/$ownScriptName.last"
function logPrint() {
logMessage=$1
if [ -z "$2" ]; then
nagios=0
else
if [[ $2 =~ ^[0-1]{1}$ ]]; then
nagios=$2
else
nagios=0
fi
fi
if [ -z "$3" ]; then
exitCommand=0
else
if [[ $3 =~ ^[0-1]{1}$ ]]; then
exitCommand=$3
else
exitCommand=0
fi
fi
currentDate=$(date)
echo "$currentDate $logMessage" >> "$scriptLog"
if [ $verbose -eq 1 ]; then
echo "$logMessage"
fi
if [ $nagios -eq 1 ]; then
echo "$logMessage" >> "$nagiosLog"
fi
if [ $exitCommand -eq 1 ]; then
exit
fi
}
function decodeBase64() {
val=$(echo "${1}" | base64 --decode | jq -r "${2}")
echo "$val"
}
function addDirectorySlash(){
echo "$1" | grep -qE "/$"
checkSlash=$?
if [ "$checkSlash" == 0 ]
then
echo "$1"
else
echo "$1/"
fi
}
########################################################
logPrint START 0 0
if [ -f "$nagiosLog" ]; then
logPrint "ERROR file $nagiosLog exists EXIT!" 1 1
else
echo $$ > "$nagiosLog"
fi
hash jq 2>/dev/null
jqCheck=$?
if [ $jqCheck -ne 0 ]
then
rm -f "$nagiosLog"
logPrint "ERROR jq not found!" 1 1
fi
if [ -f "$configFile" ]
then
logPrint "INFO config file $configFile found" 0 0
else
rm -f "$nagiosLog"
logPrint "ERROR config file $configFile not found" 1 1
fi
############ validate JSON ############
validateConfig=$(jq -r type < "$configFile")
if [ "$validateConfig" == "object" ]
then
logPrint "INFO config file is correct" 0 0
else
logPrint "ERROR config file $configFile is not correct" 1 1
fi
############ validate JSON ############
############ variables ############
mongoHost=$(jq -r .mongo_host "$configFile")
dstDirString=$(jq -r .dstDirBase "$configFile")
dstDirBase=$(addDirectorySlash "$dstDirString")
serverName=$(jq -r .hostname "$configFile")
# Speed is in bytes per second. 0 - means unlimited
keepRemoteBackupDays=5
remoteBackupDays=$(date +%Y%m%d%H%M -d "$keepRemoteBackupDays day ago")
oneYearAgo=$(date +%Y%m%d%H%M -d "1 year ago")
if [ -d "$dstDirBase" ]
then
logPrint "INFO dst dir found $dstDirBase" 0 0
else
logPrint "ERROR dst dir not found $dstDirBase. param dstDirBase missing from $configFile" 1 1
fi
mongoDir=$(addDirectorySlash "$dstDirBase$serverName")
currentBackupDir="$mongoDir$(date +%Y%m%d%H%M)"
if [ $verbose == 0 ]
then
mongodumpBin="/usr/bin/mongodump --quiet"
else
mongodumpBin="/usr/bin/mongodump"
fi
############ variables ############
############ FTP Connect ############
ftpHost=$(jq -r .ftp.ftp_host "$configFile")
ftpUser=$(jq -r .ftp.ftp_user "$configFile")
ftpPass=$(jq -r .ftp.ftp_pass "$configFile")
hash ftp 2>/dev/null
ftpCheck=$?
if [ $ftpCheck -ne 0 ]
then
rm -f "$nagiosLog"
logPrint "ERROR ftp not found!" 1 1
fi
echo 'exit' | ftp ftp://"$ftpUser":"$ftpPass"@"$ftpHost"/
checkFTP=$?
if [ $checkFTP -ne 0 ]
then
logPrint "ERROR Failed to connect to ftp host" 0 1
else
logPrint "INFO connected to ftp host" 0 0
fi
############ FTP Connect ############
if mkdir -p "$currentBackupDir"
then
logPrint "INFO directory $currentBackupDir was created" 0 0
else
logPrint "ERROR in directory create $currentBackupDir" 1 1
fi
tmpArr=$(jq -c .mongo "$configFile")
for row in $(echo "$tmpArr" | jq -r '.[] | @base64')
do
dbName=$(decodeBase64 "$row" '.db')
dbUser=$(decodeBase64 "$row" '.user')
dbPass=$(decodeBase64 "$row" '.pass')
dumpCommand="$mongodumpBin --host $mongoHost --port 27017 -u $dbUser -p $dbPass --db $dbName --gzip --out $currentBackupDir"
$dumpCommand
if [ $? == 0 ]
then
logPrint "INFO backup successful $mongoHost user $dbUser db $dbName" 0 0
else
logPrint "ERROR in backup $mongoHost user $dbUser db $dbName" 1 0
fi
done
########################################################
###################### ftp transfer ####################
hash lftp 2>/dev/null
lftpCheck=$?
if [ $lftpCheck -ne 0 ]
then
rm -f "$nagiosLog"
logPrint "ERROR lftp not found!" 1 1
fi
logPrint "start ftp transfer" 0 0
lftp -u "$ftpUser":"$ftpPass" "$ftpHost" -e "mirror -R $currentBackupDir $serverName/; bye"
checkUploadExit=$?
if [ $checkUploadExit -ne 0 ]; then
logPrint "ERROR in upload" 1 1
else
logPrint "ftp transfer finished successfully $currentBackupDir" 0 0
rm -rf "$currentBackupDir"
fi
for currentRemoteDirectory in $(curl -s -u "$ftpUser":"$ftpPass" ftp://"$ftpHost"/"$serverName"/ -X MLSD | grep 'type=dir' | cut -d';' -f8)
do
if [ $yearCopy -eq 0 ];
then
logPrint "Week Copy" 0 0
if [[ $currentRemoteDirectory =~ ^[0-9]{12}$ ]]; then
if [ "$currentRemoteDirectory" -lt "$remoteBackupDays" ]; then
if [ ! -z "$currentRemoteDirectory" ]; then
logPrint "remove $serverName/$currentRemoteDirectory" 0 0
lftp -u "$ftpUser":"$ftpPass" "$ftpHost" -e "rm -r $serverName/$currentRemoteDirectory; bye"
checkRemoteRemove=$?
if [ $checkRemoteRemove -ne 0 ]; then
logPrint "ERROR could not remove remote directory $serverName/$currentRemoteDirectory" 1 0
fi
fi
fi
fi
else
logPrint "Year Copy"
if [[ $currentRemoteDirectory =~ ^[0-9]{12}$ ]]
then
dateInDirectory=$(echo "$currentRemoteDirectory" | cut -c7-8)
logPrint "$currentRemoteDirectory $oneYearAgo $dateInDirectory"
if [ "$currentRemoteDirectory" -lt "$oneYearAgo" ]
then
logPrint "One year" 0 0
if [ ! -z "$currentRemoteDirectory" ]; then
logPrint "remove $serverName/$currentRemoteDirectory" 0 0
lftp -u "$ftpUser":"$ftpPass" "$ftpHost" -e "rm -r $serverName/$currentRemoteDirectory; bye"
checkRemoteRemove=$?
if [ $checkRemoteRemove -ne 0 ]
then
logPrint "ERROR could not remove remote directory $serverName/$currentRemoteDirectory" 1 0
fi
fi
continue
fi
if [ "$currentRemoteDirectory" -lt "$remoteBackupDays" ] && [ "$dateInDirectory" -ne 01 ]
then
logPrint "local config days"
if [ ! -z "$currentRemoteDirectory" ]; then
logPrint "remove $serverName/$currentRemoteDirectory" 0 0
lftp -u "$ftpUser":"$ftpPass" "$ftpHost" -e "rm -r $serverName/$currentRemoteDirectory; bye"
checkRemoteRemove=$?
if [ $checkRemoteRemove -ne 0 ]; then
logPrint "ERROR could not remove remote directory $serverName/$currentRemoteDirectory" 1 0
fi
fi
fi
fi
fi
done
###################### ftp transfer ####################
if grep -Fq "ERROR" "$nagiosLog" ; then
logPrint "ERRORS are found. Must not remove $nagiosLog" 0 0
else
rm -f "$nagiosLog"
logPrint "FINISH" 0 0
fi
echo "$dateTs" > "$lastRun"