Daily Full Zimbra Email Backup process





Zimbra is a rockin open source exchange (or as most call it “collaboration suite”) alternative to Microsoft Exchange.  It’s cheaper, just as functional, open source, no Microsoft Software is required, it’s cheaper;…did I mention it’s cheaper?  :)

I’m working on Zimbra Network Edition 7.x and it would seem that by default Zimbra backs up to: /opt/zimbra/backup

Going one folder level below that you find a folder call “sessions”, this is where the core data seems to be for zimbra (/opt/zimbra/backup/sessions).  Here Zimbra makes folders both “full” and “incremental”.  Your full folders will have a name of “full*”, the * meaning some variable by date like month day year (20110911.something).  To make a LONG story short for my requirements I simply needed to make 6 or 7 full backups (“daily” backups).  Here’s what I wanted:

Backups: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday <- Full

* Although incremental backups are part of the default schema and they only do a full backup at the end of the week I find it much easier with respect to backups to do full backups in case you need to restore them you don’t need to “incrementally restore” your backup which saves me time!

My process:  Zimbra > Local backup to /opt/zimbra/backup/sessions > RSYNC to FreeNAS > RSYNC “daily” offsite

I could technically RSYNC from Zimbra offsite but I’d rather let FreeNAS do the heavy lifting so I don’t impact my production email server.

Great, we have a plan but how to implement it?  How do you accomplish the task of:

Renaming the full* folder by the current day?

Here’s how…

Create a shell script:

nano today.sh

Put these contents inside of that file:

#!/bin/sh
# Resource: http://en.wikipedia.org/wiki/Date_%28Unix%29
ZIMBRADATE=$(date +”%A”)
rm -r -f /opt/zimbra/backup/sessions/$ZIMBRADATE
mv /opt/zimbra/backup/sessions/full* /opt/zimbra/backup/sessions/$ZIMBRADATE
cp -r /opt/zimbra/backup/sessions/$ZIMBRADATE/* /opt/zimbra/backup/sessions/daily

* FYI – since I posted this I’ve modified this script SEVERAL times.  It’s because I kept forgetting full paths!  Works flawlessly now ;-)

Done!  What this does is creates a variable called ZIMBRADATE, the actual command “date +”%A” is a Linux command to display the date but make it show us the “Day” so it would show us “Thursday” if that was today or “Sunday”, which ever day is today when running the command.

So if you “echo $ZIMBRADATE” instead of “rm -r -f $ZIMBRADATE” you’ll see the output.  Once we get our variable which is today’s day (I’ll use Friday in my examples) the commands where you see the variable $ZIMBRADATE are actually “Friday”.  Just to clarify, I made the word $ZIMBRADATE up, you could use $CHEESEPUFFS or $WHOCARES – it’s an arbitrary thing what’s important is what comes after it “=$(date +”%A”), you can try using other commands to after a variable and if your code was LONG, doing repetitive tasks, etc you would definitely want to do that but for us we only need one.

rm -r -f $ZIMBRADATE becomes the command “rm -r -f Friday”.  We then “move” any folder name that the stock Zimbra backup made because that folder name always starts out with “full” to “Friday”, the “mv” command is “move” but you’re essentially “renaming it”.  Linux common sense isn’t necessarily my common sense but they might know better anyway :p.

Finally we “cp” or “copy” the Friday folder to “Daily” which gets overwritten with the current day’s data (Friday’s data).  All of that then sync’s to our backup server, the backup server then sync’s the daily folder offsite for safe keeping in case of a disaster.

You’ll also want to go into your cron tasks for “zimbra” and edit a line:

Login to your server

sudo -i

su – zimbra

crontab -e

There’s a section in the crontab that should look like this:
* Although I have a comment for my own use “My Edit…”

# Backups
#
# BACKUP BEGIN
# My Edit – Full backups 7 days a week
# 0 1 * * 6 /opt/zimbra/bin/zmbackup -f -a  all –mail-report
0 1 * * 0-6 /opt/zimbra/bin/zmbackup -f -a  all –mail-report
# My Edit – comment out incremental backups
# 0 1 * * 0-5 /opt/zimbra/bin/zmbackup -i  –mail-report
0 0 * * * /opt/zimbra/bin/zmbackup -del 1m –mail-report
# BACKUP END

Anything with a “#” is commented out and not read by the script so I kept the original line in there just in case. zmbackup is the Zimbra Backup command, you can type:  zmbackup –help for information on it but it’s pretty basic.  The above method of folder renaming isn’t possible with zmbackup, you have to run a second process like we’ve done to manipulate the default output of zmbackup.

Is anything ever perfect?  I forgot yet another thing and I’m updating this post.  You need to (as root) edit your cron to include the folder gymnastics we put in place with the above script.

As root then (sudo -i or if you’re in as the zimbra user type “exit” and go back to root) do a:  crontab -e, add this line to your root user crontab:

15 3 * * 0-6 bash /full/path/to/your/script/today.sh > /var/log/today.log

* I encourage you to name your script and log something different :-)

Once you do that it’ll run at 3:15am every day, you might want to adjust that time too.   Seems by default my Zimbra backup gets done at a little after 1am.

Now you’re done, you’ve got static folders for every day of the week and one called “daily” which you can now copy offsite.  You’ll also want to make sure you have the accounts.xml from /opt/zimbra/backup.

Any comments are appreciated or if you have questions I’m always happy to help.