Subversion Repositories oZimbraBackup

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 soulskater 1
#!/bin/bash
2
 
3
# This script will do various backups of Zimbra depending on which you choose,
4
# most of them are cold backups except the msg backup which hot copies the 'store'
5
# folder for possible individual mail retrieval. Be sure to change the variables below
6
# to point to where you got the Zimbra folder and where you want it to be backed up to.
7
#
8
# This script must be run as root or a user with equal privileges or it will not work.
9
#
10
# When you run this script via crontab be sure to add '> /dev/null 2>&1' at the end
11
# of the script like below or the tar command will fail for no apparent reason.
12
# 00 12 * * * oZimbraBackup.sh --full > /dev/null 2>&1
13
#
14
# As of 2008-04-16 this script uses some extra software to extend this script,
15
# the scripts standard function will still function without these extra software's but if you
16
# intend to use this scripts built in file transfer functions you must install the described software below.
17
#
18
# Required software: rsync for normal operation and ftp or scp & expect for file transfer capabillities.
19
#
20
# Bits and pieces was adopted from a script created by Daniel W. Martin, 9 Sept 2007
21
# Licensed under BSDL license, see license.txt for information.
22
#
23
# Developer: oSource Development(as of 2009-07-01)
24
# DevTeam: Marcus Uddenhed
25
# Version: 1.0.4
26
# Last updated: 2011-04-21 22:32
27
 
28
#### Global Settings ####
29
ZimInstPath=/opt			# Installation path for Zimbra, excluding the Zimbra folder.
30
ZimHome=zimbra				# The Zimbra installation folder, excluding path to folder.
31
ZimBackupPath=/opt/backup		# Root folder for backup where backup files will be placed.
32
 
33
#### Log Settings ####
34
ZimLogEnable=yes			# Turns logging on or off(yes/no).
35
ZimLogLogRotate=no			# Enables log rotating(yes/no)
36
ZimLogRotateInt=day			# How often should we rotate logs(day, week or month)
37
ZimLogPath=/opt/logs			# Folder for log files
38
ZimLogVerbose=no			# Activates extra logging information(yes/no)
39
 
40
#### File Transfer Settings ####
41
 
42
# Enable Services (yes/no)
43
ZimFtpEnable=no				# Enable/Disable ftp file transfer(yes/no)
44
ZimScpEnable=no				# Enable/Disable scp file transfer(yes/no)
45
 
46
# Extra FTP Settings
47
ZimFtpOpt=''				# Extra options for ftp file transfer, see manual for ftp command
48
 
49
# Extra SCP Settings
50
ZimScpOpt=''				# Extra options for scp file transfer, see manual for scp command
51
 
52
# Common Settings
53
ZimFilehostUser=			# Username for file transfers
54
ZimFilehostPass=			# Password for file transfers
55
ZimFilehostAddress=			# Host address for file transfers
56
ZimFilehostFolder=			# Folder on host where files will be placed during file transfer
57
 
58
#### File Delete Settings ####
59
ZimDeleteLocalFile=no			# Enable/Disable compressed backup file deletion after successful backup(yes/no)
60
ZimDeleteTimeSet=0			# Set in minutes above 0 to keep a desired amount of files locally,
61
					# be sure to match the time with your backup schedules.
62
 
63
##### Do not change anything below this line unless you know what you are doing #####
64
 
65
# Fetch backup type
66
ZimBackupType=$1
67
 
