Zimbra offers Open Source email server software and shared calendar for Linux and the Mac
 
Go Back   Zimbra - Forums > Zimbra Collaboration Suite > Administrators

Welcome to the Zimbra - Forums!
Welcome, if you would like to post a comment in the forums, please register and review our posting policy & tips. We also encourage you to explore all things Zimbra with our team and members of the community.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-09-2008, 02:06 PM
Senior Member
 
Posts: 60
Default Yet Another Backup Script for Community Version

Hi All

I have written the following script to suit my backup needs (it is actually quite a simple script...). I want to publish it here so you can have a good and maybe help improve on it and add functionality to it.

I admit some of the ideas come from existing scripts and ideas from forums.

I will maintain a current working version of the script here: Home*-*Open Source Office Lösungen

Script features:
  • Backups with or without strong encryption!
  • Optional Off-site copying of archives after creation
  • MD5 checksums for integrity checks of archives
  • Weekly backup rotation - 1 Full & 6 Diff's per rotation
  • Email report on Full backup
  • Email notifications on errors
  • Backup file lists (attached to weekly full backup report)
  • Installer & Setup option for quick deployment (install needed software and setup env e.g. ssh pki auth and cronjob helper)
Here is the code up to date code:
Code:
#!/bin/bash
## *** Info ***
# USAGE:    -h or --help for help & usage.
#           -f or --full for Full backup.
#           -d or --diff for Diff backup.
#           -V or --version for version info.
#           --INSTALL	 for script install and setup.
#
# This is a backup script for the FOSS version of Zimbra mail server.
# The script is free and open source and for use by anyone who can find a use for it.
#
# THIS SCRIPT IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
# HOLDERS AND/OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
# THE USE OF THIS DOCUMENT, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# What this script does:
# 1. Makes daily off-line backups, at a service downtime of ~ < 2 min.
# 2. Weekly backup cycle - 1 full backup & 6 diffs.
# 3. Predefined archive sizes, for writing backups to CD or DVD media...
# 4. Backup archive compression.
# 5. Backup archive encryption.
# 6. Backup archive integrity checks and md5 checksums creation.
# 7. Automated DR - Off-site copy of backup archives via ssh.
# 8. Install and setup function for needed sofrware (Ubuntu Systems only)
# 9. Weekly eMail report & eMail on error - including CC address.
#
# This script makes use of following tools:
# apt-get, cron, dar, dpkg, mailx, md5sum, rsync, ssh, uuencode, wget, zimbra mta.
#
# We have opted to use a pre-sync directory to save on "down time", but this 
# causes one to have huge additional space usage.
# But hard drives are cheep today!
#
# What is still to come or needs work on:
# 1. Recover option
# 2. Better documentation
#
#------- CONFIG -------#
# Edit this part of the script to fit your needs.
#

#--- Directories ---#
# Please add the trailing "/" to directories!
ZM_HOME=/opt/zimbra/
SYNC_DIR=/srv/backup/sync/
ARCHIVEDIR=/srv/backup/dars/
TOO_MEDIA_DIR=/srv/backup/burn/

#
#--- PROGRAM OPTIONS ---# 
RSYNC_OPTS="-aHK --delete --exclude=*.pid"

#
#--- ARCHIVE NAMES ---#
BACKUPNAME="Zimbra_Backup"
BACKUPTYPE_F="FULL"	# name prefix to sort between full and diff backups
BACKUPTYPE_D="DIFF"
BACKUPDATE=`date +%d-%B-%Y`

#
#--- ARCHIVE SIZE ---#
# storage media size
ARCHIVESIZE="4395M"
COMPRESS="9"		# valid answers are 1 - 9 ( 9 = best )

CRYPT="yes"		# valid answers are "yes" or "no"
PASSDIR=/etc/zmbac/
PASSFILE="noread"

#
#--- EMAIL ADDRESS ---#
EMAIL="admin@localhost"
EMAILCC="admin@remotehost"
LOG="/var/log/zim_backup.log"

#--- SSH REMOTE DR COPY ---#
# This option will secure copy you archives to a remote server via 'scp'
DRCP="yes" 		# valid answers are "yes" or "no" 
SSHUSER="heinzg"
REMOTEHOST="172.16.184.1"
REMOTEDIR="/tmp/"


