Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
225 changes: 189 additions & 36 deletions zmrestore
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,18 @@ 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()
{
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
}
Expand All @@ -25,63 +33,148 @@ usage()
{

cat << EOF
zmrestore: zmrestore [-o path] -a|-u mailbox
zmrestore: zmrestore [-v 0..9] [-o path] -a|-u mailbox [-f file] [-d domain] [-t backupdatetime]
EOF

}

restore_mailbox()
{
mbox=$1
log "start restore mailbox $mbox"
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 $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"
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"
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 "$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/$domain/$mbox-$RESTORE_DATE.tgz - File Not Found"
exit 5
fi
else
log "ERROR: No domain provided. Must specify domain or file for restore_mailbox()"
exit 1
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"
$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]
while getopts :aho:u: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
Expand All @@ -104,8 +197,34 @@ 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="-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
* )
Expand All @@ -117,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
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