How To Backup Mailbox Daily, Weekly and Monthly

For backup mailbox, i am usually using zmmailbox command who has been provided by Zimbra. The command usually will backup all mailbox on the users. But in this case, i want to backup mailbox daily, weekly, monthly or by certain time/date. For to do that, i could using the simple script and execute every night by crontab.

# Make file backup-mailbox.sh in /srv directory

view sourceprint?

1.vi /srv/backup-mailbox.sh

Fill with the following line

##!/bin/bash
clear

## Backup Format 
FORMAT=tgz

## Backup location
ZBACKUP=/srv/backup/

## Folder name for backup and using date
DATE=`date +"%d%m%y"`

## Backup location separate by date
ZDUMPDIR=$ZBACKUP/$DATE

## zmmailbox location
ZMBOX=/opt/zimbra/bin/zmmailbox

### Backup Option ###

## Based on few day ago until today, example 7 days ago

#HARI=`date --date='7 days ago' +"%m/%d/%Y"`
#query="&query=after:$HARI"

## Based on certain date , example 21 Jan 2015.

#query="&query=date:01/21/2015"

## Based from/to certain date. Example Backup Mailbox before 21 Jan 2015 and after 10 Jan 2015

#query="&query=after:01/10/2015 before:01/21/2015"

if [ ! -d $ZDUMPDIR ]; then
        mkdir -p $ZDUMPDIR
fi

## Looping Account Zimbra
for account in `su - zimbra -c 'zmprov -l gaa | sort'`
do
echo "Processing mailbox $account backup..."
        $ZMBOX -z -m $account getRestURL "//?fmt=${FORMAT}$query" > $ZDUMPDIR/$account.${FORMAT}
done

echo "Zimbra Mailbox backup has been completed successfully."

Note : The above script has 3 method backup. First backup by few days ago. Second backup by certain date and third backup based on from/to certain date. Don’t forget to remove # 1 of 3 method what do you want. Save the script and give execution access

view sourceprint?

1.chmod +x /srv/backup-mailbox.sh

2.sh /srv/backup-mailbox.sh

If you want to execute every night, you can place the script in the crontab for automatically execute

Good luck and hopefully useful 

How To Restore Zimbra Mailbox

After success backup mailbox as described from previous article on this section : Zimbra Tips : How To Backup Mailbox Daily, Weekly and Monthly, you could restore the backup mailboxes with zmmailbox command who has been provided by Zimbra. The example command is like below :

su - zimbra -c "zmmailbox -z -m username postRestURL '//?fmt=tgz&resolve=skip' folder/file-location.tgz";

Note :
resolve = skip. This parameters will keep mailboxes existing on users.

resolve = reset. This parameters will delete mailboxes existing on user and will be changed with mailbox backup. The reset parameter could be changed with skip, replace, modify.

folder/file-location.tgz is folder/file location backup mailboxes.

Example

su - zimbra -c "zmmailbox -z -m admin@example.com postRestURL '//?fmt=tgz&resolve=skip' /srv/backup/20150128/admin@example.com.tgz";

The above command will be restore backup mailboxes admin@example.com user with reset resolver and the backup file is located in /srv/backup/20150128/admin@example.com.tgz

For restore backup all user, you could make a script as below :

#!/bin/bash

BACKUPDIR="/srv/backup/20150128";

clear

echo "Retrieve all zimbra user name..."

USERS=`su - zimbra -c 'zmprov -l gaa | sort'`;

for ACCOUNT in $USERS; do
NAME=`echo $ACCOUNT`;
echo "Restoring $NAME mailbox..."
su - zimbra -c "zmmailbox -z -m $NAME postRestURL '//?fmt=tgz&resolve=skip' $BACKUPDIR/$NAME.tgz";
done
echo "All mailbox has been restored sucessfully"

Good luck and hopefully useful 



https://www.zextras.com/suite/migration-tool/