#--- USE HACKS !?! ---#
# Built in hacks to fix common problems
#Hack to start Stats, even run zmlogprocess if needed
STATHACK="yes" 		# valid answers are "yes" or "no"


## ~~~~~!!!! SCRIPT RUNTIME !!!!!~~~~~ ##
# Best you don't change anything from here on, 
# ONLY EDIT IF YOU KNOW WHAT YOU ARE DOING
#
#

!!! the script is to long to fit into the post!! To view the full script visit my development site link is above.
Here is a sample print out of the log file after a full & diff backup:

Full Backup started at: 23:21

No old backups found

Setting date & version Marker into /opt/zimbra/DATE_VERSION.txt

Doing a hotsync of /opt/zimbra/ to /srv/backup/fakebackup/
sync went ok

Stopping the Zimbra server

Deactivated zimbra's cronjobs

Host mail.igrill.local
Stopping stats...Done
Stopping mta...Done
Stopping spell...Done
Stopping snmp...Done
Stopping archiving...Done
Stopping antivirus...Done
Stopping antispam...Done
Stopping imapproxy...Done
Stopping mailbox...Done
Stopping logger...Done
Stopping ldap...Done

Zimbra server has stopped

kill any left over zimbra procs
Nothing to kill

Doing a fast cold sync...
sync went ok

Reinstated zimbra's cronjobs

Host mail.igrill.local
Starting ldap...Done.
Starting logger...Done.
Starting mailbox...Done.
Starting antispam...Done.
Starting antivirus...Done.
Starting snmp...Done.
Starting spell...Done.
Starting mta...Done.
Starting stats...Done.

Zimbra has started

Service down time was - Hr:0 Min:2 Sec:16

Running A hack... This one to check and start Stats subsystem
Hack not needed Stats seems to be running fine...

All services are running


Writing a full backup called:
12_Zimbra_Backup_30-March-2008_FULL
in: /srv/backup/zimbra_dars/ with file sizes of max: 4395M
Archive is Encrypted


--------------------------------------------
19271 inode(s) saved
with 1115 hard link(s) recorded
0 inode(s) changed at the moment of the backup
0 inode(s) not saved (no inode/file change)
0 inode(s) failed to save (filesystem error)
0 inode(s) ignored (excluded by filters)
0 inode(s) recorded as deleted from reference backup
--------------------------------------------
Total number of inode considered: 19271
--------------------------------------------
EA saved for 0 inode(s)
--------------------------------------------
Archive Created successfully

Create MD5 Checksum for 12_Zimbra_Backup_30-March-2008_FULL.1.dar

MD5 Checksum Created successfully

copy archive to 172.16.184.1 remote directory /tmp/

Create file listing from archive

:::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::::
Full Zimbra Backup ended at: 23:42
Backup took Hr:0 Min:21 Sec:27 to complete
:::::::::::::::::Cheers Osoffice for the script:::::::::::::::::::::::


Diff Backup started at: 23:46

Doing a hotsync of /opt/zimbra/ to /srv/backup/fakebackup/

sync went ok

Stopping the Zimbra server

Deactivated zimbra's cronjobs

Host mail.igrill.local
Stopping stats...Done
Stopping mta...Done
Stopping spell...Done
Stopping snmp...Done
Stopping archiving...Done
Stopping antivirus...Done
Stopping antispam...Done
Stopping imapproxy...Done
Stopping mailbox...Done
Stopping logger...Done
Stopping ldap...Done

Zimbra server has stopped

kill any left over zimbra procs
Nothing to kill

Doing a fast cold sync...
sync went ok

Reinstated zimbra's cronjobs

Host mail.igrill.local
Starting ldap...Done.
Starting logger...Done.
Starting mailbox...Done.
Starting antispam...Done.
Starting antivirus...Done.
Starting snmp...Done.
Starting spell...Done.
Starting mta...Done.
Starting stats...Done.

Zimbra has started

Service down time was - Hr:0 Min:2 Sec:15

Running A hack... This one to check and start Stats subsystem
Hack not needed Stats seems to be running fine...

All services are running


Writing a diff backup called:
12_Zimbra_Backup_30-March-2008_DIFF
in: /srv/backup/zimbra_dars/ with file sizes of max: 4395M
Archive is Encrypted
Warning, the archive 12_Zimbra_Backup_30-March-2008_FULL has been encrypted. A wrong key is not possible to detect, it would cause DAR to report the archive as corrupted


