#!/bin/bash
#
#    Script to backup Zimbra OSS by stopping the service and
#    rsyncing to a destination, then restarting the service
#    This script is partially based on the scripts at
#     http://wiki.zimbra.com/index.php?title=Open_Source_Edition_Backup_Procedure
#
#    Copyright (C) 2008 Al Twohill <al-at-hol-dot-net-dot-nz>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License version 2 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
#    Or download it from http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
#### Modify these values as required for your installation
# local dir to backup to while zimbra is stopped
localPath=/opt/backup
# remote directory to backup to (as defined by rsync) (leave blank to skip)
#remotePath=zmbackup@sarge::zmbackup/
remotePath=
# password file to use with rsync (leave blank if not required)
#passwordFile=/opt/backup-password
passwordFile=
# use rysnc verbosely (leave blank to keep it quiet)
V=
#### You shouldn't need to modify below here<br />
say() {
        MESSAGE_PREFIX="zimbra backup:"
        MESSAGE="$1"
        TIMESTAMP=$(date +"%F %T")
        echo -e "$TIMESTAMP $MESSAGE_PREFIX $MESSAGE"
        logger -t $log_tag -p $log_facility.$log_level "$MESSAGE"
        logger -t $log_tag -p $log_facility_mail.$log_level "$MESSAGE"
        }
error ()  {
        MESSAGE_PREFIX="zimbra backup:"
        MESSAGE="$1"
        TIMESTAMP=$(date +"%F %T")
        echo -e $TIMESTAMP $MESSAGE >&2
        logger -t $log_tag -p $log_facility.$log_level_err "$MESSAGE"
        logger -t $log_tag -p $log_facility_mail.$log_level_err "$MESSAGE"
        exit
        }
say "backup started"
say "stopping the Zimbra services, this may take some time"
/etc/init.d/zimbra stop || error "error stopping Zimbra"
say "rsyncing the snapshot to the local directory $localPath"
rsync -aHk$V --delete /opt/zimbra/ $localPath || "error during local rsync but continuing script"
say "restarting Zimbra in the background"
(/etc/init.d/zimbra start && say "Zimbra: Services background startup completed") || error "services background startup FAILED" &

#
#if
#        then 
#	 say "begining rsync to remote directory"
#        rsync -aHk$V  --delete --password-file=$passwordFile $localPath $remotePath
#fi
#

say "finished backup script"

