#!/bin/sh
#==================================================================
#*
#* Copyleft, dinner3000
#* All rights give up.
#*
#* 文件名称:auto_trans.sh
#* 文件标识:
#* 摘 要:自动ftp脚本(全参数版)
#* 作 者:dinner3000
#* 修 改 者:
#* 修改内容:
#* 修改时间:
#* 版 本:2005-5-12
#* $Id$
#*
#==================================================================
#local variable
PS_FULLNAME=`basename ${0}` #脚本全名
PS_EXT=`echo ${PS_FULLNAME} |awk -F"." '{if(index($0,".")==0){printf("");}else{printf("%s",$NF)}}'` #脚本扩展名
PS_NAME=`echo ${PS_FULLNAME} |sed "s/.${PS_EXT}//g"` #脚本名称
PS_DIR=`dirname ${0}`
TMP_STR=`echo ${PS_DIR} |awk -F"/" '{printf("%s",$1);}'`
if [ -z ${TMP_STR} ]; then
ABS_PATH=${PS_DIR}
else
cd ${PS_DIR}
ABS_PATH=`pwd`
fi
WORK_DIR=${ABS_PATH} #脚本所在绝对路径
LOCAL_DIR=`pwd` #初始化FTP本地目录
#remote variable
REMOTE_IP="127.0.0.1" #初始化FTP主机地址
USER="Anomymous" #初始化FTP帐号
PSWD="[email protected]" #初始化FTP密码
REMOTE_DIR="/" #初始化FTP远端目录
BACKUP_DIR="undefined" #初始化文件备份目录
FILE_TYPE="*.*" #初始化要传送的文件类型
FILE_LIST="undefined" #初始化要传送的文件列表
METHOD="undefined" #初始化FTP传送方向
CONN_MODE="passive off" #初始化FTP连接模式
TRAN_MODE="binary" #初始化FTP传送模式
CLEAN="false" #是否清除传送成功的文件
#log path
RUN_LOG="${WORK_DIR}/${PS_NAME}_run.log" #脚本运行记录
TRANSFER_LOG="${WORK_DIR}/${PS_NAME}_transfer.log" #FTP传送临时记录
#-----------------------------------------------------------
loglite(){ #不换行记录
echo -n "${1}" >> ${RUN_LOG} 2>&1
}
log(){ 换行记录
echo "${1}" >> ${RUN_LOG} 2>&1
}
quit(){ #正常退出
log "===E==N==D=====`date`=====E==N==D==="
rm -f ${PS_LOCK}
exit
}
error(){ #非法退出
echo "${1}" 2>&1
log "${1}"
quit
}
#============================================================
while getopts :UCDPAh:u:p:r:l:b:t:f: OPTION #分析执行参数
do
case ${OPTION} in
h) #FTP主机地址
REMOTE_IP=${OPTARG}
;
u) #FTP帐号
USER=${OPTARG}
;
p) #FTP密码
PSWD=${OPTARG}
;
r) #FTP远端目录
REMOTE_DIR=${OPTARG}
;
l) #FTP本地目录
LOCAL_DIR=${OPTARG}
;
b) #文件备份目录
BACKUP_DIR=${OPTARG}
;
t) #要传送的文件类型
FILE_TYPE=${OPTARG}
;
f) #要传送的文件列表
FILE_LIST=${OPTARG}
;
U) #FTP传送方向(上传)
METHOD="upload"
;
D) #FTP传送方向(下载)
METHOD="download"
;
P) #FTP连接模式
CONN_MODE="passive on"
;
A) #FTP传送模式
TRAN_MODE="ascii"
;
C) #清除传送成功的文件
CLEAN="true"
;
\?)#show usage
error "${PS_FULLNAME} -[U(pload) D(ownload)] -[C(lean)] -[P(assive)] -[A(scii)] -[h host] -[u user] -[p password] -[r remote path] -[l local path] -[b backup path] -[t filetype] -[f file list]."
;
esac
done
PS_LOCK="${WORK_DIR}/${PS_NAME}_`echo "${USER}:${PSWD}@${REMOTE_IP}${REMOTE_DIR} => ${LOCAL_DIR}" |md5`.pid" #进程互斥锁, 只有参数完全相同才会互斥
if [ ${METHOD} = "undefined" ]; then #FTP传送方向未定义错
error "Tansfer mode(-[U(pload) D(ownload)]) must be set.(1)"
fi
#-------------------------------------------------------------
#init run log
log ""
log "###############`date`################"
log "REMOTE: ${USER}:${PSWD}@${REMOTE_IP}${REMOTE_DIR}"
log "LOCAL: ${LOCAL_DIR}"
log "METHOD: ${METHOD}"
log "FILE_TYPE: ${FILE_TYPE}"
log "WORK_DIR: ${WORK_DIR}"
log "PS_LOCK: ${PS_LOCK}"
log "RUN_LOG: ${RUN_LOG}"
log "TRANSFER_LOG: ${TRANSFER_LOG}"
log "###########################################################"
log ""
if [ -f ${PS_LOCK} ]; then #进程互斥
echo "`date`: ${PS_FULLNAME} already running. Exit." 2>&1
log "`date`: ${PS_FULLNAME} already running. Exit." 2>&1
exit
else
ps auxw |grep /bin/sh |grep ${PS_FULLNAME} |grep -v grep > ${PS_LOCK}
fi
#-----------------------------------------------------------
#create file list
if [ ${METHOD} = "upload" ]; then
if [ ${FILE_LIST} = "undefined" ]; then
BATCH_OPT="mput ${FILE_TYPE}"
else
BATCH_OPT=`sed 's/^/put /g' ${FILE_LIST}`
rm -f ${FILE_LIST}
fi
elif [ ${METHOD} = "download" ]; then
if [ ${FILE_LIST} = "undefined" ]; then
BATCH_OPT="mget ${FILE_TYPE}"
else
BATCH_OPT=`sed 's/^/get /g' ${FILE_LIST}`
rm -f ${FILE_LIST}
fi
else
error "Tansfer mode(-[U(pload) D(ownload)]) must be set.(2)"
fi
#transfer files
cd ${WORK_DIR}
#excute ftp batch
echo "
open ${REMOTE_IP}
prompt off
user ${USER} ${PSWD}
lcd ${LOCAL_DIR}
cd ${REMOTE_DIR}
${CONN_MODE}
${TRAN_MODE}
${BATCH_OPT}
close
bye
" |ftp -v -n |tee ${TRANSFER_LOG} |sed 's/^/>/g' >> ${RUN_LOG} #执行批量传送, 并记录日志
TOTAL_TRANSFERED=`grep "226 Transfer complete" ${TRANSFER_LOG} |wc -l` #统计传送数量
log "TOTAL TRANSFERED: ${TOTAL_TRANSFERED}"
log ""
#clear or backup files
if [ ${METHOD} = "upload" ]; then
if [ ${BACKUP_DIR} != "undefined" ]; then #备份已上传的文件
log "Backup up files ... "
COUNT=0
BATCH_BKUP=`grep -B 3 "226 Transfer complete." ${TRANSFER_LOG} |grep "local:" |awk -F" " '{printf("%s\n",$2);}'`
cd ${LOCAL_DIR}
for I in ${BATCH_BKUP}
do
mv -v ${I} ${BACKUP_DIR}/ |sed 's/^/>/g' >> ${RUN_LOG} 2>&1
COUNT=`expr ${COUNT} + 1`
done
log "TOTAL BACKUPED: ${COUNT}"
log ""
else
if [ ${CLEAN} = "true" ]; then #清除已上传的文件
log "Remove transfered files ... "
COUNT=0
BATCH_CLEAN=`grep -B 3 "226 Transfer complete." ${TRANSFER_LOG} |grep "local:" |awk -F" " '{printf("%s\n",$2);}'`
log "Execute local clear(${LOCAL_DIR}) ... "
cd ${LOCAL_DIR}
for I in ${BATCH_CLEAN}
do
rm -fv ${I} |sed 's/$/ deleted./g' >> ${RUN_LOG} 2>&1
COUNT=`expr ${COUNT} + 1`
done
log "TOTAL DELETED: ${COUNT}"
log ""
fi
fi
elif [ ${METHOD} = "download" ]; then
if [ ${CLEAN} = "true" ]; then #清除已下载的文件
log "Execute remote clear ... "
BATCH_CLEAN=`grep -B 3 "226 Transfer complete." ${TRANSFER_LOG} |grep "local:" |awk -F" " '{printf("delete %s\n",$2);}'`
echo "
open ${REMOTE_IP}
prompt off
user ${USER} ${PSWD}
lcd ${LOCAL_DIR}
cd ${REMOTE_DIR}
${BATCH_CLEAN}
close
bye
" |ftp -v -n |tee ${TRANSFER_LOG} |sed 's/^/>/g' >> ${RUN_LOG} 2>&1
COUNT=`grep "250 DELE command successful." ${TRANSFER_LOG} |wc -l`
log "TOTAL DELETED: ${COUNT}"
log ""
fi
else
error "Tansfer mode(-[U(pload) D(ownload)]) must be set.(3)"
fi
quit #正常退出
#========================================================
调用方法:
auto_trans.sh
-[U(pload) D(ownload)]
传送方向, 上传或下载, 必须指定, 且只能指定其一
-[C(lean)]
清除已传送的文件(默认不清除)
-[P(assive)]
被动模式(默认主动)
-[A(scii)]
文本模式(默认二进制)
-[h host]
主机地址(默认127.0.0.1)
-[u user]
帐号(默认匿名)
-[p password]
密码(默认匿名)
-[r remote path]
远端路径(默认根目录)
-[l local path]
本地路径(默认当前目录)
-[b backup path]
文件备份目录(默认不备份)
-[t filetype]
要传送的文件类型(默认*.*)
-[f file list]
要传送的文件列表(默认无)
|
|
|