From cb340d8c323697c07f7c469c50b3d2d4a829d97d Mon Sep 17 00:00:00 2001 From: Christo De Lange Date: Mon, 8 Jul 2013 22:05:09 -0400 Subject: [PATCH 1/2] Refine restore --- zmrestore | 49 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 13 deletions(-) mode change 100644 => 100755 zmrestore diff --git a/zmrestore b/zmrestore old mode 100644 new mode 100755 index 694b5ef..cba4ca4 --- a/zmrestore +++ b/zmrestore @@ -13,6 +13,10 @@ RESTORE_DATE=`date +%G-%m-%d_%H-%M` # set default option is 0 (unset) VERBOSE=0 +source $ZIMBRA_BIN/zmshutil || exit 1 +zmsetvars \ + zimbra_server_hostname + log() { # verbose option is turn on @@ -25,7 +29,7 @@ usage() { cat << EOF -zmrestore: zmrestore [-o path] -a|-u mailbox +zmrestore: zmrestore [-o path] -a|-u mailbox [-f file] EOF } @@ -34,16 +38,28 @@ restore_mailbox() { mbox=$1 log "start restore mailbox $mbox" - - if [ ! -z $2 ]; then - $ZIMBRA_BIN/zmmailbox -z -m $mbox getRestURL "//?fmt=tgz&resolve=reset" > $ZIMBRA_RESTORE_DIR/$2/$mbox-$RESTORE_DATE.tgz - log "restore mailbox $mbox successful" - log "restore to $ZIMBRA_RESTORE_DIR/$2" - + zimbraMailSSLPort=`zmprov gs $zimbra_server_hostname zimbraMailSSLPort | egrep -v -e '^(#|$)' | cut -d ' ' -f 2` + if [ -z "$zimbraMailSSLPort" ] ; then + log "ERROR: Cannot find zimbraMailSSLPort" + exit 1 + fi + if [ ! -z "$2" -a -z "$3" ] ; then + if [ -f $ZIMBRA_RESTORE_DIR/$2/$mbox-$RESTORE_DATE.tgz ] ; then + log "$ZIMBRA_BIN/zmmailbox -v -z -m $mbox postRestURL -u https://${zimbra_server_hostname}:${zimbraMailSSLPort} '//?fmt=tgz&resolve=reset' $ZIMBRA_RESTORE_DIR/$2/$mbox-$RESTORE_DATE.tgz" + $ZIMBRA_BIN/zmmailbox -v -z -m $mbox postRestURL -u https://${zimbra_server_hostname}:${zimbraMailSSLPort} '//?fmt=tgz&resolve=reset' $ZIMBRA_RESTORE_DIR/$2/$mbox-$RESTORE_DATE.tgz + log "restore mailbox $mbox from $ZIMBRA_RESTORE_DIR/$2/$mbox-$RESTORE_DATE.tgz successful" + else + log "ERROR: Cannot restore mailbox $mbox from $ZIMBRA_RESTORE_DIR/$2/$mbox-$RESTORE_DATE.tgz - File Not Found" + exit 5 + fi else - $ZIMBRA_BIN/zmmailbox -z -m $mbox getRestURL "//?fmt=tgz&resolve=reset" > $ZIMBRA_RESTORE_DIR/$mbox-$RESTORE_DATE.tgz - log "restore mailbox $mbox successful" - log "restore to $ZIMBRA_RESTORE_DIR" + if [ ! -z "$3" ]; then + $ZIMBRA_BIN/zmmailbox -v -z -m $mbox postRestURL -u https://${zimbra_server_hostname}:${zimbraMailSSLPort} "//?fmt=tgz&resolve=reset" $3 + log "restore mailbox $mbox from $3 successful" + else + $ZIMBRA_BIN/zmmailbox -v -z -m $mbox postRestURL -u https://${zimbra_server_hostname}:${zimbraMailSSLPort} "//?fmt=tgz&resolve=reset" $ZIMBRA_RESTORE_DIR/$mbox-$RESTORE_DATE.tgz + log "restore mailbox $mbox from $ZIMBRA_RESTORE_DIR successful" + fi fi } @@ -80,8 +96,8 @@ create_pack_restore() } # Option -# zmrestore [-ah] [-u mailbox] [-o path] -while getopts :aho:u:v OPTION; do +# zmrestore [-ah] [-u mailbox] [-o path] [-f backupfile] +while getopts :aho:u:f:v OPTION; do case $OPTION in a ) AFLAG=1 @@ -104,6 +120,13 @@ while getopts :aho:u:v OPTION; do UFLAG=$OPTARG fi ;; + f ) + if [ -z $OPTARG ]; then + echo "-f option must specify an existing backup file" + else + UFILE=$OPTARG + fi + ;; v ) VERBOSE=1 ;; @@ -127,7 +150,7 @@ if [ ! -z $UFLAG ] && [[ $AFLAG -eq 1 ]]; then exit 1 # if declare UFLAG elif [ ! -z $UFLAG ]; then - restore_mailbox $UFLAG + restore_mailbox $UFLAG from-file $UFILE fi # if AFLAG on From b3750c97b051836fd9957a54eee6720ddf19bbda Mon Sep 17 00:00:00 2001 From: Christo De Lange Date: Tue, 9 Jul 2013 08:41:05 -0400 Subject: [PATCH 2/2] Rework zmrestore to be more useful and practical --- zmrestore | 214 +++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 172 insertions(+), 42 deletions(-) diff --git a/zmrestore b/zmrestore index cba4ca4..3882050 100755 --- a/zmrestore +++ b/zmrestore @@ -19,8 +19,12 @@ zmsetvars \ log() { + local lev=1 + if [[ ! -z $2 ]] ; then + lev=$2 + fi # verbose option is turn on - if [ $VERBOSE -eq 1 ]; then + if [ $VERBOSE -ge $lev ]; then echo $1 fi } @@ -29,75 +33,148 @@ usage() { cat << EOF -zmrestore: zmrestore [-o path] -a|-u mailbox [-f file] +zmrestore: zmrestore [-v 0..9] [-o path] -a|-u mailbox [-f file] [-d domain] [-t backupdatetime] EOF } restore_mailbox() { - mbox=$1 + local OPTIND=1 + local OPTARG='' + local USER='' + local domain='' + local FILE='' + while getopts :f:d:u:t: OPTION $* ; do + case $OPTION in + d ) + domain=$OPTARG + ;; + f ) + FILE=$OPTARG + ;; + t ) + RESTORE_DATE=$OPTARG + ;; + u ) + USER=$OPTARG + ;; + * ) + log "ERROR: Unknown option $OPTION" + exit 1 + ;; + esac + done + + if [ -z "$USER" ] ; then + log "ERROR: MUST specify a user account for restore_mailbox()" + exit 1 + fi + + local mbox=$USER + + log "DEBUG: mbox:$mbox FILE:$FILE RESTORE_DATE:$RESTORE_DATE domain:$domain" 3 log "start restore mailbox $mbox" - zimbraMailSSLPort=`zmprov gs $zimbra_server_hostname zimbraMailSSLPort | egrep -v -e '^(#|$)' | cut -d ' ' -f 2` + local zimbraMailSSLPort=`zmprov gs $zimbra_server_hostname zimbraMailSSLPort | egrep -v -e '^(#|$)' | cut -d ' ' -f 2` if [ -z "$zimbraMailSSLPort" ] ; then log "ERROR: Cannot find zimbraMailSSLPort" exit 1 fi - if [ ! -z "$2" -a -z "$3" ] ; then - if [ -f $ZIMBRA_RESTORE_DIR/$2/$mbox-$RESTORE_DATE.tgz ] ; then - log "$ZIMBRA_BIN/zmmailbox -v -z -m $mbox postRestURL -u https://${zimbra_server_hostname}:${zimbraMailSSLPort} '//?fmt=tgz&resolve=reset' $ZIMBRA_RESTORE_DIR/$2/$mbox-$RESTORE_DATE.tgz" - $ZIMBRA_BIN/zmmailbox -v -z -m $mbox postRestURL -u https://${zimbra_server_hostname}:${zimbraMailSSLPort} '//?fmt=tgz&resolve=reset' $ZIMBRA_RESTORE_DIR/$2/$mbox-$RESTORE_DATE.tgz - log "restore mailbox $mbox from $ZIMBRA_RESTORE_DIR/$2/$mbox-$RESTORE_DATE.tgz successful" + if [ -z "$FILE" ] ; then + if [ ! -z "$domain" ] ; then + if [ -f $ZIMBRA_RESTORE_DIR/$domain/$mbox-$RESTORE_DATE.tgz ] ; then + $ZIMBRA_BIN/zmmailbox -v -z -m $mbox postRestURL -u https://${zimbra_server_hostname}:${zimbraMailSSLPort} '//?fmt=tgz&resolve=reset' $ZIMBRA_RESTORE_DIR/$domain/$mbox-$RESTORE_DATE.tgz + log "restore mailbox $mbox from $ZIMBRA_RESTORE_DIR/$domain/$mbox-$RESTORE_DATE.tgz successful" else - log "ERROR: Cannot restore mailbox $mbox from $ZIMBRA_RESTORE_DIR/$2/$mbox-$RESTORE_DATE.tgz - File Not Found" + log "ERROR: Cannot restore mailbox $mbox from $ZIMBRA_RESTORE_DIR/$domain/$mbox-$RESTORE_DATE.tgz - File Not Found" exit 5 fi - else - if [ ! -z "$3" ]; then - $ZIMBRA_BIN/zmmailbox -v -z -m $mbox postRestURL -u https://${zimbra_server_hostname}:${zimbraMailSSLPort} "//?fmt=tgz&resolve=reset" $3 - log "restore mailbox $mbox from $3 successful" else - $ZIMBRA_BIN/zmmailbox -v -z -m $mbox postRestURL -u https://${zimbra_server_hostname}:${zimbraMailSSLPort} "//?fmt=tgz&resolve=reset" $ZIMBRA_RESTORE_DIR/$mbox-$RESTORE_DATE.tgz - log "restore mailbox $mbox from $ZIMBRA_RESTORE_DIR successful" + log "ERROR: No domain provided. Must specify domain or file for restore_mailbox()" + exit 1 fi + else + $ZIMBRA_BIN/zmmailbox -v -z -m $mbox postRestURL -u https://${zimbra_server_hostname}:${zimbraMailSSLPort} "//?fmt=tgz&resolve=reset" $FILE + log "restore mailbox $mbox from $FILE successful" fi -} +} -create_pack_restore() -{ +unpack_restore() +{ log "search domain" - domains=`$ZIMBRA_BIN/zmprov gad` + local OPTIND=1 + local OPTARG='' + local USER='' + local domain='' + local FILE='' + local DATE='' + local domains=`$ZIMBRA_BIN/zmprov gad` + + while getopts :f:d:u:t: OPTION $* ; do + case $OPTION in + d ) + domains=$OPTARG + ;; + f ) + FILE=$OPTARG + ;; + t ) + RESTORE_DATE=$OPTARG + DATE="-t $RESTORE_DATE" + ;; + u ) + USER=$OPTARG + ;; + * ) + log "ERROR: Unknown option $OPTION" + exit 1 + ;; + esac + done + if [ -z "$FILE" ] ; then + FILE=$ZIMBRA_RESTORE_DIR/$domain-$RESTORE_DATE.tgz + fi + if [ ! -f "$FILE" ] ; then + log "ERROR: $FILE does not exist!" + exit 5 + fi for domain in $domains; do log "start restore domain $domain" - # get all accounts from domain - mboxs=`$ZIMBRA_BIN/zmprov -l gaa $domain` + local mboxs=$USER + if [ -z "$USER" ] ; then + # get all accounts from domain + mboxs=`$ZIMBRA_BIN/zmprov -l gaa $domain` + fi + cd $ZIMBRA_RESTORE_DIR # check directory if -o is set mkdir -p $ZIMBRA_RESTORE_DIR/$domain + cd $ZIMBRA_RESTORE_DIR/$domain + if [ ! -f "$FILE" ] ; then + log "ERROR: $FILE does not exist!" + exit 5 + fi + tar zxf $FILE # fetch account in tgz format for mbox in $mboxs; do -# $ZIMBRA_BIN/zmmailbox -z -m $mbox getRestURL "//?fmt=tgz&resolve=reset" > $ZIMBRA_RESTORE_DIR/$domain/$mbox-$RESTORE_DATE.tgz - restore_mailbox $mbox $domain +# #$ZIMBRA_BIN/zmmailbox -z -m $mbox getRestURL "//?fmt=tgz&resolve=reset" > $ZIMBRA_RESTORE_DIR/$domain/$mbox-$RESTORE_DATE.tgz + restore_mailbox -u $mbox -d $domain $DATE done - # pack mailbox in domain - cd $ZIMBRA_RESTORE_DIR/$domain - tar czf $domain-$RESTORE_DATE.tgz `ls` - mv $domain-$RESTORE_DATE.tgz $ZIMBRA_RESTORE_DIR cd $ZIMBRA_RESTORE_DIR rm -rf $ZIMBRA_RESTORE_DIR/$domain - + log "restore domain $domain successful" done } # Option -# zmrestore [-ah] [-u mailbox] [-o path] [-f backupfile] -while getopts :aho:u:f:v OPTION; do +# zmrestore [-ah] [-u mailbox] [-o path] [-f backupfile] [-d domain] [-t backupdate] +while getopts :aho:u:f:v:d:t: OPTION; do case $OPTION in a ) AFLAG=1 @@ -124,11 +201,30 @@ while getopts :aho:u:f:v OPTION; do if [ -z $OPTARG ]; then echo "-f option must specify an existing backup file" else - UFILE=$OPTARG + UFILE="-f $OPTARG" + fi + ;; + d ) + if [ -z $OPTARG ]; then + echo "-d option must specify the domain" + else + DOMAIN=$OPTARG + fi + ;; + t ) + if [ -z $OPTARG ]; then + echo "-t option must specify the date (of an existing backup)" + else + RESTORE_DATE=$OPTARG + DATE="-t $OPTARG" fi ;; v ) - VERBOSE=1 + if [[ -z "`echo $OPTARG | egrep -e '^[0-9]*\$' 2>/dev/null`" ]] ; then + usage + exit 1 + fi + VERBOSE=$OPTARG ;; # other option doesn't match * ) @@ -140,20 +236,54 @@ done # if not specific -a or -u it error and exit the script if [ -z $UFLAG ] && [[ $AFLAG -ne 1 ]]; then - echo "you must specific -a or -u option" + echo "you must specify -a or -u option" exit 1 -fi - # if UFLAG has value but AFLAG is used -if [ ! -z $UFLAG ] && [[ $AFLAG -eq 1 ]]; then +elif [ ! -z $UFLAG ] && [[ $AFLAG -eq 1 ]]; then echo "use -a or -u" exit 1 # if declare UFLAG elif [ ! -z $UFLAG ]; then - restore_mailbox $UFLAG from-file $UFILE -fi - + if [ -z "$UFILE" ] ; then + if [ -z "$DOMAIN" ] ; then + UFILE=$ZIMBRA_RESTORE_DIR/$UFLAG-$RESTORE_DATE.tgz + if [ ! -f "$UFILE" ] ; then + log "ERROR: Cannot determine backup file for -u $UFILE -d $DOMAIN -t $RESTORE_DATE" + exit 1 + fi + restore_mailbox -u $UFLAG -f $UFILE $DATE + else + UFILE=$ZIMBRA_RESTORE_DIR/$DOMAIN-$RESTORE_DATE.tgz + if [ ! -f "$UFILE" ] ; then + log "ERROR: Cannot determine backup file for -u $UFLAG -d $DOMAIN -t $RESTORE_DATE" + exit 1 + fi + unpack_restore -u $UFLAG -d $DOMAIN -f $UFILE $DATE + fi + else + if [ ! -f "$UFILE" ] ; then + log "ERROR: Cannot find backup file for -u $UFILE -f $UFILE -t $RESTORE_DATE" + exit 5 + fi + restore_mailbox -u $UFLAG -f $UFILE $DATE + fi # if AFLAG on -if [[ $AFLAG -eq 1 ]]; then - create_pack_retore +elif [[ $AFLAG -eq 1 ]]; then + if [ -z "$UFILE" ] ; then + if [ -z "$DOMAIN" ] ; then + unpack_restore $DATE + else + unpack_restore $DATE -d $DOMAIN + fi + else + if [ -z "$DOMAIN" ] ; then + unpack_restore $DATE -f $UFILE + else + unpack_restore $DATE -d $DOMAIN -f $UFILE + fi + fi +else + echo "you must specify -a or -u option" + exit 1 fi +