68
pre_check() {
69
# Set default abort value
70
ZimAbort=0
71
 
72
# Check for rsync
73
if [ ! -e /usr/bin/rsync ]
74
 then
75
  echo "Cannot find rsync software..."
76
  ZimAbort=1
77
fi
78
 
79
# Check for tar
80
if [ ! -e /bin/tar ]
81
 then
82
  echo "Cannot find tar software..."
83
  ZimAbort=1
84
fi
85
 
86
# Check for gzip
87
if [ ! -e /bin/gzip ]
88
 then
89
  if [ ! -e /usr/bin/gzip ]
90
   then
91
    echo "Cannot find gzip software..."
92
    ZimAbort=1
93
  fi
94
fi
95
 
96
# Check if expect, ftp & scp is installed when file transfer is set to yes
97
if [ $ZimFtpEnable = 'yes' ] || [ $ZimScpEnable = 'yes' ]
98
then
99
 if [ ! -e /usr/bin/expect ]
100
  then
101
   echo "cannot find expect command..."
102
   ZimAbort=1
103
 fi
104
 if [ $ZimFtpEnable = 'yes' ]
105
 then
106
  if [ ! -e /usr/bin/ftp ]
107
   then
108
    echo "Cannot find ftp command..."
109
    ZimAbort=1
110
  fi
111
 fi
112
 
113
 if [ $ZimScpEnable = 'yes' ]
114
 then
115
  if [ ! -e /usr/bin/scp ]
116
   then
117
    echo "Cannot find scp command..."
118
    ZimAbort=1
119
  fi
120
 fi
121
fi
122
 
123
if [ $ZimAbort = '1' ]
124
 then
125
  echo "Please install above software, script exiting..."
126
  exit
127
fi
128
}
129
 
130
pre_load() {
131
# Get temporary folder layout
132
temp_folders
133
 
134
# Get backup file format
135
backup_file
136
 
137
# Get log file format
138
log_file
139
 
140
# Check to see if the backup folder exist, create if not
141
mkdir -p $ZimBackupPath
142
 
143
# Check which zimbra version that is installed, for recovery purposes
144
sudo -u zimbra $ZimInstPath/$ZimHome/bin/zmcontrol -v > $ZimBackupPath/zimbra_version.txt
145
}
146
 
