Subversion Repositories oZimbraBackup

Rev

Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed

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