Results 1 to 2 of 2

Thread: ZCS 6.0.8 live backup

  1. #1
    Join Date
    Oct 2009
    Location
    Switzerland
    Posts
    3
    Rep Power
    6

    Default ZCS 6.0.8 live backup

    Hello,

    I just would like to share a backup script I'm starting to use. It's purpose is not to replace other "/opt/zimbra" complete backup scripts, and I'm not a shell script guru !

    But I needed a live backup solution to avoid stopping the zimbra server too often. So i digged, found some ideas and wrote this script (a previou post shows the first version of it). It also gives additionnal possibilities : having user mailboxes and some chosen preferences ready for restore. One could even extract a single mail or folder.

    It uses REST api and creates one compressed file for each mailbox and several zmprov ready files to restore accounts, passwords, aliases, forwards, filters...

    It mount a NAS share, creates a date based subdir in backup dir, and deletes the one older than x days.

    It uses two text files : one to store account prefs to save, and one to store accounts to ignore (wiki, ham ,spam...).

    Here is the main file :

    Code:
    #!/bin/bash
    ### Zimbra live backup script
    ###
    ### Disclaimer : this script is given "as is" and is not intended to replace any afficial backup technique.
    ###
    ### RESTORE COMMANDS :
    ### su - zimbra
    ### zmprov < restore_foo.txt
    ### zmmailbox -z -m $ACCOUNT postRestURL "//?fmt=tgz&resolve=skip" /backupdir/$ACCOUNT.tgz
    ### zmmailbox -z -m $ACCOUNT postRestURL "//?fmt=tgz&resolve=skip" /backupdir/$ACCOUNT.Trash.tgz
    ### cat /backupdir/$ACCOUNT.filters.txt | sed 's/^/afrl /' | zmmailbox -z -m $ACCOUNT
    ###
    ### Miguel Cordas
    ### mc@mcsi.ch
    ### 
    ### Version 0.6 / 21.11.2010
    ### 
    ### START CONFIGURATION ###
    ### Dir for compressed account archives :
    BACKDIR="/backup/zimbra";
    ### Base dir for scripts :
    BASEDIR="/root/zimbra";
    ### base name for logfile (without .log) :
    LOGFILE="backlog";
    ### ### How many backups we should keep :
    BACKNUM=7;
    ### Email when done
    EMAIL="user@company.com";
    EMAIL_SUBJECT="Zimbra_backup";
    ### Mount backup share
    CMD_START="mount //SOME.NAS.IP.ADDRESS/backup /backup/zimbra -o lfs,username=backupuseryourown,password=passwordyourown";
    CMD_END="umount /backup/zimbra";
    ### END OF CONFIGURATION ###
    ### ADD : spam folder backup.
    
    # Execute start command
    $CMD_START;
    
    # Initial check-list
    if [ `whoami` != "root" ]; then
    	echo -e "\nPlease run this script as root user. Aborting backup.";
    	exit
    fi
    if [ ! -f $BASEDIR/ignore_lst.txt ]; then
    	echo -e "\nMissing 'ignore_lst.txt'. Aborting backup."
    	exit
    fi
    if [ ! -f $BASEDIR/attribs_lst.txt ]; then
    	echo -e "\nMissing 'attribs_lst.txt'. Aborting backup."
    	exit
    fi
    DATE=`date +%Y%m%d`;
    DEL=`date "-d -$BACKNUM days" +%Y%m%d`;
    
    # Dir creation if missing
    if [ ! -d $BACKDIR ]; then mkdir $BACKDIR; fi
    # Remove restore files in case a previous backup had failed on the same day.
    if [ -d $BACKDIR/$DATE ]; then rm -rf $BACKDIR/$DATE; echo "Removing previous failed backup : "$BACKDIR/$DATE; fi
    mkdir $BACKDIR/$DATE;
    mkdir $BACKDIR/$DATE/prefs;                                                                                                                      
    mkdir $BACKDIR/$DATE/restore;
    chown -R zimbra:zimbra $BACKDIR;
    
    # Insert date and time in log file
    echo "subject: "$EMAIL_SUBJECT > $BACKDIR/$LOGFILE"_"$DATE.log;
    date >> $BACKDIR/$LOGFILE"_"$DATE.log;
    
    # Get and export prefs for all users
    su - zimbra -c "zmprov -l gaa -v > $BACKDIR/$DATE/accountprefs.txt";
    
    # Split accounprefs.txt into one file per account
    cd $BACKDIR/$DATE/prefs;
    csplit -ksz -f pref $BACKDIR/$DATE/accountprefs.txt '/^#\s*name/' {*};
    cd ../;
    
    echo "Backing up accounts in "$BACKDIR/$DATE".";
    echo "Backing up accounts in "$BACKDIR/$DATE"." >> $BACKDIR/$LOGFILE"_"$DATE.log;
    # Parse pref files
    for PREFFILE in $BACKDIR/$DATE/prefs/*; do
    	ACCOUNT=`cat $PREFFILE | grep ^"# name" | cut -d" " -f3`;
    	SHORTNAME=`echo $ACCOUNT | awk -F@ '{print $1}' | awk -F. '{print $1}'`;
    	
    # If account name not in ignore_lst.txt, get all desired prefs (present in attribs_lst.txt)
    	RES=`grep -c $SHORTNAME $BASEDIR/ignore_lst.txt`;
    	if [ $RES -eq "0" ];  then
    		echo "Backing up: "$ACCOUNT;
    		echo "Backing up: "$ACCOUNT >> $BACKDIR/$LOGFILE"_"$DATE.log;
    		mv $PREFFILE $BACKDIR/$DATE/prefs/$ACCOUNT.txt;
    		RESTCOMMAND="ca "$ACCOUNT" ''";
    		for ATTRIB in `cat $BASEDIR/attribs_lst.txt`; do
    			ATTRIBLINE=`grep ^$ATTRIB":" $BACKDIR/$DATE/prefs/$ACCOUNT.txt`;
    			if [ -n "$ATTRIBLINE" ]; then
    				ATTRIBDETAIL=`echo $ATTRIBLINE | sed "s/"$ATTRIB":\s//1"`; 
    				if [ `echo $ATTRIBDETAIL | grep -c " "` -eq 0 ]; then
    					RESTCOMMAND+=" "$ATTRIB" "$ATTRIBDETAIL;
    				else
    					RESTCOMMAND+=" "$ATTRIB" '"$ATTRIBDETAIL"'";
    				fi
    			fi
    		done;
    # Write account creation zmprov ready script
    		echo $RESTCOMMAND >> $BACKDIR/$DATE/restore/restore_accounts.txt;
    # Write password restore zmprov ready script
    		ACCOUNTPWD=`cat $BACKDIR/$DATE/prefs/$ACCOUNT.txt | grep "userPassword" | cut -d" " -f2`;
    		if [ $ACCOUNTPWD!="VALUE-BLOCKED" ] && [ -n $ACCOUNTPWD ]; then echo "ma "$ACCOUNT" userPassword "$ACCOUNTPWD >> $BACKDIR/$DATE/restore/restore_passwords.txt; fi
    # Write Alias restore zmprov ready script		
    		ALIASES=`cat $BACKDIR/$DATE/prefs/$ACCOUNT.txt | grep "zimbraMailAlias:" | cut -d" " -f2`;
    		for ALIASLINE in $ALIASES; do
    			if [ -n $ALIASLINE ]; then echo "aaa "$ACCOUNT" "$ALIASLINE >> $BACKDIR/$DATE/restore/restore_aliases.txt; fi;
    		done;
    # Write forward restore zmprov ready script (pref and hidden)	
    		FORWARDS=`cat $BACKDIR/$DATE/prefs/$ACCOUNT.txt | grep "zimbraPrefMailForwardingAddress:" | cut -d" " -f2`;
            	for FORWARDLINE in $FORWARDS; do
            		if [ -n $FORWARDLINE ]; then echo "ma "$ACCOUNT" zimbraPrefMailForwardingAddress "$FORWARDLINE >> $BACKDIR/$DATE/restore/restore_forwards.txt; fi;
    		done;
    		FORWARDS=`cat $BACKDIR/$DATE/prefs/$ACCOUNT.txt | grep "zimbraMailForwardingAddress:" | cut -d" " -f2`;
    		for FORWARDLINE in $FORWARDS; do
    			if [ -n $FORWARDLINE ]; then echo "ma "$ACCOUNT" +zimbraMailForwardingAddress "$FORWARDLINE >> $BACKDIR/$DATE/restore/restore_forwards.txt; fi;
    		done;
    # Export account mailbox and filters
    		su - zimbra -c "zmmailbox -z -m $ACCOUNT getRestURL '//?fmt=tgz' > $BACKDIR/$DATE/$ACCOUNT.tgz" 2>> $BACKDIR/$LOGFILE"_"$DATE.log;
    		su - zimbra -c "zmmailbox -z -m $ACCOUNT getRestURL '/Trash?fmt=tgz' > $BACKDIR/$DATE/$ACCOUNT.Trash.tgz" 2>> $BACKDIR/$LOGFILE"_"$DATE.log;
    		su - zimbra -c "zmmailbox -z -m $ACCOUNT gfrl > $BACKDIR/$DATE/$ACCOUNT.filters.txt" 2>> $BACKDIR/$LOGFILE"_"$DATE.log;
    		echo $ACCOUNT >> $BACKDIR/$DATE/accounts.txt;
    	else
    # If account name in ignore_lst.txt, remove prefs file
    		echo "Skipping: "$ACCOUNT;
    		echo "Skipping: "$ACCOUNT >> $BACKDIR/$LOGFILE"_"$DATE.log;
    		rm -r $PREFFILE;
    	fi
    done;
    
    # End operations
    echo "Accounts backed up in "$BACKDIR/$DATE".";
    echo "Accounts backed up in "$BACKDIR/$DATE"." >> $BACKDIR/$LOGFILE"_"$DATE.log;
    echo "Deleting "$BACKNUM" days old backup in "$BACKDIR/$DEL".";
    echo "Deleting "$BACKNUM" days old backup in "$BACKDIR/$DEL"." >> $BACKDIR/$LOGFILE"_"$DATE.log;
    
    rm -rf $BACKDIR/$DEL;
    date >> $BACKDIR/$LOGFILE"_"$DATE.log;
    /opt/zimbra/postfix/sbin/sendmail -v $EMAIL < $BACKDIR/$LOGFILE"_"$DATE.log;
    #execute end command
    cd /;
    $CMD_END;
    echo "Done.";
    Here is an exemple for the attribs_lst.txt file :
    Code:
    c
    cn
    co
    company
    description
    displayName
    gn
    l
    o
    ou
    street
    postalCode
    sn
    st
    telephoneNumber
    title
    Here is an exemple for the ignore_lst.txt file :
    Code:
    ham
    spam
    wiki
    domain_wiki
    I would really appreciate any hint / suggestion to get this script better.

    Thanks !

    Miguel Cordas.
    Last edited by mcordas; 11-21-2010 at 05:26 AM. Reason: typo

  2. #2
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    4

    Default

    This looks promising! Excellent work!
    Two questions:
    - what's the best way to guarantee that every single configurable attribute is restorable? Copying every line from prefs/$ACCOUNT.txt to attribs_lst.txt?
    - out of curiosity, why do you backup the 'Trash' folder?
    Last edited by pessoa; 07-14-2011 at 04:51 PM.

Similar Threads

  1. zmrestore from ZCS 5.0.13 to ZCS 6.0.8
    By philwillchen in forum Administrators
    Replies: 1
    Last Post: 10-06-2010, 09:06 PM
  2. Trouble Sending mail - All Messages deferred!
    By SiteDiscovery in forum Administrators
    Replies: 7
    Last Post: 09-03-2009, 05:52 AM
  3. Keeping a backup server synced with live server
    By Q-Mike in forum Administrators
    Replies: 5
    Last Post: 04-11-2008, 02:40 PM
  4. FYI: ZCS NE backup to fuse/sshfs mount, worked.
    By jagipson in forum Administrators
    Replies: 0
    Last Post: 09-28-2007, 07:37 AM
  5. ZCS 3.2 Beta Available
    By KevinH in forum Announcements
    Replies: 31
    Last Post: 07-07-2006, 04:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •