Subversion Repositories oZimbraBackup

Rev

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

#!/bin/bash

#
# This script will do various backups of Zimbra depending on which you choose,
# most of them are cold backups except the msg backup which hot copies the 'store'
# folder for possible individual mail retrieval. Be sure to change the variables below
# to point to where you got the Zimbra folder and where you want it to be backed up to.
#
# This script must be run as root or a user with equal privileges or it will not work.
#
# When you run this script via crontab be sure to add '> /dev/null 2>&1' at the end
# of the script like below or the tar command will fail for no apparent reason.
# 00 12 * * * oZimbraBackup.sh --full > /dev/null 2>&1
#
# As of 2008-04-16 this script uses some extra software to extend this script,
# the scripts standard function will still function without these extra software's but if you
# intend to use this scripts built in file transfer functions you must install the described software below.
#
# Required software: rsync for normal operation and ftp or scp & expect for file transfer capabillities.
#
# Bits and pieces was adopted from a script created by Daniel W. Martin, 9 Sept 2007
# Licensed under BSDL license, see license.txt for information.
#
# Developer: oSource Development(as of 2009-07-01)
# DevTeam: Marcus Uddenhed
# Version: 1.0.6
# Updated: 2014-05-18 16:00
#

#### Global Settings ####
ZimInstPath=/opt                        # Installation path for Zimbra, excluding the Zimbra folder.
ZimHome=zimbra                          # The Zimbra installation folder, excluding path to folder.
ZimBackupPath=/opt/backup               # Root folder for backup where backup files will be placed.

#### Log Settings ####
ZimLogEnable=yes                        # Turns logging on or off(yes/no).
ZimLogLogRotate=no                      # Enables log rotating(yes/no)
ZimLogRotateInt=day                     # How often should we rotate logs(day, week or month)
ZimLogPath=/opt/logs                    # Folder for log files
ZimLogVerbose=no                        # Activates extra logging information(yes/no)

#### File Transfer Settings ####

# Enable Services (yes/no)
ZimFtpEnable=no                         # Enable/Disable ftp file transfer(yes/no)
ZimScpEnable=no                         # Enable/Disable scp file transfer(yes/no)

# Extra FTP Settings
ZimFtpOpt=''                            # Extra options for ftp file transfer, see manual for ftp command

# Extra SCP Settings
ZimScpOpt=''                            # Extra options for scp file transfer, see manual for scp command

# Common Settings
ZimFilehostUser=                        # Username for file transfers
ZimFilehostPass=                        # Password for file transfers
ZimFilehostAddress=                     # Host address for file transfers
ZimFilehostFolder=                      # Folder on host where files will be placed during file transfer

#### File Delete Settings ####
ZimDeleteLocalFile=no                   # Enable/Disable compressed backup file deletion after successful backup(yes/no)
ZimDeleteTimeSet=0                      # Set in minutes above 0 to keep a desired amount of files locally,
                                        # be sure to match the time with your backup schedules.

##### Do not change anything below this line unless you know what you are doing #####

# Fetch backup type
ZimBackupType=$1

pre_check() {
# Set default abort value
ZimAbort=0

# Check for rsync
if [ ! -e /usr/bin/rsync ]
 then
  echo "Cannot find rsync software..."
  ZimAbort=1
fi

# Check for tar
if [ ! -e /bin/tar ]
 then
  echo "Cannot find tar software..."
  ZimAbort=1
fi

# Check for gzip
if [ ! -e /bin/gzip ]
 then
  if [ ! -e /usr/bin/gzip ]
   then
    echo "Cannot find gzip software..."
    ZimAbort=1
  fi
fi

# Check if expect, ftp & scp is installed when file transfer is set to yes
if [ $ZimFtpEnable = 'yes' ] || [ $ZimScpEnable = 'yes' ]
then
 if [ ! -e /usr/bin/expect ]
  then
   echo "cannot find expect command..."
   ZimAbort=1
 fi
 if [ $ZimFtpEnable = 'yes' ]
 then
  if [ ! -e /usr/bin/ftp ]
   then
    echo "Cannot find ftp command..."
    ZimAbort=1
  fi
 fi

 if [ $ZimScpEnable = 'yes' ]
 then
  if [ ! -e /usr/bin/scp ]
   then
    echo "Cannot find scp command..."
    ZimAbort=1
  fi
 fi
fi

if [ $ZimAbort = '1' ]
 then
  echo "Please install above software, script exiting..."
  exit
fi
}

