Simple backup script for Single Server Zimbra Community
Posted on | July 11, 2014 | Comments Off on Simple backup script for Single Server Zimbra Community
This is just a simple script that I wrote to manage backing a singer server installation of Zimbra Community. It needs some attention, additional tests, etc. but works as is with zimbra 8.x on an ubuntu 12.04 server installation. Of course you will need to change ip addresses to suit your environment…
—– start of script —–
#!/bin/bash
# 6.4.14
# run this as root
# should we run zmcontrol stop before we backup ldap
# exclude the ldap database directory from the rsync operation
# this assumes an ssh connection to the destination for the rsync
SOURCE=’/opt/zimbra’
DEST=’192.168.113.3:/opt’
EXCLUDE=’data.mdb’
LDAP_SRC=’/opt/zimbra/data/ldap/mdb/db’
LDAP_DEST_LOCAL=’/opt/ldapbackup’
LDAP_DEST_REMOTE=’192.168.113.3:/opt/zimbra/data/ldap/mdb/db/’
BACKUPLOG=’/opt/backuplog/backup_$(date +%a).log’
MDBCOPY_LOC=’/opt/zimbra/openldap/bin/mdb_copy’
RSYNC_LOC=’/usr/bin/rsync’
if [ x`whoami` != xroot ]; then
echo Error: must be run as root user
exit 1
fi
# exit 0
# create a log file with the date2
# need to test for existence of directory – if not exist make the directory – then
touch /opt/backuplog/backup_$(date +”%a”).log
# set a start time
START1=$(date +%s)
# send the start time to the log file
echo “Time backup started = $(date +%a) $(date +%T)” >> /opt/backuplog/backup_$(date +%a).log
# execute the live sync…
# test for exist rsync – if not exit else then
# test for exist source and dest – if not either one – exit else
echo syncning files from $SOURCE to $DEST
$RSYNC_LOC -avH –exclude $EXCLUDE –delete $SOURCE $DEST
# set a finish live sync time and calculate the difference
FINISH1=$(date +%s)
# i will use the times to output to a log file later
echo “total time live rsync $(( ($FINISH1-$START1) / 60 )) minutes, $(( ($FINISH1-$START1) % 60 )) seconds” >> /opt/backuplog/backup_$(date +%a).log
# start the clock again
START2=$(date +%s)
# next backup the ldap databse to a local spot – may not work if there is a file there
# test for exist – if not exist – exit else
$MDBCOPY_LOC $LDAP_SRC $LDAP_DEST_LOCAL
# now sync the database with the remote destination
$RSYNC_LOC -turv $LDAP_DEST_LOCAL/data.mdb $LDAP_DEST_REMOTE
# run rsync again to try and catch any files that might have been in use
# sync again – should not take long
$RSYNC_LOC -avH –exclude $EXCLUDE –delete $SOURCE $DEST
# here we should gather some info on the condition of the server, etc and stick it in the
log file.
FINISH2=$(date +%s)
echo “total time services down: $(( ($FINISH2-$START2) / 60 )) minutes, $(( ($FINISH2-$START2) % 60 )) seconds” >> /opt/backuplog/backup_$(date +%a).log
# Display Zimbra services status
echo “Displaying Zimbra services status” >> /opt/backuplog/backup_$(date +%a).log
su – zimbra -c/opt/zimbra/bin/zmcontrol status >> /opt/backuplog/backup_$(date +%a).log
#
# We need to clean up some things like removing the data.mdb file from /opt/ldapbackup because mdb_copy destination has to be empty
#
echo “Good bye” >> /opt/backuplog/backup_$(date +%a).log
exit 0
#### will grow this after some testing ####