View Single Post
  #15 (permalink)  
Old 04-01-2008, 10:22 AM
jpbuse jpbuse is offline
Loyal Member
 
Posts: 83
Default

Thanks for the great and simple script. I made a few edits to allow for an archive directory and rsync to a remote server. I only made the edits to the diff_backup and msgdiff_backup functions. I also added a timestamp variable before the calls so the filename would remain consistent throughout the backup, archive and sync.

Code:
#!/bin/bash

# This script will do various backups of Zimbra depending on wich you choose,
# most of them are cold backups except the msg backup wich hot copies the 'store'
# folder for possible individual mail retrival. 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.
#
# Usage: ZimBackup.sh full	- For full backup (Cold)
#	 ZimBackup.sh diff	- For diffrential backup (Cold)
#	 ZimBackup.sh msgfull	- For complete message backup (Hot)
#	 ZimBackup.sh msgdiff	- For diffrential message backup (Hot)
#
# 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 * * * ZimBackup.sh full > /dev/null 2>&1
#
# Bits and pieces was adopted from a script created by Daniel W. Martin, 9 Sept 2007
# Free to use and free of any warranty!  Marcus Uddenhed, 3 feb 2008
#
# Last updated: 2008-04-01

ZimInstPath=/opt
ZimHome=zimbra
ZimBackupPath=/home/backups
ZimTempPath=/home/backups/tmp
ZimArchive=/home/backups/archive

rsyncUser=myuser
rsyncHost=myhost
rsyncPath="~/sync/"
rsyncBWLimit=400

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

pre_check() {
	# Check to see if the tmp folder exists, and create if not
	mkdir -p $ZimTempPath
	# Check to see if the archive directory exists, and create if not
	mkdir -p $ZimArchive
	mkdir -p $ZimArchive/full
	mkdir -p $ZimArchive/msg
	
	# Check which zimbra version that is installed, for recovery purpose 
	sudo -u zimbra $ZimInstPath/$ZimHome/bin/zmcontrol -v > $ZimTempPath/zimbra_version.txt
}

full_backup() {
	# Removing possible old zimbra backup folder
	rm -r -f $ZimTempPath/$ZimHome
	
	# Stopping Zimbra
	/etc/init.d/zimbra stop
	sleep 20
	
	# Backing up zimbra folder
	cp -rv $ZimInstPath/$ZimHome $ZimTempPath/
	
	# Starting Zimbra
	/etc/init.d/zimbra start
	
	# Compressing backup for space reduction
	tar -zcvf $ZimBackupPath/ZimBackupTypeFull_`date +%Y%m%d%H%M`.tar.gz -C $ZimTempPath $ZimHome zimbra_version.txt
}

diff_backup() {
	# Hot sync before shutdown on zimbra folder
	rsync -avHK --delete $ZimInstPath/$ZimHome $ZimTempPath/
	
	# Stopping Zimbra
	/etc/init.d/zimbra stop
	sleep 20
	
	# Cold sync of zimbra folder
	rsync -avHK --delete $ZimInstPath/$ZimHome $ZimTempPath/
	
	# Starting Zimbra
	/etc/init.d/zimbra start
	
	# Get timestamp for file naming
	timestamp=`date +%Y%m%d%H%M`

	# Compressing backup for space reduction and removing unpacked folder
	tar -zcvf $ZimBackupPath/ZimBackupTypeDiff_$timestamp.tar.gz -C $ZimTempPath $ZimHome zimbra_version.txt

	# Move to archive directory
	mv $ZimBackupPath/ZimBackupTypeDiff_$timestamp.tar.gz $ZimArchive/full
	
	# Sending offsite via rsync w/ bandwidth limitation
	rsync -avHK -e ssh --bwlimit=$rsyncBWLimit $ZimArchive/full/ZimBackupTypeDiff_$timestamp.tar.gz $rsyncUser@$rsyncHost:$rsyncPath
}

msgfull_backup() {
	# Removing possible old store backup folder
	rm -r -f $ZimTempPath/store
	
	# Make dir for hot sync
	mkdir -p $ZimTempPath/store
	
	# Hot sync of mailbox messages
	rsync -avHK --delete $ZimInstPath/$ZimHome/store/0 $ZimTempPath/store/
	
	# Compressing store folder for space reduction
	tar -zcvf $ZimBackupPath/ZimBackupTypeMsg_`date +%Y%m%d%H%M`.tar.gz -C $ZimTempPath store zimbra_version.txt
}

msgdiff_backup() {
	# Make dir for hot sync
	mkdir -p $ZimTempPath/store
	
	# Hot sync of mailbox messages
	rsync -avHK --delete $ZimInstPath/$ZimHome/store/0 $ZimTempPath/store/
	
	# Get timestamp for file naming
	timestamp=`date +%Y%m%d%H%M`

	# Compressing store folder for space reduction
	cd $ZimTempPath
	tar -zcvf $ZimBackupPath/ZimBackupTypeMsg_$timestamp.tar.gz -C $ZimTempPath store zimbra_version.txt
	
	# Move to archive
	mv $ZimBackupPath/ZimBackupTypeMsg_$timestamp.tar.gz $ZimArchive/msg
	

}

case $1 in
full)
pre_check
full_backup
;;
diff)
pre_check
diff_backup
;;
msgfull)
pre_check
msgfull_backup
;;
msgdiff)
pre_check
msgdiff_backup
;;
*)
echo "Usage: ZimColdBackup.sh {full|diff|msgfull|msgdiff}"
;;
esac

Last edited by jpbuse : 04-01-2008 at 10:32 AM. Reason: typo
Reply With Quote