147
full_backup() {
148
if [ $ZimLogEnable = 'yes' ] && [ $ZimLogVerbose = 'yes' ]
149
 then
150
 
151
   # Hot sync before shutdown on zimbra folder
152
   echo "`date "+%Y-%m-%d %H:%M:%S"` - Hot syncing to backup folder..." >> $ZimLogFile
153
   rsync -avHK --delete $ZimInstPath/$ZimHome $ZimFullTmpPath >> $ZimLogFile
154
   echo "`date "+%Y-%m-%d %H:%M:%S"` - Hot syncing to backup folder done." >> $ZimLogFile
155
 
156
   # Stopping Zimbra
157
   zimbra_stop
158
 
159
   # Cold sync of zimbra folder
160
   echo "`date "+%Y-%m-%d %H:%M:%S"` - Cold syncing to backup folder..." >> $ZimLogFile
161
   rsync -avHK --delete $ZimInstPath/$ZimHome $ZimFullTmpPath >> $ZimLogFile
162
   echo "`date "+%Y-%m-%d %H:%M:%S"` - Cold syncing to backup folder done." >> $ZimLogFile
163
 
164
   # Starting Zimbra
165
   zimbra_start 
166
 
167
   # Compressing backup for space reduction
168
   echo "`date "+%Y-%m-%d %H:%M:%S"` - Compressing backup folder..." >> $ZimLogFile
169
   tar -zcvpf $ZimBackupPath/$ZimBackupFile -C $ZimBackupPath tf zimbra_version.txt >> $ZimLogFile
170
   echo "`date "+%Y-%m-%d %H:%M:%S"` - Compressed backup folder." >> $ZimLogFile
171
 
172
   # Cleaning differential folder.
173
   echo "`date "+%Y-%m-%d %H:%M:%S"` - Cleaning differential backup folder..."
174
   rm -r -f $ZimDiffTmpPath/* >> $ZimLogFile
175
   echo "`date "+%Y-%m-%d %H:%M:%S"` - Cleaned differential backup folder."
176
 
177
 else
178
 
179
  # Hot sync before shutdown on zimbra folder
180
  rsync -avHK --delete $ZimInstPath/$ZimHome $ZimFullTmpPath
181
 
182
  # Stopping Zimbra
183
  zimbra_stop
184
 
185
  # Cold sync of zimbra folder
186
  rsync -avHK --delete $ZimInstPath/$ZimHome $ZimFullTmpPath
187
 
188
  # Starting Zimbra
189
  zimbra_start
190
 
191
  # Compressing backup for space reduction
192
  tar -zcvpf $ZimBackupPath/$ZimBackupFile -C $ZimBackupPath tf zimbra_version.txt
193
 
194
  # Cleaning differential folder.
195
   rm -r -f $ZimDiffTmpPath/*
196
fi
197
}
198
 
199
diff_backup() {
200
if [ $ZimLogEnable = 'yes' ] && [ $ZimLogVerbose = 'yes' ]
201
 then
202
  # Cleaning backup folder
203
  #echo "`date "+%Y-%m-%d %H:%M:%S"` - Cleaning backup folder..." >> $ZimLogFile
204
  #rm -r -f $ZimDiffTmpPath/* >> $ZimLogFile
205
  #echo "`date "+%Y-%m-%d %H:%M:%S"` - Cleaning backup folder done." >> $ZimLogFile
206
 
207
  # Hot syncing to backup folder
208
  echo "`date "+%Y-%m-%d %H:%M:%S"` - Hot syncing to backup folder..." >> $ZimLogFile
209
  rsync -avHK --compare-dest=$ZimFullTmpPath/ $ZimInstPath/$ZimHome $ZimDiffTmpPath >> $ZimLogFile
210
  echo "`date "+%Y-%m-%d %H:%M:%S"` - Hot syncing to backup folder done." >> $ZimLogFile
211
 
212
  # Stopping Zimbra
213
  zimbra_stop
214
 
215
  # Cold syncing to backup folder
216
  echo "`date "+%Y-%m-%d %H:%M:%S"` - Cold syncing to backup folder..." >> $ZimLogFile
217
  rsync -avHK --compare-dest=$ZimFullTmpPath/ $ZimInstPath/$ZimHome $ZimDiffTmpPath >> $ZimLogFile
218
  echo "`date "+%Y-%m-%d %H:%M:%S"` - Cold syncing to backup folder done." >> $ZimLogFile
219
 
220
  # Starting Zimbra
221
  zimbra_start
222
 
223
  # Syncing backup folders
224
  #echo "`date "+%Y-%m-%d %H:%M:%S"` - Syncing backup folders..." >> $ZimLogFile
225
  #cp -R $ZimDiffTmpPath/* $ZimFullTmpPath/ >> $ZimLogFile
226
  #echo "`date "+%Y-%m-%d %H:%M:%S"` - Syncing backup folders done." >> $ZimLogFile
227
 
228
  # Compressing backup folder
229
  echo "`date "+%Y-%m-%d %H:%M:%S"` - Compressing backup folder..." >> $ZimLogFile
230
  tar -zcvpf $ZimBackupPath/$ZimBackupFile -C $ZimBackupPath td zimbra_version.txt >> $ZimLogFile
231
  echo "`date "+%Y-%m-%d %H:%M:%S"` - Compressing backup folder done." >> $ZimLogFile
232
 
233
 else
234
 
235
  # Hot sync before shutdown on zimbra folder
236
  #rm -R $ZimDiffTmpPath/*
237
  rsync -avHK --compare-dest=$ZimFullTmpPath/ $ZimInstPath/$ZimHome $ZimDiffTmpPath
238
 
239
  # Stopping Zimbra
240
  zimbra_stop
241
 
242
  # Cold sync of zimbra folder
243
  rsync -avHK --compare-dest=$ZimFullTmpPath/ $ZimInstPath/$ZimHome $ZimDiffTmpPath
244
 
245
  # Starting Zimbra
246
  zimbra_start
247
 
248
  # Syncing files from diff to full backup folder
249
  #cp -R $ZimDiffTmpPath/* $ZimFullTmpPath/
250
 
251
  # Compressing backup for space reduction
252
  tar -zcvpf $ZimBackupPath/$ZimBackupFile -C $ZimBackupPath td zimbra_version.txt
253
fi
254
}
255
 
256
msgfull_backup() {
257
if [ $ZimLogEnable = 'yes' ] && [ $ZimLogVerbose = 'yes' ]
258
 then
259
  # Hot syncing to backup folder
260
  echo "`date "+%Y-%m-%d %H:%M:%S"` - Hot syncing to backup folder..." >> $ZimLogFile
261
  rsync -avHK --delete $ZimInstPath/$ZimHome/store $ZimMsgFullTmpPath >> $ZimLogFile
262
  echo "`date "+%Y-%m-%d %H:%M:%S"` - Hot syncing to backup folder done." >> $ZimLogFile
263
 
264
  # Compressing backup folder
265
  echo "`date "+%Y-%m-%d %H:%M:%S"` - Compressing backup folder..." >> $ZimLogFile
266
  tar -zcvpf $ZimBackupPath/$ZimBackupFile -C $ZimBackupPath tmf zimbra_version.txt >> $ZimLogFile
267
  echo "`date "+%Y-%m-%d %H:%M:%S"` - Compressing backup folder done." >> $ZimLogFile
268
 
269
  echo "`date "+%Y-%m-%d %H:%M:%S"` - Cleaning differential backup folder..."
270
  rm -r -f $ZimMsgDiffTmpPath/* >> $ZimLogFile
271
  echo "`date "+%Y-%m-%d %H:%M:%S"` - Cleaned differential backup folder."
272
 
273
 else
274
  # Hot syncing to backup folder
275
  rsync -avHK --delete $ZimInstPath/$ZimHome/store $ZimMsgFullTmpPath
276
 
277
  # Compressing backup folder
278
  tar -zcvpf $ZimBackupPath/$ZimBackupFile -C $ZimBackupPath tmf zimbra_version.txt
279
 
280
  # Cleaning differential folder
281
  rm -r -f $ZimMsgDiffTmpPath/*
282
fi
283
}
284
 
285
msgdiff_backup() {
286
if [ $ZimLogEnable = 'yes' ] && [ $ZimLogVerbose = 'yes' ]
287
 then
288
  # Cleaning backup folder
289
  #echo "`date "+%Y-%m-%d %H:%M:%S"` - Cleaning backup folder..." >> $ZimLogFile
290
  #rm -r -f $ZimMsgDiffTmpPath/* >> $ZimLogFile
291
  #echo "`date "+%Y-%m-%d %H:%M:%S"` - Cleaning backup folder done." >> $ZimLogFile
292
 
293
  # Hot syncing to backup folder
294
  echo "`date "+%Y-%m-%d %H:%M:%S"` - Hot syncing to backup folder..." >> $ZimLogFile
295
  rsync -avHK --compare-dest=$ZimMsgFullTmpPath/ $ZimInstPath/$ZimHome/store $ZimMsgDiffTmpPath >> $ZimLogFile
296
  echo "`date "+%Y-%m-%d %H:%M:%S"` - Hot syncing to backup folder done." >> $ZimLogFile
297
 
298
  # Compressing backup folder
299
  echo "`date "+%Y-%m-%d %H:%M:%S"` - Compressing backup folder..." >> $ZimLogFile
300
  tar -zcvpf $ZimBackupPath/$ZimBackupFile -C $ZimBackupPath tmd zimbra_version.txt >> $ZimLogFile
301
  echo "`date "+%Y-%m-%d %H:%M:%S"` - Compressing backup folder done." >> $ZimLogFile
302
 
303
 else
304
  # Cleaning backup folder
305
  #rm -r -f $ZimMsgDiffTmpPath/*
306
 
307
  # Hot syncing to backup folder
308
  rsync -avHK --compare-dest=$ZimMsgFullTmpPath/ $ZimInstPath/$ZimHome/store $ZimMsgDiffTmpPath
309
 
310
  # Compressing backup folder
311
  tar -zcvpf $ZimBackupPath/$ZimBackupFile -C $ZimBackupPath tmd zimbra_version.txt
312
fi
313
}
314
 
315
file_transfer() {
316
# Transfer with ftp
317
if [ $ZimFtpEnable == "yes" ]
318
then
319
if [ $ZimLogEnable = 'yes' ] && [ $ZimLogVerbose = 'yes' ]
320
 then
321
  # Send task start time to log
322
  echo "`date "+%Y-%m-%d %H:%M:%S"` - Sending file via ftp to off-site storage..." >> $ZimLogFile
323
 
324
  # Make a temporary script for expect commands
325
  touch $ZimBackupPath/ftp.exp
326
 
327
  # Fill script with commands
328
  echo '#!/usr/bin/expect --' >> $ZimBackupPath/ftp.exp
329
  echo 'set timeout -1' >> $ZimBackupPath/ftp.exp
330
  echo 'spawn ftp '$ZimFilehostAddress >> $ZimBackupPath/ftp.exp
331
  echo 'expect ):' >> $ZimBackupPath/ftp.exp
332
  echo 'send '$ZimFilehostUser'\r' >> $ZimBackupPath/ftp.exp
333
  echo 'expect :' >> $ZimBackupPath/ftp.exp
334
  echo 'send '$ZimFilehostPass'\r' >> $ZimBackupPath/ftp.exp
335
  echo 'expect >' >> $ZimBackupPath/ftp.exp
336
  echo 'send '$ZimFtpOpt'\r' >> $ZimBackupPath/ftp.exp
337
  echo 'send "send '$ZimBackupPath/$ZimBackupFile $ZimFilehostFolder/$ZimBackupFile'\r"' >> $ZimBackupPath/ftp.exp
338
  echo 'expect >' >> $ZimBackupPath/ftp.exp
339
  echo 'send quit\r' >> $ZimBackupPath/ftp.exp
340
  echo 'expect closed' >> $ZimBackupPath/ftp.exp
341
 
342
  # Run expect with created script
343
  expect $ZimBackupPath/ftp.exp >> $ZimLogFile
344
 
345
  # Delete temporary expect script
346
  rm $ZimBackupPath/ftp.exp
347
 
348
  # Send task stop time to log
349
  echo "`date "+%Y-%m-%d %H:%M:%S"` - Sending file via ftp to off-site storage done." >> $ZimLogFile
350
 
351
 else
352
 
353
  # Make a temporary script for expect commands
354
  touch $ZimBackupPath/ftp.exp
355
 
356
  # Fill script with commands
357
  echo '#!/usr/bin/expect --' >> $ZimBackupPath/ftp.exp
358
  echo 'set timeout -1' >> $ZimBackupPath/ftp.exp
359
  echo 'spawn ftp '$ZimFilehostAddress >> $ZimBackupPath/ftp.exp
360
  echo 'expect ):' >> $ZimBackupPath/ftp.exp
361
  echo 'send '$ZimFilehostUser'\r' >> $ZimBackupPath/ftp.exp
362
  echo 'expect :' >> $ZimBackupPath/ftp.exp
363
  echo 'send '$ZimFilehostPass'\r' >> $ZimBackupPath/ftp.exp
364
  echo 'expect >' >> $ZimBackupPath/ftp.exp
365
  echo 'send '$ZimFtpOpt'\r' >> $ZimBackupPath/ftp.exp
366
  echo 'send "send '$ZimBackupPath/$ZimBackupFile $ZimFilehostFolder/$ZimBackupFile'\r"' >> $ZimBackupPath/ftp.exp
367
  echo 'expect >' >> $ZimBackupPath/ftp.exp
368
  echo 'send quit\r' >> $ZimBackupPath/ftp.exp
369
  echo 'expect closed' >> $ZimBackupPath/ftp.exp
370
 
371
  # Run expect with created script
372
  expect $ZimBackupPath/ftp.exp
373
 
374
  # Delete temporary expect script
375
  rm $ZimBackupPath/ftp.exp
376
 fi
377
fi
378
 
379
# Transfer with scp
380
if [ $ZimScpEnable == "yes" ]
381
then
382
 if [ $ZimLogEnable = 'yes' ] && [ $ZimLogVerbose = 'yes' ]
383
  then
384
 
385
  # Send task start time to log
386
   echo "`date "+%Y-%m-%d %H:%M:%S"` - Sending file via scp to off-site storage..." >> $ZimLogFile
387
 
388
   # Make a temporary script for expect commands
389
   touch $ZimBackupPath/scp.exp
390
 
391
   # Fill script with commands
392
   echo '#!/usr/bin/expect --' >> $ZimBackupPath/scp.exp
393
   echo 'set timeout -1' >> $ZimBackupPath/scp.exp
394
   echo 'spawn scp '$ZimScpOpt $ZimBackupPath/$ZimBackupFile $ZimFilehostUser'@'$ZimFilehostAddress':'$ZimFilehostFolder >> $ZimBackupPath/scp.exp
395
   echo 'expect :' >> $ZimBackupPath/scp.exp
396
   echo 'send '$ZimFilehostPass'\r' >> $ZimBackupPath/scp.exp
397
   echo 'expect closed' >> $ZimBackupPath/scp.exp
398
 
399
   # Run expect with created script
400
   expect $ZimBackupPath/scp.exp >> $ZimLogFile
401
 
402
   # Delete temporary expect script
403
   rm $ZimBackupPath/scp.exp
404
 
405
  # Send task stop time to log
406
   echo "`date "+%Y-%m-%d %H:%M:%S"` - Sending file via scp to off-site storage done." >> $ZimLogFile
407
 
408
  else
409
 
410
   # Make a temporary script for expect commands
411
   touch $ZimBackupPath/scp.exp
412
 
413
   # Fill script with commands
414
   echo '#!/usr/bin/expect --' >> $ZimBackupPath/scp.exp
415
   echo 'set timeout -1' >> $ZimBackupPath/scp.exp
416
   echo 'spawn scp '$ZimScpOpt $ZimBackupPath/$ZimBackupFile $ZimFilehostUser'@'$ZimFilehostAddress':'$ZimFilehostFolder >> $ZimBackupPath/scp.exp
417
   echo 'expect :' >> $ZimBackupPath/scp.exp
418
   echo 'send '$ZimFilehostPass'\r' >> $ZimBackupPath/scp.exp
419
   echo 'expect closed' >> $ZimBackupPath/scp.exp
420
 
421
   # Run expect with created script
422
   expect $ZimBackupPath/scp.exp
423
 
424
   # Delete temporary expect script
425
   rm $ZimBackupPath/scp.exp
426
 fi
427
fi
428
 
429
# Remove local file(s) if ZimDeleteLocalFile is set to 'yes'
430
if [ $ZimDeleteLocalFile == "yes" ]
431
then
432
 if [ $ZimLogEnable = 'yes' ] && [ $ZimLogVerbose = 'yes' ]
433
  then
434
 
435
  # Send task start time to log
436
   echo "`date "+%Y-%m-%d %H:%M:%S"` - Deleting local file(s)..." >> $ZimLogFile
437
 
438
   # Remove backup files matching criteria
439
   find $ZimBackupPath -maxdepth 1 -type f -mmin +$ZimDeleteTimeSet -name ZimBackup\*.tar.gz -exec rm {} +  >> $ZimLogFile
440
 
441
   # Send task stop time to log
442
   echo "`date "+%Y-%m-%d %H:%M:%S"` - Deleting local file(s) done." >> $ZimLogFile
443
 
444
  else
445
 
446
   # Remove backup files matching criteria
447
   find $ZimBackupPath -maxdepth 1 -type f -mmin +$ZimDeleteTimeSet -name ZimBackup\*.tar.gz -exec rm {} +
448
 
449
 fi
450
fi
451
}
452
 
453
log_start() {
454
if [ $ZimLogEnable = 'yes' ]
455
 then
456
 
457
  # Sending backup start time to log
458
  echo "" >> $ZimLogFile
459
  echo "-------------------------------------------------------" >> $ZimLogFile
460
  echo "Backup Started: `date "+%Y-%m-%d %H:%M:%S"` Type: $ZimBackupType" >> $ZimLogFile
461
 
462
fi
463
}
464
 
465
log_end() {
466
if [ $ZimLogEnable = 'yes' ]
467
 then
468
 
469
  # Sending backup stop time to log
470
  echo "Backup Finished: `date "+%Y-%m-%d %H:%M:%S"` Type: $ZimBackupType" >> $ZimLogFile
471
  echo "-------------------------------------------------------" >> $ZimLogFile
472
 
473
fi
474
}
475
 
476
zimbra_start() {
477
if [ "$2" = "--no-start" ]
478
then
479
  # If --no-start is set ignore starting Zimbra
480
  echo "`date "+%Y-%m-%d %H:%M:%S"` - Zimbra services not starting, --no-start is set." >> $ZimLogFile
481
  echo "Zimbra services not starting, --no-start is set."
482
else
483
 # Starting Zimbra
484
 if [ $ZimLogEnable = 'yes' ]
485
  then
486
   # Sending task start time to log
487
   echo "`date "+%Y-%m-%d %H:%M:%S"` - Zimbra services starting..." >> $ZimLogFile
488
 
489
   # Starting Zimbra
490
   su zimbra -c -l "zmcontrol start" >> $ZimLogFile
491
 
492
   # Send task stop time to log
493
   echo "`date "+%Y-%m-%d %H:%M:%S"` - Zimbra services started." >> $ZimLogFile
494
 
495
  else
496
 
497
   # Starting Zimbra
498
   su zimbra -c -l "zmcontrol start"
499
 
500
  fi
501
fi
502
}
503
 
504
zimbra_stop() {
505
# Stopping Zimbra
506
if [ $ZimLogEnable = 'yes' ]
507
 then
508
 
509
  # Sending task start time to log
510
  echo "`date "+%Y-%m-%d %H:%M:%S"` - Zimbra services stopping..." >> $ZimLogFile
511
 
512
  # Stopping Zimbra
513
  su zimbra -c -l "zmcontrol stop" >> $ZimLogFile
514
 
515
  # Sending task stop time to log
516
  echo "`date "+%Y-%m-%d %H:%M:%S"` - Zimbra services stopped." >> $ZimLogFile
517
 
518
  # Sleep for 10 seconds to give shutdown some extra time before backup starts
519
  sleep 10
520
 
521
else
522
 
523
  # Stopping Zimbra
524
  su zimbra -c -l "zmcontrol stop"
525
 
526
  # Sleep for 10 seconds to give shutdown some extra time before backup starts
527
  sleep 10
528
 
529
fi
530
}
531
 
532
full_restore() {
533
echo "Full Restore under development"
534
}
535
 
536
diff_restore() {
537
echo "Diff Restore under development"
538
}
539
 
540
backup_file() {
541
# Checks what backup is choosen and sets file-name appropiate
542
 
543
# Full system backup
544
if [ "$ZimBackupType" = "--full" ]
545
then
546
 ZimBackupFile=ZimBackupSystemFull_`date +%Y%m%d%H%M`.tar.gz
547
fi
548
 
549
# Differential system backup
550
if [ "$ZimBackupType" = "--diff" ]
551
then
552
 ZimBackupFile=ZimBackupSystemDiff_`date +%Y%m%d%H%M`.tar.gz
553
fi
554
 
555
# Full message backup
556
if [ "$ZimBackupType" = "--msg-full" ]
557
then
558
 ZimBackupFile=ZimBackupMsgFull_`date +%Y%m%d%H%M`.tar.gz
559
fi
560
 
561
# Differential message backup
562
if [ "$ZimBackupType" = "--msg-diff" ]
563
then
564
 ZimBackupFile=ZimBackupMsgDiff_`date +%Y%m%d%H%M`.tar.gz
565
fi
566
}
567
 
568
log_file() {
569
# Check to see if the log folder exist, create if not
570
mkdir -p $ZimLogPath
571
 
572
# Check log intervall and decide which file-name to use
573
if [ "$ZimLogLogRotate" = "yes" ]
574
then
575
 
576
 # Set log file-name to day
577
 if [ "$ZimLogRotateInt" = "day" ]
578
 then
579
 
580
  # If file do not exist create it
581
  touch $ZimLogPath/ZimBackupDay_`date +%Y-%m-%d`.log
582
 
583
  # Set correct file-name
584
  ZimLogFile=$ZimLogPath/ZimBackupDay_`date +%Y-%m-%d`.log
585
 fi
586
 
587
 # Set log file-name to week
588
 if [ "$ZimLogRotateInt" = "week" ]
589
 then
590
 
591
  # If file do not exist create it
592
  touch $ZimLogPath/ZimBackupWeek_`date +%Y-%V`.log
593
 
594
  # Set correct file-name
595
  ZimLogFile=$ZimLogPath/ZimBackupWeek_`date +%Y-%V`.log
596
 fi
597
 
598
 # Set log file-name to month
599
 if [ "$ZimLogRotateInt" = "month" ]
600
 then
601
 
602
  # If file do not exist create it
603
  touch $ZimLogPath/ZimBackupMonth_`date +%Y-%m`.log
604
 
605
  # Set correct file-name
606
  ZimLogFile=$ZimLogPath/ZimBackupMonth_`date +%Y-%m`.log
607
 fi
608
 
609
else
610
 
611
 # If file do not exist create it
612
 touch $ZimLogPath/ZimBackup.log
613
 
614
 # Set correct file-name
615
 ZimLogFile=$ZimLogPath/ZimBackup.log
616
 
617
fi
618
}
619
 
620
temp_folders() {
621
# Create & set temp folders under $ZimBackupPath
622
ZimFullTmpPath=$ZimBackupPath/tf
623
ZimDiffTmpPath=$ZimBackupPath/td
624
ZimMsgFullTmpPath=$ZimBackupPath/tmf
625
ZimMsgDiffTmpPath=$ZimBackupPath/tmd
626
mkdir -p $ZimFullTmpPath
627
mkdir -p $ZimDiffTmpPath
628
mkdir -p $ZimMsgFullTmpPath
629
mkdir -p $ZimMsgDiffTmpPath
630
}
631
 
632
restore_guide() {
633
echo "Restore Guide"
634
}
635
 
636
script_help() {
637
# Show help for script
638
echo "oZimBackup.sh Usage:"
639
echo ""
640
echo "--full			For full backup (Cold)"
641
echo "--diff			For differential backup (Cold)"
642
echo "--msg-full		For complete message backup (Hot)"
643
echo "--msg-diff		For differential message backup (Hot)"
644
echo "--check			Check if needed software is installed, depends"
645
echo "				on script configuration"
646
echo "--restore-guide		Shows a short guide on how to restore Zimbra"
647
#echo "--full-restore		Does a complete restore"
648
#echo "--diff-restore		Restores a differential backup"
649
echo "--help			Shows this help"
650
echo ""
651
echo "--no-start		Tells Zimbra to stay offline after backup"
652
#echo "<file to restore>	File to restore from"
653
echo ""
654
echo "Ex. Full backup:"
655
echo " oZimBackup.sh --full"
656
echo ""
657
#echo "Ex. Complete restore:"
658
#echo " oZimBackup.sh --full-restore /path/to/file/zimbackup.tar.gz"
659
echo ""
660
echo "Ex. Full differential backup leaving Zimbra in offline mode:"
661
echo " oZimBackup.sh --diff --no-start"
662
echo ""
663
}
664
 
665
case $1 in
666
--full)
667
pre_check
668
pre_load
669
log_start
670
full_backup
671
file_transfer
672
log_end
673
;;
674
--diff)
675
pre_check
676
pre_load
677
log_start
678
diff_backup
679
file_transfer
680
log_end
681
;;
682
--msg-full)
683
pre_check
684
pre_load
685
log_start
686
msgfull_backup
687
file_transfer
688
log_end
689
;;
690
--msg-diff)
691
pre_check
692
pre_load
693
log_start
694
msgdiff_backup
695
file_transfer
696
log_end
697
;;
698
--full-restore)
699
full_restore
700
;;
701
--diff-restore)
702
diff_restore
703
;;
704
--restore-guide)
705
restore_guide
706
;;
707
--check)
708
pre_check
709
;;
710
--help)
711
script_help
712
;;
713
*)
714
script_help
715
;;
716
esac