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 😀
Any way of taking incremental backup
Hi Vikram,
For incremental Backup or daily backup, you can using attribute “days ago” on the script. The script just backup mailboxes 😀
for account in `su – zimbra -c ‘zmprov -l gaa | sort’` for loop not working here not accept ‘ ‘ comm
Hi,
If cannot use a single quote, you can use double quote
ERROR: service.FAILURE (system failure: GET from https://mydomain.com:8443/home/user@mydomain.com/?fmt=tgz&query=date:18/06/2022 failed, status=500. Server Error)
Processing mailbox user@mydomain.com backup…
Hello,
Please add -t 0 before GetRestUrl (GRU)
ERROR: service.FAILURE (system failure: GET from https://mydomain.com:8443/home/user@mydomain.com/?fmt=tgz&query=date:18/12/2022 failed, status=500. Server Error)
Processing mailbox user@mydomain.com backup…
ERROR: service.FAILURE (system failure: GET from https://mydomain.com:8443/home/user@mydomain.com/?fmt=tgz&query=date:18/12/2022 failed, status=500. Server Error)
Processing mailbox user@mydomain.com backup…
I’m also getting same error even added -t 0 before GetRestUrl
Hi,
I am also got problem when using Zimbra proxy. When I disable Zimbra Proxy, backup mailbox work perfectly
date must be m/d/year
What about restoring backups?
Thank you
Hi Atazmak,
You can check at this article : https://imanudin.net/2015/01/29/how-to-restore-zimbra-mailbox/
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.”
Hello,
Can you give me more details? please sending from this page: https://imanudin.net/contact/
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
Hi Ferjun,
Could you please give me more information such as log or something else
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)
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)
Hi Ferjun,
It seems problem with your SSL Certificate. Please verify your SSL certificate with the following command and paste the result :
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=
Sorry for the long post,
Appreciate your help, thanks a lot..
Hi Ferjun,
It seems still 5 years about SSL Cert. Are you has been try restart services Zimbra and try again to backup?
hi iman,
yes I tried to restart Zimbra services. I also restart Server but still thesame issue..
Hi
How to backup mailbox to other machine?
Hi,
You can use rsync to backup all contains folder /opt to other machine 😀
thanks for this article, Iman!
Hi Iman,
The article is very useful.
Can I exclude some mailbox from backup.
Hi Pankaj,
You can use this parameter in query
example
The script will backup all your mailbox except in junk folder
How can i define more folders? .. junk and trash
Hello,
All folder will be backup if having data
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
Then, you can open /tmp/allusers.txt and remove users to exclude backup. Open backup script and change this line
become
How to backup all mailboxes to different storage?
Thanks
Hi FT,
You can mounting different storage to your Zimbra server via NFS or other thing.
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…
Hello Julio,
Could you please tell me the script at line 27? maybe you miss something or not configure properly
hi iman,
does this backup includes user accounts and password thanks
or how to backup user accounts and password only?
thanks
Hi Beph,
No. This guidance is only backup your mailbox data (Inbox, Draft, Sent and etc)
hi iman,
Im getting this error, when i try to take a backup of one particular uses, what may be the issue please help on this.
ERROR: service.FAILURE (system failure: GET from http://mail.xyz.com:443/home/admin@xyz.com/?fmt=tgz failed, status=400. Bad Request)
Hi Sundararajan Vinoth,
Please try single command below
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}’`
Hi Mauro Assis,
Thanks for sharing. Appreciated 🙂
i need to backup , daily and weekly. can i use this script ?? how to add in crontab..
Hi Nagendra,
You can try this awesome tools for easy create crontab : http://www.openjs.com/scripts/jslibrary/demos/crontab.php
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,
Hi mas,
Coba pada folder share nya diberikan hak akses 777
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,
ERROR: zclient.CLIENT_ERROR (unable to parse URI: https://mail.demo.studio:7071/service/admin/soap/) (cause: java.net.URISyntaxException Illegal character in path at index 67: https://mail.demo.studio:8443/home/test@demo.studio /?fmt=tgz&query=after:01/01/2016%20before:08/08/2020)
am getting this error
am getting this error when i run the script , these error is only for 4 to 5 mailbox rest of the mailbox backup correctly.
Hi,
Please try without any options
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. ??
after successfull backup can we get an email notifications?? can we add some lines in this script ??
Hi nagendra prasad,
You can add some lines in the script. For example, you can check the script from this article : https://imanudin.net/2017/02/23/script-automatic-restart-services-when-zimbra-stoppednot-running/
Hi .. Iman
Thanks for this post. Help him.
Any Disaster Recovery (DR) process for zimbra open source ?
Hi Bhanu Suthar,
You can try this one : https://imanudin.net/2015/03/24/how-to-install-configure-zimbra-high-availability-ha/
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
Hi Amithrajc,
You can backup all folder /opt/zimbra and transfer to new server. I am usually use rsync to do that
very good article iman. very helpful indeed.
how can i do incremental backup?
Hello Shawn,
You can use crontab to make a backup every night 🙂
Iman ,
very good article thanks alot.
should i run the cron job as root or zimbra user ?
Hello,
You should run as ROOT user
Hello mas iman, ini berlaku juga untuk zimbra open source ya?
Hello mas,
Berlaku juga mas
How to kill backup is running?
Hi,
You can check using ps ax and kill the PID
ok thanks,
i have a problem
i’m using backup before 7 days
ERROR: service.FAILURE (system failure: GET from https://mailsvr.domain.vn:443/home/user@domain.vn/?fmt=tgz&query=after:27/07/2019 failed, status=500. Server Error)
Hello,
Are you already make sure the data 7 days ago is there on your mailbox? how about export from preferences menu?
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..
No,
zmschedulebackup only in Zimbra NE
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.”
Hello,
This method run as ROOT user and did not ask Zimbra Password
Great post Iman,
Thanks
If you want to do a full backup you just need to let all the “Backup Options” commented. Right?
Hi Nuno,
If you want to take full backup, you can remove $query from this line
become
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
Hi Albert,
Yes you can. You can change loop user from
Become
You must create an acc.txt file in the /srv folder and fill it with all users except user 3
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}
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
Hi,
You can re-import msg file using zmmailbox command: https://wiki.zimbra.com/wiki/Zmmailbox#zmmailbox_help_message
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)
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.
Hi Imam ,
Thanks for Such a wonderful Article .
My Question is , will the Date based Query be taking care of Special Attributes like Tags , Calendar , Briefcase etc apart from just exporting mails and folders .
Kindly Suggest
Hi,
Unfortunately this is not currently possible.