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

Posted by

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 😀

87 comments

    1. Hi Vikram,

      For incremental Backup or daily backup, you can using attribute “days ago” on the script. The script just backup mailboxes 😀

    2. for account in `su – zimbra -c ‘zmprov -l gaa | sort’` for loop not working here not accept ‘ ‘ comm

      1. Dear Sir,
        We have an Zimbra Email server ,We are set the crontab job for backup and everyday cron job is running properly but only this script is taking only “A” to :V” rest email id are not backped so request you to please help me where is problem and also same folder is moving on AWS S3 bucket
        If you found any thing please reply on this email id. u.mishra2@gmail.com.
        Below are our script.
        ##!/bin/bash
        clear
        rm -rf /srv/backup/*
        ## 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.”

  1. 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”’

    1. 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

  2. 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?

  3. 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

      1. hi iman, below is the error:

        Processing mailbox user1@testmailone.com.ph backup…
        ERROR: zclient.IO_ERROR (Unable to get REST resource from https://mail.testmailone.com.ph/home/user1@testmailone.com.ph/?fmt=tgz&query=after:09/07/2015: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target) (cause: javax.net.ssl.SSLHandshakeException sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target)

        1. Processing mailbox user2@testmailtwo.com.ph backup…
          ERROR: zclient.IO_ERROR (Unable to get REST resource from https://mail.testmailtwo.com.ph/home/user2@testmailtwo.com.ph/?fmt=tgz&query=after:09/07/2015: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target) (cause: javax.net.ssl.SSLHandshakeException sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target)

          1. Hi Ferjun,

            It seems problem with your SSL Certificate. Please verify your SSL certificate with the following command and paste the result :

            /opt/zimbra/bin/zmcertmgr viewdeployedcrt
            
  4. 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”

  5. 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=

  6. ::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=

    1. 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

  7. Thanks for quick response.

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

    1. 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`
      
  8. 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…

  9. hi iman,
    does this backup includes user accounts and password thanks

    or how to backup user accounts and password only?

    thanks

    1. 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

  10. 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}’`

  11. 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

  12. 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,

  13. 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,

  14. am getting this error when i run the script , these error is only for 4 to 5 mailbox rest of the mailbox backup correctly.

  15. thanks Iman.. i delete the mailbox and re-created .. now backup is working perfect… one last request can we add date and time to the folder. ??

  16. Dear Iman ,
    my dell server have hardware issue so we are trying to change the server how to take backup of my complete email id and their mailbox

    please help me on this

    thanks

  17. thanks to reply my first query…

    zmschedulebackup. This command is used to schedule full backups, incremental backups, and deletion of old backups.

    is it possible in open source if yes, can you explain how..

    thank you..

  18. how to run cron using zimbra username without inputting password

    ## 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.”

  19. Great post Iman,

    Thanks

    If you want to do a full backup you just need to let all the “Backup Options” commented. Right?

    1. Hi Nuno,
      If you want to take full backup, you can remove $query from this line

      $ZMBOX -z -m $account getRestURL "//?fmt=${FORMAT}$query" > $ZDUMPDIR/$account.${FORMAT}
      

      become

      $ZMBOX -z -m $account getRestURL "//?fmt=${FORMAT}" > $ZDUMPDIR/$account.${FORMAT}
      
  20. HI there is it possible to ignore some users? lets say i only want to backup user 1 and user 2 but not user 3?
    Thank you

    1. Hi Albert,
      Yes you can. You can change loop user from

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

      Become

      for account in `cat /srv/acc.txt | sort`
      

      You must create an acc.txt file in the /srv folder and fill it with all users except user 3

  21. Hi there i was wondering if this has happened to you that i get

    ERROR: zclient.IO_ERROR (Unable to get ?fmt=tgz) (cause: java.net.SocketTimeoutException Read timed out)

    the odd thing is that the zimbra has 2 cores with 6 gigs of ram and backing up 33 accounts only whats odd is that not always i get this error. i tried looking up that error seems something to do with memory allocation for zmjava which also has something to do with zmlocalconfig.

    i tried adding -t 0 but still getting the timeout not sure why?
    Thank you

    $ZMBOX -z -t 0 -m $account getRestURL “//?fmt=${FORMAT}$query” > $ZDUMPDIR/$account.${FORMAT}

  22. Hi Iman

    How about if i want to restored message in the /opt/zimbra/stored?

    As i have offline backup /opt/zimbra on aNAS but i want to restored on messages.
    What i did is upload from /opt/zimbra/store/0/24 (offline backup NAS) to /opt/zimbra/store/0/24 on the live zimbra mail. After completing i check my inbox but still not shown there.

    Is there anyway to restored message from a offline backup

  23. ERROR: zclient.IO_ERROR (Unable to get resource from ‘https://mail.shtigen.com/home/arik@shtigen.com/%EF%BF%BD?fmt=tgzпїЅ’ : PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target) (cause: javax.net.ssl.SSLHandshakeException PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target)

  24. Hi Iman.
    In first place, i appreciate your post, and forgive if i misspell, english is not my native.
    Second, i end up here searching for an error whyle i tri to backup the accounts from one domain on the server. (The server has 5 domains, and two of them don’t backup)
    I get this error when i try to backup with the getRestURL method:

    ERROR: zclient.IO_ERROR (Unable to get resource from ‘https://mailserver.com/home/ventas@mailserver.com/?fmt=tgz’ : mailserver.com: Nombre o servicio desconocido) (cause: java.net.UnknownHostException mailserver.com: Nombre o servicio desconocido)

    For security reasons i’ve changed the output, ‘mailserver’ isn’t the real name of the server.
    What i checked up:
    -Verified the Certs, still valid until October 25 2022
    -zmprov -l gd mailserver.com (seems fine in the five domains)
    -zimbra_zmprov_default_to_ldap=false (i don’t think whas this, cause the other domains do the backup)
    zimbra_zmprov_default_soap_server = localhost (Same as above, don’t changed it casuse the other domains backup fine)
    -the /etc/hosts (contains the loopback ip, and the local ip address with the FQDN)
    -The external DNS resolution points to the public ip of the server.

    At this time, i don’t know where else to look.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.