{"id":2195,"date":"2013-07-08T10:29:34","date_gmt":"2013-07-08T02:29:34","guid":{"rendered":"http:\/\/rmohan.com\/?p=2195"},"modified":"2013-07-08T11:58:48","modified_gmt":"2013-07-08T03:58:48","slug":"zimbra-backup-sh","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=2195","title":{"rendered":"zimbra  backup.sh"},"content":{"rendered":"<div>\n<div id=\"LC1\">#!\/bin\/bash<\/div>\n<div id=\"LC2\"><\/div>\n<div id=\"LC3\"># Zimbra Backup Script<\/div>\n<div id=\"LC4\"># This script is intended to run from the crontab as root<\/div>\n<div id=\"LC5\"># Date outputs and su vs sudo corrections by other contributors, thanks, sorry I don&#8217;t have names to attribute!<\/div>\n<div id=\"LC6\"># Free to use and free of any warranty! Daniel W. Martin, 5 Dec 2008<\/div>\n<div id=\"LC7\"># Updated by Scott Harwell on 02\/03\/2013 to try to circumvent ldap 85 GB allocated file.<\/div>\n<div id=\"LC8\"># Also, this backup only backs up locally; ncftp has been commented out.<\/div>\n<div id=\"LC9\"><\/div>\n<div id=\"LC10\"><\/div>\n<div id=\"LC11\"># Outputs the time the backup started, for log\/tracking purposes<\/div>\n<div id=\"LC12\">echo Time backup started = $(date +%T)<\/div>\n<div id=\"LC13\">before=&#8221;$(date +%s)&#8221;<\/div>\n<div id=\"LC14\"><\/div>\n<div id=\"LC15\"># Live sync before stopping Zimbra to minimize sync time with the services down<\/div>\n<div id=\"LC16\"># Comment out the following line if you want to try single cold-sync only<\/div>\n<div id=\"LC17\">rsync -avHK &#8211;exclude &#8216;data\/ldap\/mdb\/db&#8217; &#8211;delete \/opt\/zimbra\/ \/media\/backup\/zimbra_backup<\/div>\n<div id=\"LC18\"><\/div>\n<div id=\"LC19\"># which is the same as: \/opt\/zimbra \/backup<\/div>\n<div id=\"LC20\"># Including &#8211;delete option gets rid of files in the dest folder that don&#8217;t exist at the src<\/div>\n<div id=\"LC21\"># this prevents logfile\/extraneous bloat from building up overtime.<\/div>\n<div id=\"LC22\"><\/div>\n<div id=\"LC23\"># Now we need to shut down Zimbra to rsync any files that were\/are locked<\/div>\n<div id=\"LC24\"># whilst backing up when the server was up and running.<\/div>\n<div id=\"LC25\">before2=&#8221;$(date +%s)&#8221;<\/div>\n<div id=\"LC26\"><\/div>\n<div id=\"LC27\"># Stop Zimbra Services<\/div>\n<div id=\"LC28\">su &#8211; zimbra -c&#8221;\/opt\/zimbra\/bin\/zmcontrol stop&#8221;<\/div>\n<div id=\"LC29\">sleep 30<\/div>\n<div id=\"LC30\"><\/div>\n<div id=\"LC31\"># Kill any orphaned Zimbra processes<\/div>\n<div id=\"LC32\">ORPHANED=`ps -u zimbra -o &#8220;pid=&#8221;` &amp;&amp; kill -9 $ORPHANED<\/div>\n<div id=\"LC33\"><\/div>\n<div id=\"LC34\"># Only enable the following command if you need all Zimbra user owned<\/div>\n<div id=\"LC35\"># processes to be killed before syncing<\/div>\n<div id=\"LC36\"># ps auxww | awk &#8216;{print $1&#8243; &#8220;$2}&#8217; | grep zimbra | kill -9 `awk &#8216;{print $2}&#8217;`<\/div>\n<div id=\"LC37\"><\/div>\n<div id=\"LC38\"># Sync to backup directory<\/div>\n<div id=\"LC39\">rsync -avHKS &#8211;exclude &#8216;data\/ldap\/mdb\/db&#8217; &#8211;delete \/opt\/zimbra\/ \/media\/backup\/zimbra_backup<\/div>\n<div id=\"LC40\"><\/div>\n<div id=\"LC41\"># Sync LDAP Sparse Files (cp copies sparse files properly)<\/div>\n<div id=\"LC42\">cp -r \/opt\/zimbra\/data\/ldap\/mdb\/db \/media\/backup\/zimbra_backup\/data\/ldap\/mdb\/<\/div>\n<div id=\"LC43\"><\/div>\n<div id=\"LC44\"># Restart Zimbra Services<\/div>\n<div id=\"LC45\">su &#8211; zimbra -c &#8220;\/opt\/zimbra\/bin\/zmcontrol start&#8221;<\/div>\n<div id=\"LC46\"><\/div>\n<div id=\"LC47\"># Calculates and outputs amount of time the server was down for<\/div>\n<div id=\"LC48\">after=&#8221;$(date +%s)&#8221;<\/div>\n<div id=\"LC49\">elapsed=&#8221;$(expr $after &#8211; $before2)&#8221;<\/div>\n<div id=\"LC50\">hours=$(($elapsed \/ 3600))<\/div>\n<div id=\"LC51\">elapsed=$(($elapsed &#8211; $hours * 3600))<\/div>\n<div id=\"LC52\">minutes=$(($elapsed \/ 60))<\/div>\n<div id=\"LC53\">seconds=$(($elapsed &#8211; $minutes * 60))<\/div>\n<div id=\"LC54\">echo Server was down for: &#8220;$hours hours $minutes minutes $seconds seconds&#8221;<\/div>\n<div id=\"LC55\"><\/div>\n<div id=\"LC56\"># Create a txt file in the backup directory that&#8217;ll contains the current Zimbra<\/div>\n<div id=\"LC57\"># server version. Handy for knowing what version of Zimbra a backup can be restored to.<\/div>\n<div id=\"LC58\">su &#8211; zimbra -c &#8220;zmcontrol -v &gt; \/media\/backup\/zimbra_backup\/conf\/zimbra_version.txt&#8221;<\/div>\n<div id=\"LC59\"># or examine your \/opt\/zimbra\/.install_history<\/div>\n<div id=\"LC60\"><\/div>\n<div id=\"LC61\"># Display Zimbra services status<\/div>\n<div id=\"LC62\">echo Displaying Zimbra services status&#8230;<\/div>\n<div id=\"LC63\">su &#8211; zimbra -c &#8220;\/opt\/zimbra\/bin\/zmcontrol status&#8221;<\/div>\n<div id=\"LC64\"><\/div>\n<div id=\"LC65\"># Create archive of backed-up directory for offsite transfer<\/div>\n<div id=\"LC66\"># cd \/backup\/zimbra<\/div>\n<div id=\"LC67\">umask 0177<\/div>\n<div id=\"LC68\">today=&#8221;$(date +%m-%d-%y)&#8221;<\/div>\n<div id=\"LC69\">tar -zcvf &#8220;\/media\/backup\/zimbra_backup_tars\/mail.backup.$today.tgz&#8221; -C \/media\/backup\/zimbra_backup .<\/div>\n<div id=\"LC70\"><\/div>\n<div id=\"LC71\">####### SCOTT COMMENTED OUT AS NO TRANSFER AT THIS POINT<\/div>\n<div id=\"LC72\"># Transfer file to backup server<\/div>\n<div id=\"LC73\">#ncftpput -u &lt;username&gt; -p &lt;password&gt; &lt;ftpserver&gt; \/&lt;desired dest. directory&gt; \/tmp\/mail.backup.tgz<\/div>\n<div id=\"LC74\">#<\/div>\n<div id=\"LC75\">#rm \/tmp\/mail.backup.tgz<\/div>\n<div id=\"LC76\">#######<\/div>\n<div id=\"LC77\"><\/div>\n<div id=\"LC78\"># Outputs the time the backup finished<\/div>\n<div id=\"LC79\">echo Time backup finished = $(date +%T)<\/div>\n<div id=\"LC80\"><\/div>\n<div id=\"LC81\"># Calculates and outputs total time taken<\/div>\n<div id=\"LC82\">after=&#8221;$(date +%s)&#8221;<\/div>\n<div id=\"LC83\">elapsed=&#8221;$(expr $after &#8211; $before)&#8221;<\/div>\n<div id=\"LC84\">hours=$(($elapsed \/ 3600))<\/div>\n<div id=\"LC85\">elapsed=$(($elapsed &#8211; $hours * 3600))<\/div>\n<div id=\"LC86\">minutes=$(($elapsed \/ 60))<\/div>\n<div id=\"LC87\">seconds=$(($elapsed &#8211; $minutes * 60))<\/div>\n<div id=\"LC88\">echo Time taken: &#8220;$hours hours $minutes minutes $seconds seconds&#8221;<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p> #!\/bin\/bash # Zimbra Backup Script # This script is intended to run from the crontab as root # Date outputs and su vs sudo corrections by other contributors, thanks, sorry I don&#8217;t have names to attribute! # Free to use and free of any warranty! Daniel W. Martin, 5 Dec 2008 # Updated by [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/2195"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2195"}],"version-history":[{"count":5,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/2195\/revisions"}],"predecessor-version":[{"id":2198,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/2195\/revisions\/2198"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}