Zimbra Tips : How To Backup Mailbox Daily, Weekly and Monthly

Home » Zimbra » Zimbra Tips : How To Backup Mailbox Daily, Weekly and Monthly
Zimbra 42 Comments

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

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

chmod +x /srv/backup-mailbox.sh
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 😀

42 thoughts on - Zimbra Tips : How To Backup Mailbox Daily, Weekly and Monthly

  • I m new in linux and I install zimbra mail server and want to backup of my all mail box, when I run this script received an error

    /srv/backup-mailbox.sh: line 34: [: /srv/backup/date: binary operator expected

    /srv/backup-mailbox.sh: line 39: syntex error near unexpected token ‘|’

    /srv/backup-mailbox.sh: line 39: ‘for account in ‘su -zimbra -c ‘zmprov -l gaa | sort”’

    • Hi Rao,

      Are you has been modify the script for your purpose? the script is provided 3 method backup. First backup by few days ago. Second backup by certain date and third backup based on from/to certain date. You should choose one for the backup

  • hi iman,

    I need to full backup everyday.After the backup job succesfully completed it should be delete previous day’s backup.What can i do for this backup?

  • hi iman,

    how to modify your backup script if i have a multi domain in one server??
    Tried using daily script but I got error.

    Thanks

  • Same error on all users, I have four domain in one server..

    I used this script:
    ## Based on few day ago until today, example 1 days ago
    HARI=`date –date=’1 days ago’ +”%m/%d/%Y”`
    query=”&query=after:$HARI”

  • hi iman,
    below is the results:

    ::service mta::
    notBefore=Jul 8 02:49:50 2015 GMT
    notAfter=Jul 6 02:49:50 2020 GMT
    subject= /C=US/ST=N/A/O=Zimbra Collaboration Server/OU=Zimbra Collaboration Server/CN=mail.testmailone.com.ph
    issuer= /C=US/ST=N/A/L=N/A/O=Zimbra Collaboration Server/OU=Zimbra Collaboration Server/CN=mail.testmailone.com.ph
    SubjectAltName=
    ::service proxy::
    notBefore=Jul 8 02:49:50 2015 GMT
    notAfter=Jul 6 02:49:50 2020 GMT
    subject= /C=US/ST=N/A/O=Zimbra Collaboration Server/OU=Zimbra Collaboration Server/CN=mail.testmailone.com.ph
    issuer= /C=US/ST=N/A/L=N/A/O=Zimbra Collaboration Server/OU=Zimbra Collaboration Server/CN=mail.testmailone.com.ph
    SubjectAltName=

  • ::service mailboxd::
    notBefore=Jul 8 02:49:50 2015 GMT
    notAfter=Jul 6 02:49:50 2020 GMT
    subject= /C=US/ST=N/A/O=Zimbra Collaboration Server/OU=Zimbra Collaboration Server/CN=mail.testmailone.com.ph
    issuer= /C=US/ST=N/A/L=N/A/O=Zimbra Collaboration Server/OU=Zimbra Collaboration Server/CN=mail.testmailone.com.ph
    SubjectAltName=
    ::service ldap::
    notBefore=Jul 8 02:49:50 2015 GMT
    notAfter=Jul 6 02:49:50 2020 GMT
    subject= /C=US/ST=N/A/O=Zimbra Collaboration Server/OU=Zimbra Collaboration Server/CN=mail.testmailone.com.ph
    issuer= /C=US/ST=N/A/L=N/A/O=Zimbra Collaboration Server/OU=Zimbra Collaboration Server/CN=mail.testmailone.com.ph
    SubjectAltName=

    • Hi Pankaj,

      You can use this parameter in query

      not in:foldername
      

      example

      query="&query=date:01/21/2015 not in:junk
      

      The script will backup all your mailbox except in junk folder

  • Thanks for quick response.

    I want to exclude some email accounts for backup or there is any option to exclude all closed email accounts.

    • Hi,

      You can retrieve all user use this command

      su - zimbra -c "zmprov -l gaa" > /tmp/allusers.txt
      

      Then, you can open /tmp/allusers.txt and remove users to exclude backup. Open backup script and change this line

      for account in `su - zimbra -c 'zmprov -l gaa | sort'`
      

      become

      for account in `cat /tmp/allusers.txt | sort`
      
  • hello iman, i am trying to use the script but this is what i get on all the accounts
    : /srv/backup-mailbox.sh: line 27: /srv/backup//070416/warehouse@aziolarefin.nl.tgz: No such file or directory
    Processing mailbox xrosa@holdinggroups.com backup…

    • Hi Sundararajan Vinoth,

      Please try single command below

      /opt/zimbra/bin/zmmailbox -z -m user1@example.com getRestURL "//?fmt=tgz" > /tmp/user1@example.com.tgz
      

      If this single command work correctly, you can review your script

  • Backup of active accounts only —
    modify :
    for account in `su – zimbra -c ‘zmprov -l gaa | sort’`
    ::::::
    for account in `zmaccts |grep -v -E “spam|ham|virus|admin|domain” | grep active | awk ‘{ print $1}’`

  • thanks iman its awesome….one last request can i upgrade my zimbra open source version (Zimbra 8.6.0_GA_1153 (build 20141215151110)) latest version 8.7.11

  • Assalamualaikum,
    maaf mas iman, mau nanya jika folder tempat backup storage ada dinetwork dan telah saya mounting di folder mnt/backup.
    Namun hasilnya error :
    [root@mail /]# zmbkpose -f
    2017-09-20 11:16:22 DEBUG: Geting accounts in order of need backup.
    2017-09-20 11:16:22 DEBUG: 0 accounts in backup storage
    2017-09-20 11:16:22 DEBUG: 7 accounts in ldap server
    2017-09-20 11:16:22 DEBUG: 7 accounts without backup
    2017-09-20 11:16:22 DEBUG: Testing ldapserver ldap://192.168.50.66:389, using user uid=zimbra,cn=admins,cn=zimbra
    2017-09-20 11:16:22 DEBUG: Analizing 7 accounts
    2017-09-20 11:16:22 DEBUG: admin@demo1.com – START FULL backup …
    2017-09-20 11:16:22 DEBUG: admin@demo1.com – Doing ldap backup …
    2017-09-20 11:16:22 DEBUG: admin@demo1.com – ldap backup complete
    2017-09-20 11:16:22 DEBUG: admin@demo1.com – Doing mailbox backup …
    2017-09-20 11:16:22 DEBUG: admin@demo1.com – mailbox backup complete
    /bin/tar: mailbox.tgz: file changed as we read it

    jika saya arahkan kelocal folder berjalan normal.
    Mohon arahannya mas.

    Tks & Salam,

  • Maaf mas baru test lagi menurut arahan mas iman namun hasil masih tetap sama.

    Padahal share folder tersebut saya test dari windows dapat full access ,bisa RWX.
    Kira2 dimana ya mas masalahnya ?

    Salam,

LEAVE A COMMENT