new code

keeking發表於2011-04-29
#!/bin/ksh
#
# @Version: 1.0
# @Author: Huang Yan
# @Date: 04/20/2011
# @Description: This script. will open an connection to remote server
# and zip xml files that related to one product. Then download
# the zip file to local machine and delete the remote zip
# file and xml files.
##--------------------------------------------------------------------##
##                         Function Area                              ##
##--------------------------------------------------------------------##

# @summary --format the message and show it in the console
# @param level --the level of the message(0-INFO,1-WARNING,2-ERROR)
# @param message --the message to show
# @return
#
logger()
{
   if [ $# -ne 2 ]
   then
       echo "Usage: logger 0|1|2 message"
       return 1
   fi
   
   type=$1
   message=$2

   case $type in
       0)type=INFO;;
       1)type=WARNING;;
       2)type=ERROR;;
       *)echo "Usage: logger 0|1|2 message"
         return 1;;
   esac

   echo `date "+%Y-%m-%d  %H:%M:%S,%s"` [`basename $0`] $type -- $message | tee -a "`basename $0`.`date "+%Y-%m-%d"`.log"
   return 0 
}

# @summary --get all parameters from the configure file
# @return
#
readParams()
{
    workdir=`pwd`
    if [[ ! -d ${workdir}/config && ! -f ${workdir}/config/transfer.config ]]
    then
        if [[ ${hasallparams} -ne 0 ]]
        then
            logger 2 "There is no config file or command line arguments,\
                  please give arguments in config file(${workdir}/config/transfer.config)\
                  or use command line arguments."
            return 1
        fi

    fi

    hasallparams=0

    productid=`grep productid ${workdir}/config/transfer.config | cut -f2 -d=`
    remote_host_name=`grep remote_host_name ${workdir}/config/transfer.config | cut -f2 -d=`
    remote_host_userid=`grep remote_host_userid ${workdir}/config/transfer.config | cut -f2 -d=`
    remote_base_folder_name=`grep remote_base_folder_name ${workdir}/config/transfer.config | cut -f2 -d=`
    local_base_folder_name=`grep local_base_folder_name ${workdir}/config/transfer.config | cut -f2 -d=`

    [[ -n $productid ]] && [[ -n $remote_host_name ]] && [[ -n $remote_host_userid ]] \
     && [[ -n $remote_base_folder_name ]] && [[ -n $local_base_folder_name ]] || hasallparams=1

    if [[ ${hasallparams} -ne 0 ]]
    then
        logger 2 "The config file is not configed correctly,\
                please reconfigure it(configfile:${workdir}/config/transfer.config)."
        return 1

    fi

    return 0
}


# @summary --compress files in the specified directory on remote server to a zip file
# @param remote_host_name --remote host name
# @param remote_host_userid --user id 
# @param remote_folder --folder on the remote server
# @param file_name --zip file name
# @return 0|1 --0 success, 1 error
#

