-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzimbraBackup.sh
534 lines (455 loc) · 17.5 KB
/
zimbraBackup.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
#!/bin/sh
###############################
# version 1.1.1
# author Anton Antonov
###############################
localBackupDir="/opt/backup"
ftpHost=10.10.10.10
ftpUser=qwe
ftpPass=qweqwe
# Speed is in bytes per second. 0 - means unlimited
ftpUploadSpeed=0
ftpDownloadSpeed=0
keepLocalCopy=1
keepLocalBackupDays=1
keepRemoteCopy=1
keepRemoteBackupDays=2
verbose=1
###############################
# do not edit below this line
###############################
dateTs=$(date +%s)
ownScriptName=$(basename "$0" | sed -e 's/.sh$//g')
hostname=$(hostname)
scriptLog="/var/log/$ownScriptName.log"
nagiosLog="/var/log/$ownScriptName.nagios"
lastRun="/var/log/$ownScriptName.last"
localBackupDays=$(date +%Y%m%d%H%M%S -d "$keepLocalBackupDays day ago")
remoteBackupDays=$(date +%Y%m%d%H%M%S -d "$keepRemoteBackupDays day ago")
zimbraUser="zimbra"
zimbraZmprov="/opt/zimbra/bin/zmprov"
zimbraZmmailbox="/opt/zimbra/bin/zmmailbox"
zmprovCommand="/bin/sudo -u $zimbraUser $zimbraZmprov"
zmmailboxCommand="/bin/sudo -u $zimbraUser $zimbraZmmailbox"
curlBin="/bin/curl"
lftpBin="/bin/lftp"
currentDate=$(date +%Y%m%d%H%M%S)
currentBackupDir="$localBackupDir/$currentDate"
tmpDir="$currentBackupDir/tmp"
distributinlistMembersDir="$currentBackupDir/distributinlistMembers"
userpassDir="$currentBackupDir/userpass"
userdataDir="$currentBackupDir/userdata"
filtersDir="$currentBackupDir/filters"
calendarDir="$currentBackupDir/calendars"
contactsDir="$currentBackupDir/contacts"
ldapDir="$currentBackupDir/ldap"
tasksDir="$currentBackupDir/tasks"
mailDir="$currentBackupDir/mail"
restoreScriptsDir="$currentBackupDir/restoreScripts"
domainsFile="$currentBackupDir/domains.txt"
adminsFile="$currentBackupDir/admins.txt"
emailsFile="$currentBackupDir/emails.txt"
distributinlistFile="$currentBackupDir/distributinlist.txt"
restoreCreateUsers="$restoreScriptsDir/createUsers.sh"
##############################
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
echo $(date) $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 createDir() {
currentDir=$1
if [ ! -d $currentDir ]; then
logPrint "Directory $currentDir is does NOT exist" 0 0
/bin/sudo -u $zimbraUser mkdir $currentDir
checkMkdirExitStatus=$?
if [ $checkMkdirExitStatus -ne 0 ]; then
logPrint "ERROR Could not create $currentDir" 1 1
else
logPrint "Directory $currentDir created" 0 0
fi
else
logPrint "Directory $currentDir exists" 0 0
/bin/sudo -u $zimbraUser /bin/test -w $currentDir
checkWriteToTmpBackupDir=$?
if [ $checkWriteToTmpBackupDir -ne 0 ]; then
logPrint "user $zimbraUser could not write to $currentDir" 1 1
fi
fi
}
##############################
logPrint START 0 0
if [ -f $nagiosLog ]; then
logPrint "file $nagiosLog exists EXIT!" 1 1
else
echo $$ > $nagiosLog
fi
if [[ $EUID -ne 0 ]]; then
logPrint "This script must be run as root. Current user $EUID" 1 1
else
logPrint "Current UID is $EUID" 0 0
fi
if [ ! -d $localBackupDir ]; then
logPrint "Directory $localBackupDir is does NOT exist" 0 1
else
logPrint "Directory $localBackupDir exists" 0 0
fi
id $zimbraUser > /dev/null 2>&1
checkZimbraUser=$?
if [ $checkZimbraUser -ne 0 ]; then
logPrint "user $zimbraUser does NOT exist" 1 1
fi
/bin/sudo -u $zimbraUser /bin/test -w $localBackupDir
checkWriteToBackupDir=$?
if [ $checkWriteToBackupDir -ne 0 ]; then
logPrint "user $zimbraUser could not write to $localBackupDir" 1 1
fi
if [ ! -e $zimbraZmprov ]; then
logPrint "zmprov not found in $zimbraZmprov" 0 1
else
logPrint "zmprove found $zimbraZmprov" 0 0
fi
if [ ! -e $curlBin ]; then
logPrint "curl not found in $curlBin" 0 1
else
logPrint "curl found $curlBin" 0 0
fi
if [ ! -e $lftpBin ]; then
logPrint "lftp not found in $lftpBin" 0 1
else
logPrint "lftp found $lftpBin" 0 0
fi
createDir $currentBackupDir
createDir $tmpDir
createDir $distributinlistMembersDir
createDir $userpassDir
createDir $userdataDir
createDir $filtersDir
createDir $calendarDir
createDir $contactsDir
createDir $mailDir
createDir $ldapDir
createDir $tasksDir
createDir $restoreScriptsDir
logPrint "create distributinlists file $domainsFile" 0 0
$zmprovCommand gad > $domainsFile
checkStatusGAD=$?
if [ $checkStatusGAD -ne 0 ]; then
logPrint "ERROR could not execute $zmprovCommand gad > $domainsFile" 1 1
fi
logPrint "create distributinlists file $adminsFile" 0 0
$zmprovCommand gaaa > $adminsFile
checkStatusGAAD=$?
if [ $checkStatusGAAD -ne 0 ]; then
logPrint "ERROR could not execute $zmprovCommand gaaa > $adminsFile" 1 1
fi
logPrint "create distributinlists file $emailsFile" 0 0
$zmprovCommand -l gaa > $emailsFile
checkStatusGAA=$?
if [ $checkStatusGAA -ne 0 ]; then
logPrint "ERROR could not execute $zmprovCommand -l gaa > $emailsFile" 1 0
fi
logPrint "create distributinlists file $distributinlistFile" 0 0
$zmprovCommand gadl > $distributinlistFile
checkStatusGADL=$?
if [ $checkStatusGADL -ne 0 ]; then
logPrint "ERROR could not execute $zmprovCommand gadl > $distributinlistFile" 1 0
fi
while read distributionLists
do
logPrint "create distributinlist $distributionLists" 0 0
$zmprovCommand gdlm $distributionLists > $distributinlistMembersDir/$distributionLists.txt
checkStatusGDLM=$?
if [ $checkStatusGDLM -ne 0 ]; then
logPrint "ERROR could not execute $zmprovCommand gdlm > $distributinlistMembersDir/$distributionLists.txt" 1 0
fi
done < $distributinlistFile
while read email
do
logPrint "get ldap for $email" 0 0
$zmprovCommand -l ga $email > $ldapDir/$email.ldif
checkStatusGetLDAP=$?
if [ $checkStatusGetLDAP -ne 0 ]; then
logPrint "ERROR could not execute $zmprovCommand -l ga $email > $ldapDir/$email.ldif" 1 0
fi
done < $emailsFile
while read email
do
logPrint "get user password for $email" 0 0
$zmprovCommand -l ga $email userPassword | grep userPassword: | awk '{ print $2}' > $userpassDir/$email.shadow;
checkStatusGetUserPass=$?
if [ $checkStatusGetUserPass -ne 0 ]; then
logPrint "ERROR could not execute $zmprovCommand -l ga $email userPassword | grep userPassword: | awk \'{ print $2}\' > $userpassDir/$email.shadow" 1 0
fi
done < $emailsFile
while read email
do
logPrint "get user data for $email" 0 0
$zmprovCommand ga $email | grep -i Name: > $userdataDir/$email
checkStatusGetUserData=$?
if [ $checkStatusGetUserData -ne 0 ]; then
logPrint "ERROR could not execute $zmprovCommand ga $email | grep Name: > $userdataDir/$email" 1 0
fi
done < $emailsFile
while read email
do
logPrint "get calendar in tgz for user $email" 0 0
$zmmailboxCommand -z -m $email getRestURL '/Calendar?fmt=tgz' > $calendarDir/$email.tgz
checkStatusGetCalendarTGZ=$?
if [ $checkStatusGetCalendarTGZ -ne 0 ]; then
logPrint "ERROR could not execute $zmmailboxCommand -z -m $email getRestURL /Calendar?fmt=tgz > $calendarDir/$email.tgz" 0 0
fi
logPrint "get calendar in ics for user $email" 0 0
$zmmailboxCommand -z -m $email getRestURL '/Calendar?fmt=ics' > $calendarDir/$email.ics
checkStatusGetCalendarICS=$?
if [ $checkStatusGetCalendarICS -ne 0 ]; then
logPrint "ERROR could not execute $zmmailboxCommand -z -m $email getRestURL /Calendar?fmt=ics > $calendarDir/$email.ics" 0 0
fi
done < $emailsFile
while read email
do
logPrint "get contacts in ics for user $email" 0 0
$zmmailboxCommand -z -m $email getRestURL '/Contacts?fmt=csv' > $contactsDir/$email.csv
checkStatusGetContactsCSV=$?
if [ $checkStatusGetContactsCSV -ne 0 ]; then
logPrint "ERROR could not execute $zmmailboxCommand -z -m $email getRestURL /Calendar?fmt=csv > $calendarDir/$email.csv" 0 0
fi
logPrint "get contacts in tgz for user $email" 0 0
$zmmailboxCommand -z -m $email getRestURL '/Contacts?fmt=tgz' > $contactsDir/$email.tgz
checkStatusGetContactsTGZ=$?
if [ $checkStatusGetContactsTGZ -ne 0 ]; then
logPrint "ERROR could not execute $zmmailboxCommand -z -m $email getRestURL /Calendar?fmt=tgz > $calendarDir/$email.tgz" 0 0
fi
done < $emailsFile
while read email
do
logPrint "get tasks in vcard for user $email" 0 0
$zmmailboxCommand -z -m $email getRestURL '/Tasks' > $tasksDir/$email.vcard
checkStatusGetTasksVCARD=$?
if [ $checkStatusGetTasksVCARD -ne 0 ]; then
logPrint "ERROR could not execute $zmmailboxCommand -z -m $email getRestURL '/Tasks' > $tasksDir/$email.vcard" 0 0
fi
logPrint "get tasks in tgz for user $email" 0 0
$zmmailboxCommand -z -m $email getRestURL '/Tasks?fmt=tgz' > $tasksDir/$email.tgz
checkStatusGetTasksTGZ=$?
if [ $checkStatusGetTasksTGZ -ne 0 ]; then
logPrint "ERROR could not execute $zmmailboxCommand -z -m $email getRestURL /Tasks?fmt=tgz > $tasksDir/$email.tgz" 0 0
fi
done < $emailsFile
while read email
do
logPrint "get filter for $email" 0 0
$zmprovCommand ga $email zimbraMailSieveScript > $tmpDir/$email
sed -i -e "1d" $tmpDir/$email
sed 's/zimbraMailSieveScript: //g' $tmpDir/$email > $filtersDir/$email
done < $emailsFile
while read email
do
logPrint "get mail for $email" 0 0
$zmmailboxCommand -z -m $email getRestURL '/?fmt=tgz' > $mailDir/$email.tgz
checkGetMail=$?
if [ $checkGetMail -ne 0 ]; then
logPrint "ERROR could not execute $zmmailboxCommand -z -m $email getRestURL '/?fmt=tgz' > $mailDir/$email.tgz" 1 0
fi
done < $emailsFile
############# restore scripts ##############
cat <<END >$restoreCreateUsers
#!/bin/bash
currentDir=\$(dirname \$0)
currentBaseDir=\$(echo "\$currentDir" | sed -r 's/restoreScripts//g')
echo \$currentBaseDir
tmpDir="\$currentBaseDir/tmp"
distributinlistMembersDir="\$currentBaseDir/distributinlistMembers"
userpassDir="\$currentBaseDir/userpass"
userdataDir="\$currentBaseDir/userdata"
filtersDir="\$currentBaseDir/filters"
mailDir="\$currentBaseDir/mail"
zimbraUser="zimbra"
zmprovBin="/opt/zimbra/bin/zmprov"
zmprovCommnand="sudo -u \$zimbraUser \$zmprovBin"
zmmailboxBin="/opt/zimbra/bin/zmmailbox"
zmmailboxCommand="sudo -u \$zimbraUser \$zmmailboxBin"
##############################################
function usage() {
echo "Usage: ./createUser.sh --restore=all"
echo "Usage: ./createUser.sh [email protected]"
echo "--restore >>>> restore email or all emails"
exit 1
}
##############################################
for i in "\$@"
do
case \$i in
--restore=*)
restore="\${i#*=}"
shift # past argument=value
;;
*)
# unknown option
;;
esac
done
if [[ -z \$restore ]]; then
echo "ERROR: restore parameter is not set"
usage
fi
emailRegex="^[a-z0-9!#\\$%&'*+/=?^_\\\`{|}~-]+(\.[a-z0-9!#$%&'*+/=?^_\\\`{|}~-]+)*@([a-z0-9]([a-z0-9-]*[a-z0-9])?\.)+[a-z0-9]([a-z0-9-]*[a-z0-9])?\\$"
echo \$restore
if [[ \$restore =~ \$emailRegex ]] || [ \$restore == "all" ]; then
echo "restore seems valid input \$restore"
else
echo "restore seems NOT valid input \$restore"
usage
fi
if [[ \$EUID -ne 0 ]]; then
echo "This script must be run as root. Current user \$EUID"
exit
else
echo "Current UID is \$EUID"
fi
if [ ! -d \$distributinlistMembersDir ] || [ ! -d \$userpassDir ] || [ ! -d \$userdataDir ] || [ ! -d \$filtersDir ] || [ ! -d \$mailDir ]; then
echo "some directories are missing."
echo "check \$distributinlistMembersDir"
echo "check \$userpassDir"
echo "check \$userdataDir"
echo "check \$filtersDir"
echo "check \$mailDir"
exit
fi
if [ \$restore == "all" ]; then
users=\$(<\$currentBaseDir/emails.txt)
else
users=\$restore
grep -qw \$users \$currentBaseDir/emails.txt
checkIfEmail=\$?
if [ \$checkIfEmail -ne 0 ]; then
echo "email \$users does not exist in archive - \$currentBaseDir/emails.txt"
exit
fi
fi
for email in \$users
do
\$zmprovCommnand -l gaa | grep -q \$email
checkIfAccountExists=\$?
if [[ \$checkIfAccountExists -eq 0 ]]; then
echo "Account \$email alredy exists."
else
echo "Account \$email does NOT exit"
givenName=\$(grep givenName: \$userdataDir/\$email | cut -d ":" -f2 | sed 's/ //g')
displayName=\$(grep displayName: \$userdataDir/\$email | cut -d ":" -f2 | sed 's/ //g')
shadowpass=\$(cat \$userpassDir/\$email.shadow)
echo "create account \$email givenName \$givenName displayName \$displayName"
tmpPass="CHANGEme"
\$zmprovCommnand ca \$email CHANGEme cn "\$givenName" displayName "\$displayName" givenName "\$givenName"
checkCreateUser=\$?
if [ \$checkCreateUser -eq 0 ]; then
echo "user \$email created"
else
echo "ERROR could not create \$zmprovCommnand ca \$email CHANGEme cn \$givenName displayName \$displayName givenName \$givenName"
fi
\$zmprovCommnand ma \$email userPassword \$shadowpass
checkPasswordSet=\$?
if [ \$checkPasswordSet -eq 0 ]; then
echo "password set on \$email"
else
echo "ERROR could not set password for user \$email"
fi
echo "restore mail for \$email"
\$zmmailboxCommand -z -m \$email postRestURL "/?fmt=tgz&resolve=skip" \$mailDir/\$email.tgz
checkMailRestore=\$?
if [ \$checkPasswordSet -eq 0 ]; then
echo "mail restored \$email"
else
echo "ERROR could not restore mail for \$email"
fi
fi
done
END
chmod 755 $restoreCreateUsers
############# restore scripts ##############
if [ $keepRemoteCopy -eq 1 ]; then
logPrint "start ftp transfer" 0 0
$lftpBin -u $ftpUser:$ftpPass $ftpHost -e "set net:connection-limit 1; set net:limit-rate $ftpDownloadSpeed:$ftpUploadSpeed; mirror -R $currentBackupDir $hostname/; bye"
checkUploadExit=$?
echo $checkUploadExit
if [ $checkUploadExit -ne 0 ]; then
logPrint "ERROR in upload" 1 1
else
logPrint "ftp transfer finished successfully" 0 0
fi
for currentRemoteDirectory in $($curlBin -s -u $ftpUser:$ftpPass ftp://$ftpHost/$hostname/ -X MLSD | grep 'type=dir' | cut -d';' -f8)
do
if [[ $currentRemoteDirectory =~ ^[0-9]{14}$ ]]; then
if [ $currentRemoteDirectory -lt $remoteBackupDays ]; then
if [ ! -z $currentRemoteDirectory ]; then
logPrint "remove $hostname/$currentRemoteDirectory" 0 0
$lftpBin -u $ftpUser:$ftpPass $ftpHost -e "rm -r $hostname/$currentRemoteDirectory; bye"
checkRemoteRemove=$?
if [ $checkRemoteRemove -ne 0 ]; then
logPrint "ERROR could not remove remote directory $hostname/$currentRemoteDirectory" 1 0
fi
fi
fi
fi
done
fi
####### Local HouseKeep #######
if [ $keepLocalCopy -eq 1 ]; then
localBackupDirs=$(ls $localBackupDir)
for directory in $localBackupDirs ; do
if [[ $directory =~ ^[0-9]{14}$ ]]; then
if [ $directory -lt $localBackupDays ]; then
removeDir=$localBackupDir/$directory
logPrint "check if directory exists $removeDir" 0 0
if [ -d $removeDir ] && [ ! -z $directory ] ; then
logPrint "remove $removeDir" 0 0
rm -rf $removeDir
removeDirStatus=$?
logPrint "remove status $removeDirStatus" 0 0
if [ $removeDirStatus -ne 0 ]; then
logPrint "ERROR could not remove directory $removeDir" 1 1
fi
else
logPrint "ERROR directory $removeDir does not exist. Someting went wrong" 1 1
fi
fi
fi
done
else
rm -rf $localBackupDir/*
fi
####### Local HouseKeep #######
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