-
Notifications
You must be signed in to change notification settings - Fork 0
/
zmsnapshot
executable file
·349 lines (316 loc) · 9.73 KB
/
zmsnapshot
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
#!/bin/bash
#
# zmbkpose
#
# Bash script to hot backup and hot restore Zimbra Collaboration Suite Opensource
#
# Copyright (C) 2007 Rubens Alonso Filho <rubens@harv.com.br>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public
# License as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA
#
# Version: 1.0.0
show_help ()
{
echo "Uses:
zmsnapshot -f
Starts a full backup of all accounts.
zmsnapshot -f mail
Starts a full backup of one account specified at command line.
zmsnapshot -l
Lists all backup sessions done.
zmsnapshot -r mail1,mail2,...,mailn session_name
Restore all contents (mail, contacts, appointments...) to any account from
session specifieds at command line. If session is not specified, all
sessions will be restored from oldest to newest session. It may take longer.
zmsnapshot -restoreAccount mail1,mail2,...,mailn
Restore deleted accounts back to directory and all of its contents from
oldest to newest session. It may take longer.
zmsnapshot -restoreAccount mail1,mail2,...,mailn --LDAPOnly session_name
Restore only account attributes, like password, class of service, etc; from
specified session.
HINT: It may be necessary to flush server's cache to apply imediatelly
recovered attributes. So at Zimbra server, run: zmprov fc account account_name"
exit 0
}
# Setup
CURL=$(which curl)
LDAPSEARCH=$(which ldapsearch)
LDAPDELETE=$(which ldapdelete)
LDAPADD=$(which ldapadd)
SESSION=$(date +%Y%m%d%H%M%S)
session_start()
{
echo "SESSION: $SESSION started at $(date)" >> $WORKDIR/sessions.txt
TEMPDIR=$(mktemp -d $WORKDIR/XXXX)
mkdir ${WORKDIR}/${SESSION}
}
session_end()
{
echo "SESSION: $SESSION ended at $(date)" >> $WORKDIR/sessions.txt
rm -rf "$TEMPDIR"
}
all_accounts_backup ()
{
ACCOUNTSLIST=$(mktemp)
$LDAPSEARCH -x -H $LDAPMASTERSERVER -D $LDAPZIMBRADN -w $LDAPZIMBRAPASS -b "" -LLL "(objectclass=zimbraAccount)" zimbraMailDeliveryAddress zimbraMailHost | grep ^zimbraMail | awk '{print $2}' > "$ACCOUNTSLIST"
for MAIL in $(grep @ $ACCOUNTSLIST); do
account_backup $MAIL
done
}
account_backup ()
{
# Backup LDAP mail account data and extract the mailhost
$LDAPSEARCH -x -H $LDAPMASTERSERVER -D $LDAPZIMBRADN -w $LDAPZIMBRAPASS -b "" -LLL "(zimbraMailDeliveryAddress=$MAIL)" > $TEMPDIR/$MAIL.ldiff
MAILHOST=$(grep ^zimbraMailHost $TEMPDIR/$MAIL.ldiff | awk '{print $2}')
if [ -z $MAILHOST ]; then
echo "Could not find Mailhost in LDAP data. Maybe the mail account does not exist"
return 1
fi
# Not every mail account is always backed up.
# Figure out which SESSION it was in the last time
PREVIOUSSESSION=$(grep $MAIL $WORKDIR/sessions.txt | grep -v ^WARN | tail -1 | awk -F: '{print $1}')
if [ ! -z $PREVIOUSSESSION ]; then
echo $MAIL " found in " $PREVIOUSSESSION
else
echo "No previous backup session."
fi
# Download and extract into temp
mkdir ${TEMPDIR}/$MAIL
(cd $TEMPDIR/$MAIL; $CURL -s -k -u $ADMINUSER:$ADMINPASS https://$MAILHOST:7071/home/$MAIL/?fmt=tgz | tar xfpz - ; cd - )
# rsync from temp into session dir, taking the previous session into account
rsync -rlpcgoDi --delete --link-dest=../$PREVIOUSSESSION/ $TEMPDIR/ $WORKDIR/$SESSION
echo $SESSION:$MAIL:$(date +%m/%d/%y):$? >> $WORKDIR/sessions.txt
}
list_sessions ()
{
if [ -z $MAIL ]; then
LIST=$(grep SESSION: $WORKDIR/sessions.txt| grep started | awk '{print $2}')
else
LIST=$(grep $MAIL $WORKDIR/sessions.txt | grep -v ^WARN | awk -F: '{print $1}')
fi
for DIRECTORY in $LIST; do
if [ ! -d "$DIRECTORY" ]; then
echo $DIRECTORY " missing directory !?"
else
echo $DIRECTORY
fi
done
}
account_restore ()
{
ACCOUNTBKPS=$(mktemp)
K=1
while true; do
MAIL=$(echo $1, | cut -d, -f$K)
if [ -z $MAIL ]; then
break
fi
grep $MAIL $WORKDIR/sessions.txt | grep -v ^WARN: > $ACCOUNTBKPS
if ! [ -s $ACCOUNTBKPS ]; then
echo "$MAIL: No backups found. Impossible to restore"
((K = K+1))
else
if [ -z $2 ]; then
echo "Not Implemented."
# Complete restore from oldest to newest
((K = K+1))
else
ACCOUNTSESSION=$(grep $2 $WORKDIR/sessions.txt | tail -1 | awk '{print $2}')
if [ -z $ACCOUNTSESSION ]; then
echo "$MAIL: $2 session doesnt exists. Impossible to proceed..."
break
else
ACCOUNTINSESSIO=$(grep $MAIL $ACCOUNTBKPS | grep $ACCOUNTSESSION)
if [ -z $ACCOUNTINSESSIO ]; then
echo "$MAIL not found in session $ACCOUNTSESSION. Impossible to restore."
((K = K+1))
else
MAILHOST=$(grep ^zimbraMailHost $WORKDIR/$ACCOUNTSESSION/$MAIL.ldiff | awk '{print $2}')
$CURL -k --data-binary @$WORKDIR/$ACCOUNTSESSION/$MAIL.tgz -u $ADMINUSER:$ADMINPASS https://$MAILHOST:7071/home/$MAIL/?fmt=tgz\&resolve=replace
((K = K+1))
unset MAIL
fi
fi
fi
fi
done
exit 0
}
LDAP_content_restore ()
{
ACCOUNTBKPS=$(mktemp)
K=1
while true; do
MAIL=$(echo $1, | cut -d, -f$K)
if [ -z $MAIL ]; then
break
fi
if [ -z $2 ]; then
EXIST=$($LDAPSEARCH -x -H $LDAPMASTERSERVER -D $LDAPZIMBRADN -w $LDAPZIMBRAPASS -b "" -LLL "(&(objectclass=zimbraAccount)(zimbraMailDeliveryAddress=$MAIL))" uid)
if ! [ -z "$EXIST" ]; then
echo "$MAIL account exists. Run zmsnapshot -r $MAIL session_name."
((K = K+1))
else
grep $MAIL $WORKDIR/sessions.txt | grep -e ^inc- -e ^full- > $ACCOUNTBKPS
if ! [ -s $ACCOUNTBKPS ]; then
echo "$MAIL: No backups found. Impossible to restore"
((K = K+1))
else
echo "Sessions found
$(cat $ACCOUNTBKPS | awk -F: '{print $1}')"
for ACCOUNTSESSION in $(cat $ACCOUNTBKPS | awk -F: '{print $1}'); do
echo "Restoring from $ACCOUNTSESSION"
MAILHOST=$(grep ^zimbraMailHost $WORKDIR/$ACCOUNTSESSION/$MAIL.ldiff | awk '{print $2}')
$LDAPDELETE -r -x -H $LDAPMASTERSERVER -D $LDAPZIMBRADN -c -w $LDAPZIMBRAPASS $(grep ^dn: $WORKDIR/$ACCOUNTSESSION/$MAIL.ldiff | awk '{print $2}') 2>/dev/null
$LDAPADD -x -H $LDAPMASTERSERVER -D $LDAPZIMBRADN -c -w $LDAPZIMBRAPASS -f $WORKDIR/$ACCOUNTSESSION/$MAIL.ldiff
$CURL -k --data-binary @$WORKDIR/$ACCOUNTSESSION/$MAIL.tgz -u $ADMINUSER:$ADMINPASS https://$MAILHOST:7071/home/$MAIL/?fmt=tgz\resolv=replace
echo "$MAIL restored from $ACCOUNTSESSION"
done
((K = K+1))
unset MAIL
fi
fi
else
ACCOUNTSESSION=$(grep $2 $WORKDIR/sessions.txt | grep $MAIL | tail -1 | awk -F: '{print $1}')
if [ -z $ACCOUNTSESSION ]; then
echo "$MAIL: Session $2 doesnt exist or not found. Impossible to restore..."
((K = K+1))
else
USERDN=$(grep ^dn: $WORKDIR/$ACCOUNTSESSION/$MAIL.ldiff | awk '{print $2}')
$LDAPDELETE -r -x -H $LDAPMASTERSERVER -D $LDAPZIMBRADN -c -w $LDAPZIMBRAPASS $USERDN
$LDAPADD -x -H $LDAPMASTERSERVER -D $LDAPZIMBRADN -c -w $LDAPZIMBRAPASS -f $WORKDIR/$ACCOUNTSESSION/$MAIL.ldiff
echo "User profile and settings restored from $ACCOUNTSESSION"
((K = K+1))
unset MAIL
fi
fi
done
exit 0
}
# Loading config file
source /etc/zmsnapshot/zmsnapshot.conf
if ! [ -z "$BACKUPUSER" ]; then
if [ "$(id -u)" != "$(id -u $BACKUPUSER)" ]; then
echo "You must be $BACKUPUSER to run this script"
exit 0
fi
else
echo "You must set BACKUPUSER"
exit 0
fi
if ! [ -z "$WORKDIR" ]; then
if ! [ -d "$WORKDIR" ]; then
echo "$WORKDIR doesnt exist"
exit 0
fi
else
echo "You must set WORKDIR"
exit 0
fi
if [ -z "$ADMINUSER" ]; then
echo "You must set ADMINUSER"
exit 0
fi
if [ -z "$ADMINPASS" ]; then
echo "You must set ADMINPASS"
exit 0
fi
if [ -z "$LDAPMASTERSERVER" ]; then
echo "You must set LDAPMASTERSERVER"
exit 0
fi
if [ -z "$LDAPZIMBRADN" ]; then
echo "You must set LDAPZIMBRADN"
exit 0
fi
if [ -z "$LDAPZIMBRAPASS" ]; then
echo "You must set LDAPZIMBRAPASS"
exit 0
fi
if [ -z "$LOGFILE" ]; then
echo "You must set LOGFILE"
exit 0
fi
case "$1" in
"-f" )
if [ -z "$2" ]; then
session_start
all_accounts_backup
session_end
elif [ -z "$3" ]; then
session_start
MAIL=$2
account_backup
session_end
else
echo "Incorrect parameters $@. See help."
show_help
fi
;;
"-l" )
if [ -z "$2" ]; then
list_sessions
elif [ -z "$3" ]; then
MAIL=$2
list_sessions
else
echo "Incorrect parameters $@. See help."
show_help
fi
;;
"-r" )
if [ -z "$2" ]; then
echo "Incorrect parameters $@. See help."
show_help
else
if [ -z "$4" ]; then
account_restore $2 $3
else
echo "Incorrect parameters $@. See help."
show_help
fi
fi
;;
"-restoreAccount" )
if [ -z "$2" ]; then
echo "Incorrect parameters $@. See help."
show_help
else
if [ -z "$3" ]; then
LDAP_content_restore $2
else
if [ "$3" = "--LDAPOnly" ]; then
LDAP_content_restore $2 $4
else
echo "Incorrect parameters $@. See help."
show_help
fi
fi
fi
;;
# "--DeusMeAjude" )
# if ! [ -z "$2" ]; then
# echo "Incorrect parameters $@. See help."
# show_help
# else
# DeusMeAjude
# fi
# ;;
* )
echo "Incorrect parameters $@. See help."
show_help
;;
esac
exit 0