pre_load() {
# Get temporary folder layout
temp_folders

# Get backup file format
backup_file

# Get log file format
log_file

# Check to see if the backup folder exist, create if not
mkdir -p $ZimBackupPath

# Check which zimbra version that is installed, for recovery purposes
sudo -u zimbra $ZimInstPath/$ZimHome/bin/zmcontrol -v > $ZimBackupPath/zimbra_version.txt
}

full_backup() {
if [ $ZimLogEnable = 'yes' ] && [ $ZimLogVerbose = 'yes' ]
 then

   # Hot sync before shutdown on zimbra folder
   echo "`date "+%Y-%m-%d %H:%M:%S"` - Hot syncing to backup folder..." >> $ZimLogFile
   rsync -avHK --delete $ZimInstPath/$ZimHome $ZimFullTmpPath >> $ZimLogFile
   echo "`date "+%Y-%m-%d %H:%M:%S"` - Hot syncing to backup folder done." >> $ZimLogFile

   # Stopping Zimbra
   zimbra_stop

   # Cold sync of zimbra folder
   echo "`date "+%Y-%m-%d %H:%M:%S"` - Cold syncing to backup folder..." >> $ZimLogFile
   rsync -avHK --delete $ZimInstPath/$ZimHome $ZimFullTmpPath >> $ZimLogFile
   echo "`date "+%Y-%m-%d %H:%M:%S"` - Cold syncing to backup folder done." >> $ZimLogFile

   # Starting Zimbra
   zimbra_start 

   # Compressing backup for space reduction
   echo "`date "+%Y-%m-%d %H:%M:%S"` - Compressing backup folder..." >> $ZimLogFile
   tar -zcvpf $ZimBackupPath/$ZimBackupFile -C $ZimBackupPath tf zimbra_version.txt >> $ZimLogFile
   echo "`date "+%Y-%m-%d %H:%M:%S"` - Compressed backup folder." >> $ZimLogFile

   # Cleaning differential folder.
   echo "`date "+%Y-%m-%d %H:%M:%S"` - Cleaning differential backup folder..."
   rm -r -f $ZimDiffTmpPath/* >> $ZimLogFile
   echo "`date "+%Y-%m-%d %H:%M:%S"` - Cleaned differential backup folder."
   
 else

  # Hot sync before shutdown on zimbra folder
  rsync -avHK --delete $ZimInstPath/$ZimHome $ZimFullTmpPath

  # Stopping Zimbra
  zimbra_stop

  # Cold sync of zimbra folder
  rsync -avHK --delete $ZimInstPath/$ZimHome $ZimFullTmpPath

  # Starting Zimbra
  zimbra_start

  # Compressing backup for space reduction
  tar -zcvpf $ZimBackupPath/$ZimBackupFile -C $ZimBackupPath tf zimbra_version.txt
  
  # Cleaning differential folder.
   rm -r -f $ZimDiffTmpPath/*
fi
}

diff_backup() {
if [ $ZimLogEnable = 'yes' ] && [ $ZimLogVerbose = 'yes' ]
 then
  # Cleaning backup folder
  #echo "`date "+%Y-%m-%d %H:%M:%S"` - Cleaning backup folder..." >> $ZimLogFile
  #rm -r -f $ZimDiffTmpPath/* >> $ZimLogFile
  #echo "`date "+%Y-%m-%d %H:%M:%S"` - Cleaning backup folder done." >> $ZimLogFile

  # Hot syncing to backup folder
  echo "`date "+%Y-%m-%d %H:%M:%S"` - Hot syncing to backup folder..." >> $ZimLogFile
  rsync -avHK --compare-dest=$ZimFullTmpPath/ $ZimInstPath/$ZimHome $ZimDiffTmpPath >> $ZimLogFile
  echo "`date "+%Y-%m-%d %H:%M:%S"` - Hot syncing to backup folder done." >> $ZimLogFile

  # Stopping Zimbra
  zimbra_stop

  # Cold syncing to backup folder
  echo "`date "+%Y-%m-%d %H:%M:%S"` - Cold syncing to backup folder..." >> $ZimLogFile
  rsync -avHK --compare-dest=$ZimFullTmpPath/ $ZimInstPath/$ZimHome $ZimDiffTmpPath >> $ZimLogFile
  echo "`date "+%Y-%m-%d %H:%M:%S"` - Cold syncing to backup folder done." >> $ZimLogFile

  # Starting Zimbra
  zimbra_start

  # Syncing backup folders
  #echo "`date "+%Y-%m-%d %H:%M:%S"` - Syncing backup folders..." >> $ZimLogFile
  #cp -R $ZimDiffTmpPath/* $ZimFullTmpPath/ >> $ZimLogFile
  #echo "`date "+%Y-%m-%d %H:%M:%S"` - Syncing backup folders done." >> $ZimLogFile

  # Compressing backup folder
  echo "`date "+%Y-%m-%d %H:%M:%S"` - Compressing backup folder..." >> $ZimLogFile
  tar -zcvpf $ZimBackupPath/$ZimBackupFile -C $ZimBackupPath td zimbra_version.txt >> $ZimLogFile
  echo "`date "+%Y-%m-%d %H:%M:%S"` - Compressing backup folder done." >> $ZimLogFile

 else

  # Hot sync before shutdown on zimbra folder
  #rm -R $ZimDiffTmpPath/*
  rsync -avHK --compare-dest=$ZimFullTmpPath/ $ZimInstPath/$ZimHome $ZimDiffTmpPath

  # Stopping Zimbra
  zimbra_stop

  # Cold sync of zimbra folder
  rsync -avHK --compare-dest=$ZimFullTmpPath/ $ZimInstPath/$ZimHome $ZimDiffTmpPath

  # Starting Zimbra
  zimbra_start

  # Syncing files from diff to full backup folder
  #cp -R $ZimDiffTmpPath/* $ZimFullTmpPath/

  # Compressing backup for space reduction
  tar -zcvpf $ZimBackupPath/$ZimBackupFile -C $ZimBackupPath td zimbra_version.txt
fi
}

msgfull_backup() {
if [ $ZimLogEnable = 'yes' ] && [ $ZimLogVerbose = 'yes' ]
 then
  # Hot syncing to backup folder
  echo "`date "+%Y-%m-%d %H:%M:%S"` - Hot syncing to backup folder..." >> $ZimLogFile
  rsync -avHK --delete $ZimInstPath/$ZimHome/store $ZimMsgFullTmpPath >> $ZimLogFile
  echo "`date "+%Y-%m-%d %H:%M:%S"` - Hot syncing to backup folder done." >> $ZimLogFile

  # Compressing backup folder
  echo "`date "+%Y-%m-%d %H:%M:%S"` - Compressing backup folder..." >> $ZimLogFile
  tar -zcvpf $ZimBackupPath/$ZimBackupFile -C $ZimBackupPath tmf zimbra_version.txt >> $ZimLogFile
  echo "`date "+%Y-%m-%d %H:%M:%S"` - Compressing backup folder done." >> $ZimLogFile
  
  echo "`date "+%Y-%m-%d %H:%M:%S"` - Cleaning differential backup folder..."
  rm -r -f $ZimMsgDiffTmpPath/* >> $ZimLogFile
  echo "`date "+%Y-%m-%d %H:%M:%S"` - Cleaned differential backup folder."

 else
  # Hot syncing to backup folder
  rsync -avHK --delete $ZimInstPath/$ZimHome/store $ZimMsgFullTmpPath

  # Compressing backup folder
  tar -zcvpf $ZimBackupPath/$ZimBackupFile -C $ZimBackupPath tmf zimbra_version.txt
  
  # Cleaning differential folder
  rm -r -f $ZimMsgDiffTmpPath/*
fi
}

msgdiff_backup() {
if [ $ZimLogEnable = 'yes' ] && [ $ZimLogVerbose = 'yes' ]
 then
  # Cleaning backup folder
  #echo "`date "+%Y-%m-%d %H:%M:%S"` - Cleaning backup folder..." >> $ZimLogFile
  #rm -r -f $ZimMsgDiffTmpPath/* >> $ZimLogFile
  #echo "`date "+%Y-%m-%d %H:%M:%S"` - Cleaning backup folder done." >> $ZimLogFile

  # Hot syncing to backup folder
  echo "`date "+%Y-%m-%d %H:%M:%S"` - Hot syncing to backup folder..." >> $ZimLogFile
  rsync -avHK --compare-dest=$ZimMsgFullTmpPath/ $ZimInstPath/$ZimHome/store $ZimMsgDiffTmpPath >> $ZimLogFile
  echo "`date "+%Y-%m-%d %H:%M:%S"` - Hot syncing to backup folder done." >> $ZimLogFile

  # Compressing backup folder
  echo "`date "+%Y-%m-%d %H:%M:%S"` - Compressing backup folder..." >> $ZimLogFile
  tar -zcvpf $ZimBackupPath/$ZimBackupFile -C $ZimBackupPath tmd zimbra_version.txt >> $ZimLogFile
  echo "`date "+%Y-%m-%d %H:%M:%S"` - Compressing backup folder done." >> $ZimLogFile

 else
  # Cleaning backup folder
  #rm -r -f $ZimMsgDiffTmpPath/*

  # Hot syncing to backup folder
  rsync -avHK --compare-dest=$ZimMsgFullTmpPath/ $ZimInstPath/$ZimHome/store $ZimMsgDiffTmpPath

  # Compressing backup folder
  tar -zcvpf $ZimBackupPath/$ZimBackupFile -C $ZimBackupPath tmd zimbra_version.txt
fi
}

file_transfer() {
# Transfer with ftp
if [ $ZimFtpEnable == "yes" ]
then
if [ "$2" = "--no-send" ]
then
  # If --no-send is set ignore sending file to off site.
  echo "`date "+%Y-%m-%d %H:%M:%S"` - Backup not sending backup file to external storage, --no-send is set." >> $ZimLogFile
  echo "Not sending backup to external storage, --no-send is set."
else
if [ $ZimLogEnable = 'yes' ] && [ $ZimLogVerbose = 'yes' ]
 then
  # Send task start time to log
  echo "`date "+%Y-%m-%d %H:%M:%S"` - Sending file via ftp to off-site storage..." >> $ZimLogFile

  # Make a temporary script for expect commands
  touch $ZimBackupPath/ftp.exp

  # Fill script with commands
  echo '#!/usr/bin/expect --' >> $ZimBackupPath/ftp.exp
  echo 'set timeout -1' >> $ZimBackupPath/ftp.exp
  echo 'spawn ftp '$ZimFilehostAddress >> $ZimBackupPath/ftp.exp
  echo 'expect ):' >> $ZimBackupPath/ftp.exp
  echo 'send '$ZimFilehostUser'\r' >> $ZimBackupPath/ftp.exp
  echo 'expect :' >> $ZimBackupPath/ftp.exp
  echo 'send '$ZimFilehostPass'\r' >> $ZimBackupPath/ftp.exp
  echo 'expect >' >> $ZimBackupPath/ftp.exp
  echo 'send '$ZimFtpOpt'\r' >> $ZimBackupPath/ftp.exp
  echo 'send "send '$ZimBackupPath/$ZimBackupFile $ZimFilehostFolder/$ZimBackupFile'\r"' >> $ZimBackupPath/ftp.exp
  echo 'expect >' >> $ZimBackupPath/ftp.exp
  echo 'send quit\r' >> $ZimBackupPath/ftp.exp
  echo 'expect closed' >> $ZimBackupPath/ftp.exp

  # Run expect with created script
  expect $ZimBackupPath/ftp.exp >> $ZimLogFile

  # Delete temporary expect script
  rm $ZimBackupPath/ftp.exp

  # Send task stop time to log
  echo "`date "+%Y-%m-%d %H:%M:%S"` - Sending file via ftp to off-site storage done." >> $ZimLogFile

 else

  # Send task start time to log
  echo "`date "+%Y-%m-%d %H:%M:%S"` - Sending file via ftp to off-site storage..." >> $ZimLogFile

  # Make a temporary script for expect commands
  touch $ZimBackupPath/ftp.exp

  # Fill script with commands
  echo '#!/usr/bin/expect --' >> $ZimBackupPath/ftp.exp
  echo 'set timeout -1' >> $ZimBackupPath/ftp.exp
  echo 'spawn ftp '$ZimFilehostAddress >> $ZimBackupPath/ftp.exp
  echo 'expect ):' >> $ZimBackupPath/ftp.exp
  echo 'send '$ZimFilehostUser'\r' >> $ZimBackupPath/ftp.exp
  echo 'expect :' >> $ZimBackupPath/ftp.exp
  echo 'send '$ZimFilehostPass'\r' >> $ZimBackupPath/ftp.exp
  echo 'expect >' >> $ZimBackupPath/ftp.exp
  echo 'send '$ZimFtpOpt'\r' >> $ZimBackupPath/ftp.exp
  echo 'send "send '$ZimBackupPath/$ZimBackupFile $ZimFilehostFolder/$ZimBackupFile'\r"' >> $ZimBackupPath/ftp.exp
  echo 'expect >' >> $ZimBackupPath/ftp.exp
  echo 'send quit\r' >> $ZimBackupPath/ftp.exp
  echo 'expect closed' >> $ZimBackupPath/ftp.exp

  # Run expect with created script
  expect $ZimBackupPath/ftp.exp

  # Delete temporary expect script
  rm $ZimBackupPath/ftp.exp

  # Send task stop time to log
  echo "`date "+%Y-%m-%d %H:%M:%S"` - Sending file via ftp to off-site storage done." >> $ZimLogFile

 fi
fi
fi

# Transfer with scp
if [ $ZimScpEnable == "yes" ]
then
if [ "$2" = "--no-send" ]
then
  # If --no-send is set ignore sending file to off site.
  echo "`date "+%Y-%m-%d %H:%M:%S"` - Backup not sending backup file to external storage, --no-send is set." >> $ZimLogFile
  echo "Not sending backup to external storage, --no-send is set."
else
 if [ $ZimLogEnable = 'yes' ] && [ $ZimLogVerbose = 'yes' ]
  then

  # Send task start time to log
   echo "`date "+%Y-%m-%d %H:%M:%S"` - Sending file via scp to off-site storage..." >> $ZimLogFile

   # Make a temporary script for expect commands
   touch $ZimBackupPath/scp.exp

   # Fill script with commands
   echo '#!/usr/bin/expect --' >> $ZimBackupPath/scp.exp
   echo 'set timeout -1' >> $ZimBackupPath/scp.exp
   echo 'spawn scp '$ZimScpOpt $ZimBackupPath/$ZimBackupFile $ZimFilehostUser'@'$ZimFilehostAddress':'$ZimFilehostFolder >> $ZimBackupPath/scp.exp
   echo 'expect :' >> $ZimBackupPath/scp.exp
   echo 'send '$ZimFilehostPass'\r' >> $ZimBackupPath/scp.exp
   echo 'expect closed' >> $ZimBackupPath/scp.exp

   # Run expect with created script
   expect $ZimBackupPath/scp.exp >> $ZimLogFile

   # Delete temporary expect script
   rm $ZimBackupPath/scp.exp

  # Send task stop time to log
   echo "`date "+%Y-%m-%d %H:%M:%S"` - Sending file via scp to off-site storage done." >> $ZimLogFile

  else

   # Send task start time to log
   echo "`date "+%Y-%m-%d %H:%M:%S"` - Sending file via scp to off-site storage..." >> $ZimLogFile

   # Make a temporary script for expect commands
   touch $ZimBackupPath/scp.exp

   # Fill script with commands
   echo '#!/usr/bin/expect --' >> $ZimBackupPath/scp.exp
   echo 'set timeout -1' >> $ZimBackupPath/scp.exp
   echo 'spawn scp '$ZimScpOpt $ZimBackupPath/$ZimBackupFile $ZimFilehostUser'@'$ZimFilehostAddress':'$ZimFilehostFolder >> $ZimBackupPath/scp.exp
   echo 'expect :' >> $ZimBackupPath/scp.exp
   echo 'send '$ZimFilehostPass'\r' >> $ZimBackupPath/scp.exp
   echo 'expect closed' >> $ZimBackupPath/scp.exp

   # Run expect with created script
   expect $ZimBackupPath/scp.exp

   # Delete temporary expect script
   rm $ZimBackupPath/scp.exp

   # Send task stop time to log
   echo "`date "+%Y-%m-%d %H:%M:%S"` - Sending file via scp to off-site storage done." >> $ZimLogFile

 fi
fi
fi

# Remove local file(s) if ZimDeleteLocalFile is set to 'yes'
if [ $ZimDeleteLocalFile == "yes" ]
then
 if [ $ZimLogEnable = 'yes' ] && [ $ZimLogVerbose = 'yes' ]
  then

  # Send task start time to log
   echo "`date "+%Y-%m-%d %H:%M:%S"` - Deleting local file(s)..." >> $ZimLogFile

   # Remove backup files matching criteria
   find $ZimBackupPath -maxdepth 1 -type f -mmin +$ZimDeleteTimeSet -name ZimBackup\*.tar.gz -exec rm {} +  >> $ZimLogFile

   # Send task stop time to log
   echo "`date "+%Y-%m-%d %H:%M:%S"` - Deleting local file(s) done." >> $ZimLogFile

  else

   # Remove backup files matching criteria
   find $ZimBackupPath -maxdepth 1 -type f -mmin +$ZimDeleteTimeSet -name ZimBackup\*.tar.gz -exec rm {} +

 fi
fi
}

log_start() {
if [ $ZimLogEnable = 'yes' ]
 then

  # Sending backup start time to log
  echo "" >> $ZimLogFile
  echo "-------------------------------------------------------" >> $ZimLogFile
  echo "Backup Started: `date "+%Y-%m-%d %H:%M:%S"` Type: $ZimBackupType" >> $ZimLogFile

fi
}

log_end() {
if [ $ZimLogEnable = 'yes' ]
 then

  # Sending backup stop time to log
  echo "Backup Finished: `date "+%Y-%m-%d %H:%M:%S"` Type: $ZimBackupType" >> $ZimLogFile
  echo "-------------------------------------------------------" >> $ZimLogFile

fi
}

zimbra_start() {
if [ "$2" = "--no-start" ]
then
  # If --no-start is set ignore starting Zimbra
  echo "`date "+%Y-%m-%d %H:%M:%S"` - Zimbra services not starting, --no-start is set." >> $ZimLogFile
  echo "Zimbra services not starting, --no-start is set."
else
 # Starting Zimbra
 if [ $ZimLogEnable = 'yes' ]
  then
   # Sending task start time to log
   echo "`date "+%Y-%m-%d %H:%M:%S"` - Zimbra services starting..." >> $ZimLogFile

   # Starting Zimbra
   su zimbra -c -l "zmcontrol start" >> $ZimLogFile

   # Send task stop time to log
   echo "`date "+%Y-%m-%d %H:%M:%S"` - Zimbra services started." >> $ZimLogFile

  else

   # Starting Zimbra
   su zimbra -c -l "zmcontrol start"

  fi
fi
}

zimbra_stop() {
# Stopping Zimbra
if [ $ZimLogEnable = 'yes' ]
 then

  # Sending task start time to log
  echo "`date "+%Y-%m-%d %H:%M:%S"` - Zimbra services stopping..." >> $ZimLogFile

  # Stopping Zimbra
  su zimbra -c -l "zmcontrol stop" >> $ZimLogFile

  # Sending task stop time to log
  echo "`date "+%Y-%m-%d %H:%M:%S"` - Zimbra services stopped." >> $ZimLogFile

  # Sleep for 10 seconds to give shutdown some extra time before backup starts
  sleep 10

else

  # Stopping Zimbra
  su zimbra -c -l "zmcontrol stop"

  # Sleep for 10 seconds to give shutdown some extra time before backup starts
  sleep 10

fi
}

full_restore() {
echo "Full Restore under development"
}

diff_restore() {
echo "Diff Restore under development"
}

backup_file() {
# Checks what backup is choosen and sets file-name appropiate

# Full system backup
if [ "$ZimBackupType" = "--full" ]
then
 ZimBackupFile=ZimBackupSystemFull_`date +%Y%m%d%H%M`.tar.gz
fi

# Differential system backup
if [ "$ZimBackupType" = "--diff" ]
then
 ZimBackupFile=ZimBackupSystemDiff_`date +%Y%m%d%H%M`.tar.gz
fi

# Full message backup
if [ "$ZimBackupType" = "--msg-full" ]
then
 ZimBackupFile=ZimBackupMsgFull_`date +%Y%m%d%H%M`.tar.gz
fi

# Differential message backup
if [ "$ZimBackupType" = "--msg-diff" ]
then
 ZimBackupFile=ZimBackupMsgDiff_`date +%Y%m%d%H%M`.tar.gz
fi
}

log_file() {
# Check to see if the log folder exist, create if not
mkdir -p $ZimLogPath

# Check log intervall and decide which file-name to use
if [ "$ZimLogLogRotate" = "yes" ]
then

 # Set log file-name to day
 if [ "$ZimLogRotateInt" = "day" ]
 then

  # If file do not exist create it
  touch $ZimLogPath/ZimBackupDay_`date +%Y-%m-%d`.log

  # Set correct file-name
  ZimLogFile=$ZimLogPath/ZimBackupDay_`date +%Y-%m-%d`.log
 fi

 # Set log file-name to week
 if [ "$ZimLogRotateInt" = "week" ]
 then

  # If file do not exist create it
  touch $ZimLogPath/ZimBackupWeek_`date +%Y-%V`.log

  # Set correct file-name
  ZimLogFile=$ZimLogPath/ZimBackupWeek_`date +%Y-%V`.log
 fi

 # Set log file-name to month
 if [ "$ZimLogRotateInt" = "month" ]
 then

  # If file do not exist create it
  touch $ZimLogPath/ZimBackupMonth_`date +%Y-%m`.log

  # Set correct file-name
  ZimLogFile=$ZimLogPath/ZimBackupMonth_`date +%Y-%m`.log
 fi

else

 # If file do not exist create it
 touch $ZimLogPath/ZimBackup.log

 # Set correct file-name
 ZimLogFile=$ZimLogPath/ZimBackup.log

fi
}

temp_folders() {
# Create & set temp folders under $ZimBackupPath
ZimFullTmpPath=$ZimBackupPath/tf
ZimDiffTmpPath=$ZimBackupPath/td
ZimMsgFullTmpPath=$ZimBackupPath/tmf
ZimMsgDiffTmpPath=$ZimBackupPath/tmd
mkdir -p $ZimFullTmpPath
mkdir -p $ZimDiffTmpPath
mkdir -p $ZimMsgFullTmpPath
mkdir -p $ZimMsgDiffTmpPath
}

restore_guide() {
echo "Restore Guide"
}

script_help() {
# Show help for script
echo "oZimBackup.sh Usage:"
echo ""
echo "--full                    For full backup (Cold)"
echo "--diff                    For differential backup (Cold)"
echo "--msg-full                For complete message backup (Hot)"
echo "--msg-diff                For differential message backup (Hot)"
echo "--check                   Check if needed software is installed, depends"
echo "                          on script configuration"
echo "--restore-guide           Shows a short guide on how to restore Zimbra"
#echo "--full-restore           Does a complete restore"
#echo "--diff-restore           Restores a differential backup"
echo "--help                    Shows this help"
echo ""
echo "--no-start                Tells Zimbra to stay offline after backup"
#echo "<file to restore>        File to restore from"
echo ""
echo "Ex. Full backup:"
echo " oZimBackup.sh --full"
echo ""
#echo "Ex. Complete restore:"
#echo " oZimBackup.sh --full-restore /path/to/file/zimbackup.tar.gz"
echo ""
echo "Ex. Full differential backup leaving Zimbra in offline mode:"
echo " oZimBackup.sh --diff --no-start"
echo ""
}

case $1 in
--full)
pre_check
pre_load
log_start
full_backup
file_transfer
log_end
;;
--diff)
pre_check
pre_load
log_start
diff_backup
file_transfer
log_end
;;
--msg-full)
pre_check
pre_load
log_start
msgfull_backup
file_transfer
log_end
;;
--msg-diff)
pre_check
pre_load
log_start
msgdiff_backup
file_transfer
log_end
;;
--full-restore)
full_restore
;;
--diff-restore)
diff_restore
;;
--restore-guide)
restore_guide
;;
--check)
pre_check
;;
--help)
script_help
;;
*)
script_help
;;
esac