--------------------------------------------
154 inode(s) saved
with 1115 hard link(s) recorded
0 inode(s) changed at the moment of the backup
19118 inode(s) not saved (no inode/file change)
0 inode(s) failed to save (filesystem error)
0 inode(s) ignored (excluded by filters)
5 inode(s) recorded as deleted from reference backup
--------------------------------------------
Total number of inode considered: 19277
--------------------------------------------
EA saved for 0 inode(s)
--------------------------------------------
Archive Created successfully

Create MD5 Checksum for 12_Zimbra_Backup_30-March-2008_DIFF.1.dar

MD5 Checksum Created successfully

copy archive to 172.16.184.1 remote directory /tmp/

Create file listing from archive

:::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::::
Diff Zimbra Backup ended at: 23:50
Backup took Hr:0 Min:4 Sec:26 to complete
:::::::::::::::::::Cheers Osoffice the script:::::::::::::::::::::::::

I hope this helps someone and have fun!
( Note the service downtime was due to a very weak vmware guest running on my laptop.... So it is faster in real life )

Cheers
Heinzg

Last edited by heinzg : 03-31-2008 at 01:13 AM. Reason: Script Update - Re-write.
Reply With Quote
  #2 (permalink)  
Old 02-09-2008, 02:11 PM
Zimbra Contractor
 
Posts: 5,651
Default

Awesome, be sure to get that into Open Source Edition Backup Procedure - Zimbra :: Wiki
Think under the snapshots section might be appropriate as you have both a full & differential method in there
Thanks for sharing!
Reply With Quote
  #3 (permalink)  
Old 02-11-2008, 04:16 PM
Senior Member
 
Posts: 57
Default

Mailing the log is disabled in the script right? to enable it, remove # in front of $MAILX everywhere and crontab 30 4 * * * /root/zmbac.sh > /tmp/zim_backup_report.log

or?
Reply With Quote
  #4 (permalink)  
Old 02-11-2008, 11:20 PM
Senior Member
 
Posts: 60
Default

Hi osiris!

Yes the sending mails part has been disabled for the moment.
As you have mentioned you will get it working again. This is the part of the script I still want to refine in the coming days!

My cronjob for this scrip looks like this:
Code:
0 3 * * 1    /bin/sh   /root/zmbac.sh -f > /tmp/zim_backup_report.log 2>&1
0 3 * * 2-7 /bin/sh   /root/zmbac.sh -d >> /tmp/zim_backup_report.log 2>&1
I will post an update when I have sorted out the mail reports part.

Cheers
Heinzg
Reply With Quote
  #5 (permalink)  
Old 02-16-2008, 12:11 PM
Senior Member
 
Posts: 57
Default

guess it should be 0 for full and 1-6 for diff, thaught it went from 0 to 6 not 1 to 7.. Anyhow, I see in the script something that makes me think you have or have had the same issues as me now.

After a backup is done, every service run except the zmstat service.. I have tried to uncomment the 2 lines in your script regarding this:

Code:
        # quick hack to start the stats component
        # echo
        # sudo -u $ZM_USER "$ZM_HOME"bin/zmstatctl start
Also tried with changing sudo to use the su -c way, last night I even tried to set up a crontab for zimbra user like:

Code:
30 4 * * * /opt/zimbra/bin/zmstatctl stop && /opt/zimbra/bin/zmstatctl start
It simply refuses to work, I have to manually via shell as the zimbra user

Code:
/opt/zimbra/bin/zmstatctl stop && /opt/zimbra/bin/zmstatctl start
to get things going again... Found any solution to that one? or someone else with an idea for what to try?
Reply With Quote
  #6 (permalink)  
Old 02-16-2008, 12:26 PM
Senior Member
 
Posts: 57
Default

Ahh nevermind, got it, I changed every(!) instance of sudo to su - zimbra -c way in the script, now it works fine...
Reply With Quote
  #7 (permalink)  
Old 02-16-2008, 02:58 PM
Senior Member
 
Posts: 60
Default

hi osiris!

according to crontab 0 or 7 is Sunday, for me full backup day, and thus 1-6 Monday to Saturday is diff day.

Does RedHat not have sudo install now a days? I am more a Ubuntu kinda guy.
Anyway noted. I will put a comment in there that non sudoers should replace sudo with:
Code:
su - $ZM_USER -c
I have noticed and I think it is a bug... not sure, that the "stats" component is displaying the wrong info in "zmcontrol status" it shows that 'stats' is stopped after starting the server new... thus the quick hack "zmstatctl start"!
But it seems that stats are being collected even if it displays it as being stopped. BUG

