#!/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 return 0 } # @summary --get the parameter from the configure file by the key # @param key --the key of the parameter # @return value --the value of the parameter # 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)\ 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(config file:${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 [[ $# -eq 4 ]] 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 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)\ 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(config file:${workdir}/config/transfer.config)." 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 logger 0 "Connecting to ${remote_host_name}:${remote_base_folder_name}/${productid} by ${remote_host_userid}" ssh ${remote_host_userid}@${remote_host_name}:${remote_base_folder_name}/${productid} < zip ${productid}.zip * mv -f ${productid}.zip ${remote_base_folder_name} EOF result=`ssh ${remote_host_userid}@${remote_host_name} "ls ${remote_base_folder_name}/${productid}.zip"` grep ${productid}.zip ${result} result=$? if [[ ${result} -ne 0 ]] then logger 2 "Compress zip file in ${remote_base_folder_name}/${productid} directory on ${remote_host_name} failed" exit 1 fi logger 0 "Compress zip file in ${remote_base_folder_name}/${productid} directory on ${remote_host_name} successfully" logger 0 "Starting to download zip file ${productid}.zip from ${remote_base_folder_name}/${productid} to ${local_base_folder_name}" scp {$remote_host_userid}@${remote_host_name}:${remote_base_folder_name}/${productid}.zip ${local_base_folder_name} ls ${local_base_folder_name}/${productid}.zip | grep ${productid}.zip result=$? if [[ ${result} -ne 0 ]] then logger 2 "Can't find zip file ${productid}.zip in local directory ${local_base_folder_name}" exit 1 fi logger 0 "Download zip file ${productid}.zip from ${remote_base_folder_name}/${productid} to ${local_base_folder_name} successfully" unzip ssh {$remote_host_userid}@${remote_host_name} "rm -rf ${remote_base_folder_name}/${productid}" ssh {$remote_host_userid}@${remote_host_name} "rm -f ${remote_base_folder_name}/${productid}.zip"