compressFile()
{

    if [[ $# -ne 4 ]]
    then
        echo "Usage: compressFile remote_host_name remote_host_userid remote_folder file_name"
        return 1
    fi
   
    remote_host_name=$1
    remote_host_userid=$2
    remote_folder=$3
    file_name=$4
   
    ssh -tt ${remote_host_userid}@${remote_host_name} >/dev/null 2>&1 <    cd ${remote_base_folder_name}/${productid} && \
    zip -r ${productid}.zip * && \
    mv -f ${productid}.zip ${remote_base_folder_name}
    exit
    EOF
   
    ssh ${remote_host_userid}@${remote_host_name} "ls ${remote_base_folder_name}/${productid}.zip | grep ${productid}.zip" >/dev/null
   
    result=$?

    if [[ ${result} -ne 0 ]]
    then
        logger 2 "Compress zip file in ${remote_base_folder_name}/${productid} directory on${remote_host_name} failed"
        return 1
    fi
   
    return 0
}

# @summary --download the compressed file from remote server to local
# @param remote_host_name --remote host name
# @param remote_host_userid --user id 
# @param remote_file_path --path of the file on the remote server
# @param local_file_path --local path to store the file
# @return 0|1 --0 success, 1 error
#

downloadFile()
{
    if [[ $# -ne 4 ]]
    then
        echo "Usage: downloadFile remote_host_name remote_host_userid remote_file_path local_file_path"
        return 1
    fi
   
    remote_host_name=$1
    remote_host_userid=$2
    remote_file_path=$3
    local_file_path=$4
    size_remote=
    size_local=
   
    ssh ${remote_host_userid}@${remote_host_name} "ls -rlt ${remote_file_path}" | grep `basename ${remote_file_path}` | awk '{print $5}' | echo > ${size_remote}
   
    scp ${remote_host_userid}@${remote_host_name}:${remote_file_path} ${local_file_path}

    ls ${local_file_path}/`basename ${remote_file_path}` | grep `basename ${remote_file_path}` | awk '{print $5}' | echo > ${size_local}

    if [[ ${size_remote} -ne ${size_local} ]]
    then
        logger 2 "Remote file size is ${size_remote}, local file size is ${size_remote}"
        return 1
    fi
   
    return 0
}


##--------------------------------------------------------------------##
##                          Main Process                              ##
##--------------------------------------------------------------------##

productid=
remote_host_name=
remote_host_userid=
remote_base_folder_name=
local_base_folder_name=

hasallparams=

if [[ $# -eq 5 ]]
then
   hasallparams=0
   productid=$1
   remote_host_name=$2
   remote_host_userid=$3
   remote_base_folder_name=$4
   local_base_folder_name=$5  
else
   hasallparams=1
   readParams
fi

result=$?

if [[ ${result} -ne 0 ]]
then
    echo""
    echo "Usage: `basename $0` productid remote_host_name remote_host_userid remote_base_folder_name local_base_folder_name"
    exit 1
fi

#compress files on remote server
logger 0 "Connecting to ${remote_host_name}:${remote_base_folder_name}/${productid} by ${remote_host_userid}"
ssh -tt ${remote_host_userid}@${remote_host_name} >/dev/null 2>&1 <exit
EOF

result=$?

if [[ ${result} -ne 0 ]]
then
    logger 0 "Connecting to ${remote_host_name} failed"
    exit 1
fi

logger 0 "Connected to ${remote_host_name}"

logger 0 "Starting to compress files in ${remote_base_folder_name}/${productid} on ${remote_host_name}"

compressFile ${remote_host_name} ${remote_host_userid} ${remote_base_folder_name}/${productid}  ${productid}.zip
result=$?

if [[ ${result} -ne 0 ]]
then
    logger 2 "Compress files in ${remote_base_folder_name}/${productid} directory on ${remote_host_name} failed"
    exit 1
fi

logger 0 "Compress files in ${remote_base_folder_name}/${productid} on ${remote_host_name} successfully"

#download zip file to local
logger 0 "Starting to download ${productid}.zip from ${remote_host_name} to ${local_base_folder_name}"

downloadFile ${remote_host_name} ${remote_host_userid} ${remote_base_folder_name}/${productid}.zip ${local_base_folder_name}
result=$?

if [[ ${result} -ne 0 ]]
then
    logger 2 "Download file ${productid}.zip from ${remote_host_name} to ${local_base_folder_name} failed"
    exit 1
fi

logger 0 "Download file ${productid}.zip from ${remote_host_name} to ${local_base_folder_name} successfully"

#unzip file in local
logger 0 "Starting to unzip ${productid}.zip to ${local_base_folder_name}"

unzip -o ${local_base_folder_name}/${productid}.zip -d ${local_base_folder_name}/${productid}
result=$?

if [[ ${result} -ne 0 ]]
then
    logger 2 "Unzip file ${productid}.zip failed"
    exit 1
fi

logger 0 "Unzip file ${productid}.zip successfully"

#delete local zip file
logger 0 "Starting to delete ${productid}.zip from ${local_base_folder_name}"
rm -f ${local_base_folder_name}/${productid}.zip
result=$?

if [[ ${result} -ne 0 ]]
then
    logger 2 "Delete file ${productid}.zip from ${local_base_folder_name} failed"
    exit 1
fi

logger 0 "Delete file ${productid}.zip from ${local_base_folder_name} successfully"

# remove zip file and files from remote server
logger 0 "Starting to Delete file ${productid}.zip on ${remote_host_name}"

ssh ${remote_host_userid}@${remote_host_name} "rm -f ${remote_base_folder_name}/${productid}.zip"
result=$?

if [[ ${result} -ne 0 ]]
then
    logger 2 "Delete file ${productid}.zip on ${remote_host_name} failed"
    exit 1
fi
logger 0 "Delete file ${productid}.zip on ${remote_host_name} successfully"

logger 0 "Starting to Delete directory ${remote_base_folder_name} and files within it on ${remote_host_name}"

ssh ${remote_host_userid}@${remote_host_name} "rm -rf ${remote_base_folder_name}/${productid}"

result=$?

if [[ ${result} -ne 0 ]]
then
    logger 2 "Delete directory ${remote_base_folder_name} and files within it on ${remote_host_name} failed"
    exit 1
fi
logger 0 "Delete directory ${remote_base_folder_name} and files within it on ${remote_host_name} successfully"






來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7899089/viewspace-694059/,如需轉載,請註明出處,否則將追究法律責任。

相關文章