I have had way to much wine to go into this deeper tonight.
Have fun, It is weekend you know!
P.S. it is well cold here today, how is it in Norway?
Reply With Quote
  #8 (permalink)  
Old 02-16-2008, 03:13 PM
Senior Member
 
Posts: 57
Default

It has sudo, thats not the issue here it seems.. But the for some weird reason, it refuses to restart stat service when using the sudo command, only when using su instead.. No idea why, really makes no sense..

Quite cold here to, but got a bottle of cognac to warm me
__________________
Release 5.0.8_GA_2462.RHEL5_20080709170039 RHEL5 FOSS edition
Reply With Quote
  #9 (permalink)  
Old 03-01-2008, 02:39 PM
Senior Member
 
Posts: 60
Default

Hi All!

I have updated the backup script.


  1. It now sends emails on errors
  2. "su" is used instead of "sudo"
  3. Archive are now created from ./
  4. More safety checks
  5. Hack to check and start of "Stats" module after server restarts

From now on I will host the scrip at the link below where you can view the script's code online before downloading,

Script home page

Have fun!
Reply With Quote
  #10 (permalink)  
Old 03-01-2008, 03:34 PM
Senior Member
 
Posts: 57
Default

That new version didnt work very well, will test a bit and report.. But here is the output so far:


Code:
      Full Backup started at: 00:26

no old backups found

Printing date: 02-March-2008 and software version:  into /opt/zimbra/DATE_VERSION.txt

Doing a hotsync of /opt/zimbra/ to /backup2/sync/
sync went ok

Stopping the Zimbra server


Release 5.0.2_GA_1975.RHEL5_20080130221917 RHEL5

/opt/zimbra/bin/zmcontrol [-v -h -H <host>] command [args]

        -v:     display version
        -h:     print usage statement
        -H:     Host name (localhost)

        Command in:
                maintenance                Toggle maintenance mode
                shutdown                             Stop services
                start                               Start services
                startup                             Start services
                status                      Display service status
                stop                                 Stop services

zmcontrol shutdown had an error!
Null message body; hope that's ok

kill any left over zimbra procs

Doing a fast cold sync of /opt/zimbra/ & /backup2/sync/ while the Zimbra server is down
sync went ok



Release 5.0.2_GA_1975.RHEL5_20080130221917 RHEL5

/opt/zimbra/bin/zmcontrol [-v -h -H <host>] command [args]

        -v:     display version
        -h:     print usage statement
        -H:     Host name (localhost)

        Command in:
                maintenance                Toggle maintenance mode
                shutdown                             Stop services
                start                               Start services
                startup                             Start services
                status                      Display service status
                stop                                 Stop services

There was an error starting Zimbra!
Null message body; hope that's ok
I am expecting 7 pids for Stats, rebooting Stats subsystem!
Usage: zmstatctl start|stop|status|rotate
Starts/stops monitoring processes, checks status, or rotates logs.
Stopping stats failed!
Null message body; hope that's ok
Usage: zmstatctl start|stop|status|rotate
Starts/stops monitoring processes, checks status, or rotates logs.
Starting stats failed!
Null message body; hope that's ok

Writing a full backup called:08_Zimbra_Backup_02-March-2008_FULL in: /backup2/zimbra_dars/ with file sizes of max: 4695M
__________________
Release 5.0.8_GA_2462.RHEL5_20080709170039 RHEL5 FOSS edition
Reply With Quote
Reply


Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Zimbra Backup Script ... Small Problem frankb Administrators 12 07-14-2008 08:40 AM
Debian - Upgrade from 4.5.6 magikman Installation 1 01-19-2008 11:59 AM
Backup script issues SSS Administrators 18 12-03-2007 08:56 AM
4.0.4 to 4.5.6 upgrade failed in network edition chenthil Administrators 1 08-27-2007 09:36 AM
[SOLVED] Simple backup question... dameron Administrators 3 08-25-2007 09:36 PM

Why Join?

Registering let's you ask questions, makes it easier to search, displays any files attached to posts, and notifies you about replies.

Zimbrablog.com

sourceforge.net



 

Search Engine Optimization by vBSEO 3.1.0