forked from tachtler/dovecot-backup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdovecot_backup.sh
More file actions
405 lines (356 loc) · 15.4 KB
/
dovecot_backup.sh
File metadata and controls
405 lines (356 loc) · 15.4 KB
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
#!/bin/bash
##############################################################################
# Script-Name : dovecot_backup.sh #
# Description : Script to backup the mailboxes from dovecot. #
# On successful execution only a LOG file will be written. #
# On error while execution, a LOG file and a error message #
# will be send by e-mail. #
# #
# Last update : 04.07.2018 #
# Version : 1.05 #
# #
# Author : Klaus Tachtler, <klaus@tachtler.net> #
# DokuWiki : http://www.dokuwiki.tachtler.net #
# Homepage : http://www.tachtler.net #
# #
# +----------------------------------------------------------------------+ #
# | This program is free software; you can redistribute it and/or modify | #
# | it under the terms of the GNU General Public License as published by | #
# | the Free Software Foundation; either version 2 of the License, or | #
# | (at your option) any later version. | #
# +----------------------------------------------------------------------+ #
# #
# Copyright (c) 2018 by Klaus Tachtler. #
# #
##############################################################################
##############################################################################
# H I S T O R Y #
##############################################################################
# Version : 1.01 #
# Description : Bugfix: Delete all temporary domain directories not only the #
# last one. Thanks to Guenther J. Niederwimmer. #
# -------------------------------------------------------------------------- #
# Version : 1.02 #
# Description : GitHub: Issue #1 #
# The name of the variable to delete the number of old backup #
# files $DAYS_DELETE was renamed to $BACKUPFILES_DELETE. This #
# was done for better understanding, because if the script was #
# running more than once a day, this could be misunderstood. #
# Thanks to Diane Trout. #
# -------------------------------------------------------------------------- #
# Version : 1.03 #
# Description : Quota calculation double the calculated size of a mailbox, #
# when dict was used. See also following mailing-list entry: #
# #
# https://www.dovecot.org/list/dovecot/2012-February/ #
# 063585.html #
# #
# Thanks to André Peters. #
# -------------------------------------------------------------------------- #
# Version : 1.04 #
# Description : Typo: Correction of the return code query of #
# "# Delete LOCK file." in a pure string comparison. #
# Thanks to Oli Sennhauser. #
# -------------------------------------------------------------------------- #
# Version : 1.05 #
# Description : GitHub: Issue #4 #
# Add error handling for dsync command. #
# Add runtime statistics. #
# Thanks to HenrikWMG. #
# -------------------------------------------------------------------------- #
# Version : x.xx #
# Description : <Description> #
# -------------------------------------------------------------------------- #
##############################################################################
##############################################################################
# >>> Please edit following lines for personal settings and custom usages. ! #
##############################################################################
# CUSTOM - Script-Name.
SCRIPT_NAME='dovecot_backup'
# CUSTOM - Backup-Files.
DIR_BACKUP='/srv/backup'
FILE_BACKUP=dovecot_backup_`date '+%Y%m%d_%H%M%S'`.tar.gz
FILE_DELETE='*.tar.gz'
BACKUPFILES_DELETE=14
# CUSTOM - dovecot Folders.
MAILDIR_TYPE='maildir'
MAILDIR_NAME='Maildir'
MAILDIR_USER='vmail'
MAILDIR_GROUP='vmail'
# CUSTOM - Mail-Recipient.
MAIL_RECIPIENT='you@example.com'
# CUSTOM - Status-Mail [Y|N].
MAIL_STATUS='N'
##############################################################################
# >>> Normaly there is no need to change anything below this comment line. ! #
##############################################################################
# Variables.
DSYNC_COMMAND=`command -v dsync`
TAR_COMMAND=`command -v tar`
TOUCH_COMMAND=`command -v touch`
RM_COMMAND=`command -v rm`
PROG_SENDMAIL=`command -v sendmail`
CAT_COMMAND=`command -v cat`
DATE_COMMAND=`command -v date`
MKDIR_COMMAND=`command -v mkdir`
FILE_LOCK='/tmp/'$SCRIPT_NAME'.lock'
FILE_LOG='/var/log/'$SCRIPT_NAME'.log'
FILE_LAST_LOG='/tmp/'$SCRIPT_NAME'.log'
FILE_MAIL='/tmp/'$SCRIPT_NAME'.mail'
FILE_MBOXLIST='/tmp/'$SCRIPT_NAME'.mboxlist'
VAR_HOSTNAME=`uname -n`
VAR_SENDER='root@'$VAR_HOSTNAME
VAR_EMAILDATE=`$DATE_COMMAND '+%a, %d %b %Y %H:%M:%S (%Z)'`
declare -a VAR_FAILED_USER=()
VAR_COUNT_USER=0
VAR_COUNT_FAIL=0
# Functions.
function log() {
echo $1
echo `$DATE_COMMAND '+%Y/%m/%d %H:%M:%S'` " INFO:" $1 >>${FILE_LAST_LOG}
}
function retval() {
if [ "$?" != "0" ]; then
case "$?" in
*)
log "ERROR: Unknown error $?"
;;
esac
fi
}
function movelog() {
$CAT_COMMAND $FILE_LAST_LOG >> $FILE_LOG
$RM_COMMAND -f $FILE_LAST_LOG
$RM_COMMAND -f $FILE_LOCK
}
function sendmail() {
case "$1" in
'STATUS')
MAIL_SUBJECT='Status execution '$SCRIPT_NAME' script.'
;;
*)
MAIL_SUBJECT='ERROR while execution '$SCRIPT_NAME' script !!!'
;;
esac
$CAT_COMMAND <<MAIL >$FILE_MAIL
Subject: $MAIL_SUBJECT
Date: $VAR_EMAILDATE
From: $VAR_SENDER
To: $MAIL_RECIPIENT
MAIL
$CAT_COMMAND $FILE_LAST_LOG >> $FILE_MAIL
$PROG_SENDMAIL -f $VAR_SENDER -t $MAIL_RECIPIENT < $FILE_MAIL
$RM_COMMAND -f $FILE_MAIL
}
# Main.
log ""
log "+-----------------------------------------------------------------+"
log "| Start backup the mailboxes of dovecot server................... |"
log "+-----------------------------------------------------------------+"
log ""
log "Run script with following parameter:"
log ""
log "SCRIPT_NAME...: $SCRIPT_NAME"
log ""
log "DIR_BACKUP....: $DIR_BACKUP"
log ""
log "MAIL_RECIPIENT: $MAIL_RECIPIENT"
log "MAIL_STATUS...: $MAIL_STATUS"
log ""
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$DSYNC_COMMAND" ]; then
log "Check if command '$DSYNC_COMMAND' was found....................[FAILED]"
sendmail ERROR
movelog
exit 11
else
log "Check if command '$DSYNC_COMMAND' was found....................[ OK ]"
fi
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$TAR_COMMAND" ]; then
log "Check if command '$TAR_COMMAND' was found......................[FAILED]"
sendmail ERROR
movelog
exit 12
else
log "Check if command '$TAR_COMMAND' was found......................[ OK ]"
fi
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$TOUCH_COMMAND" ]; then
log "Check if command '$TOUCH_COMMAND' was found....................[FAILED]"
sendmail ERROR
movelog
exit 13
else
log "Check if command '$TOUCH_COMMAND' was found....................[ OK ]"
fi
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$RM_COMMAND" ]; then
log "Check if command '$RM_COMMAND' was found.......................[FAILED]"
sendmail ERROR
movelog
exit 14
else
log "Check if command '$RM_COMMAND' was found.......................[ OK ]"
fi
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$CAT_COMMAND" ]; then
log "Check if command '$CAT_COMMAND' was found......................[FAILED]"
sendmail ERROR
movelog
exit 15
else
log "Check if command '$CAT_COMMAND' was found......................[ OK ]"
fi
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$DATE_COMMAND" ]; then
log "Check if command '$DATE_COMMAND' was found.....................[FAILED]"
sendmail ERROR
movelog
exit 16
else
log "Check if command '$DATE_COMMAND' was found.....................[ OK ]"
fi
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$MKDIR_COMMAND" ]; then
log "Check if command '$MKDIR_COMMAND' was found....................[FAILED]"
sendmail ERROR
movelog
exit 17
else
log "Check if command '$MKDIR_COMMAND' was found....................[ OK ]"
fi
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$PROG_SENDMAIL" ]; then
log "Check if command '$PROG_SENDMAIL' was found................[FAILED]"
sendmail ERROR
movelog
exit 18
else
log "Check if command '$PROG_SENDMAIL' was found................[ OK ]"
fi
# Check if LOCK file NOT exist.
if [ ! -e "$FILE_LOCK" ]; then
log "Check if script is NOT already runnig .....................[ OK ]"
$TOUCH_COMMAND $FILE_LOCK
else
log "Check if script is NOT already runnig .....................[FAILED]"
log ""
log "ERROR: The script was already running, or LOCK file already exists!"
log ""
sendmail ERROR
movelog
exit 20
fi
# Check if DIR_BACKUP Directory NOT exists.
if [ ! -d "$DIR_BACKUP" ]; then
log "Check if DIR_BACKUP exists.................................[FAILED]"
$MKDIR_COMMAND -p $DIR_BACKUP
log "DIR_BACKUP was now created.................................[ OK ]"
else
log "Check if DIR_BACKUP exists.................................[ OK ]"
fi
# Start backup.
log ""
log "+-----------------------------------------------------------------+"
log "| Run backup $SCRIPT_NAME ..................................... |"
log "+-----------------------------------------------------------------+"
log ""
# Set right permissions to backup directory.
chown -R $MAILDIR_USER:$MAILDIR_GROUP $DIR_BACKUP
chmod 700 $DIR_BACKUP
# Start real backup process for all users.
for users in `doveadm user "*"`; do
log "Start backup process for user: $users ..."
((VAR_COUNT_USER++))
DOMAINPART=${users#*@}
LOCALPART=${users%%@*}
LOCATION="$DIR_BACKUP/$DOMAINPART/$LOCALPART/$MAILDIR_NAME"
USERPART="$DOMAINPART/$LOCALPART"
log "Extract mailbox data for user: $users ..."
$DSYNC_COMMAND -o plugin/quota= -f -u $users backup $MAILDIR_TYPE:$LOCATION
# Check the status of dsync and continue the script depending on the result.
if [ "$?" != "0" ]; then
case "$?" in
1) log "Synchronization failed > user: $users !!!"
;;
2) log "Synchronization was done without errors, but some changes couldn't be done, so the mailboxes aren't perfectly synchronized for user: $users !!!"
;;
esac
if [ "$?" -gt "3" ]; then
log "Synchronization failed > user: $users !!!"
fi
((VAR_COUNT_FAIL++))
VAR_FAILED_USER+=($users);
else
log "Synchronization done for user: $users ..."
cd $DIR_BACKUP
log "Packaging to archive for user: $users ..."
$TAR_COMMAND -cvzf $users-$FILE_BACKUP $USERPART --atime-preserve --preserve-permissions
chown $MAILDIR_USER:$MAILDIR_GROUP $users-$FILE_BACKUP
chmod 600 $users-$FILE_BACKUP
log "Delete archive files for user: $users ..."
(ls $users-$FILE_DELETE -t|head -n $BACKUPFILES_DELETE;ls $users-$FILE_DELETE )|sort|uniq -u|xargs -r rm
if [ "$?" != "0" ]; then
log "Delete old archive files $DIR_BACKUP .....................[FAILED]"
else
log "Delete old archive files $DIR_BACKUP .....................[ OK ]"
fi
log "Delete mailbox files for user: $users ..."
$RM_COMMAND "$DIR_BACKUP/$DOMAINPART" -rf
if [ "$?" != "0" ]; then
log "Delete mailbox files at: $DIR_BACKUP .....................[FAILED]"
else
log "Delete mailbox files at: $DIR_BACKUP .....................[ OK ]"
fi
fi
log "Ended backup process for user: $users ..."
log ""
done
# Delete LOCK file.
if [ "$?" != "0" ]; then
retval $?
log ""
$RM_COMMAND -f $FILE_LOCK
sendmail ERROR
movelog
exit 99
else
log ""
log "+-----------------------------------------------------------------+"
log "| End backup $SCRIPT_NAME ..................................... |"
log "+-----------------------------------------------------------------+"
log ""
fi
# Finish syncing with runntime statistics.
log "+-----------------------------------------------------------------+"
log "| Runntime statistics............................................ |"
log "+-----------------------------------------------------------------+"
log ""
log "- Number of determined users: $VAR_COUNT_USER"
log "- ...Summary of failed users: $VAR_COUNT_FAIL"
if [ "$VAR_COUNT_FAIL" -gt "0" ]; then
log "- ...Mailbox of failed users: "
for i in "${VAR_FAILED_USER[@]}"
do
log "- ... $i"
done
fi
log ""
log "+-----------------------------------------------------------------+"
log "| Finish......................................................... |"
log "+-----------------------------------------------------------------+"
log ""
# If errors occured on user backups, exit with return code 1 instead of 0.
if [ "$VAR_COUNT_FAIL" -gt "0" ]; then
sendmail ERROR
movelog
exit 1
else
# Status e-mail.
if [ $MAIL_STATUS = 'Y' ]; then
sendmail STATUS
fi
movelog
exit 0
fi