写了一个批量自动上传下载文件的小工具。 特点:
1. 文件名中可包含日期如TEST20070212.txt
2. 可多次运行,不会重复处理已成功的条目
3. 配置方便,异常处理完善
想来今后或许还用得上,所以放在这里晒一下。好不好用各位试下就知道了。 myftp.sh内容如下:
典型的配置范例myftp.cfg如下:
#SYSID FILE_NAME DIRECTION(DNLD/UPLD) REMOTE_PATH/ LOCAL_PATH/
0001 010YCDZ$YYYYMMDD$.txt UPLD ftp://user:passwd@10.245.62.226:21/dz/ /home/myftp/whdl/
0002 010YCJZ$YYYYMMDD$.TXT UPLD ftp://user:passwd@10.245.62.226:21/jz/ /home/myftp/whdl/
0003 010YJJS$YYYY-MM-DD$.TXT DNLD ftp://user:passwd@10.245.62.226:21/pk/js/ /home/myftp/whdl/
1. 文件名中可包含日期如TEST20070212.txt
2. 可多次运行,不会重复处理已成功的条目
3. 配置方便,异常处理完善
想来今后或许还用得上,所以放在这里晒一下。好不好用各位试下就知道了。 myftp.sh内容如下:
#!/bin/bash
#Author: Robin Guo
DATE_YYYYMMDD=`date +%Y%m%d`
DATE_YYMMDD=`date +%y%m%d`
DATE_YYYY_MM_DD=`date +%Y-%m-%d`
#日志文件~/log/YYYYMMDD.LOG
FTP_LOG=~/log/${DATE_YYYYMMDD}.LOG
#Function ftp_download
#Example:
#ftp_download ftp://interfs:qwerasdf@10.245.10.245:21/home/interfs/etc/config.test config.test
ftp_download()
{
if [ "$#" = "2" ]
then
if [ -f $2 ]
then
#文件已存在,说明上次下载已成功,可跳过
echo "INFO : $2 exists" | tee -a $FTP_LOG
else
echo "FROM : $1" | tee -a $FTP_LOG
echo "TO : $2" | tee -a $FTP_LOG
curl -s -P 21000 --connect-timeout 8 --max-time 60 $1 -o $2 > /dev/null 2>&1
CURL_RETCODE=$?
if [ "$CURL_RETCODE" = "0" ]
then
echo "INFO : downlaod $1 is ok" | tee -a $FTP_LOG
touch $2 > /dev/null 2>&1
else
echo "ERROR: download $1 is failed. curl return ($CURL_RETCODE)" | tee -a $FTP_LOG
rm -f $2 > /dev/null 2>&1
fi
fi
echo "" | tee -a $FTP_LOG
fi
}
#Function ftp_upload
#Example:
#ftp_upload uploadfile.txt ftp://interfs:qwerasdf@10.245.10.245:21/home/interfs/etc/upload.txt
ftp_upload()
{
if [ "$#" = "2" ]
then
if [ -f $1 ]
then
if [ -f $1.ok ]
then
#“文件.ok”存在,说明上次已成功上传,可跳过
echo "INFO : already upload $1" | tee -a $FTP_LOG
else
echo "FROM : $1" | tee -a $FTP_LOG
echo "TO : $2" | tee -a $FTP_LOG
curl -s -P 21000 --connect-timeout 8 --max-time 60 -T $1 $2 > /dev/null 2>&1
CURL_RETCODE=$?
if [ "$CURL_RETCODE" = "0" ]
then
#上传成功,做标记
touch ${1}.ok > /dev/null 2>&1
echo "INFO : upload $1 is ok" | tee -a $FTP_LOG
else
echo "ERROR: upload $1 is failed. curl return ($CURL_RETCODE)" | tee -a $FTP_LOG
fi
fi
else
echo "ERROR: upload failed. $1 not exists." | tee -a $FTP_LOG
fi
echo "" | tee -a $FTP_LOG
fi
}
date "+%y-%m-%d %H:%M" | tee -a $FTP_LOG
if [ "$#" != "1" ]
then
echo "Usage: ftp.sh ftp.cfg"
exit 1
fi
FTP_CONFIG=$1
if [ -f $FTP_CONFIG ]
then
echo "INFO : Config File: $FTP_CONFIG" | tee -a $FTP_LOG
echo "" | tee -a $FTP_LOG
else
echo "ERROR: Config File $FTP_CONFIG not exist" | tee -a $FTP_LOG
exit 1
fi
killall curl > /dev/null 2>&1
#从配置中读取条目
while read V_SYSID V_FILE_NAME V_DIRE V_REMOTE_PATH V_LOCAL_PATH
do
FIRST_CHAR=${V_SYSID:0:1}
if [ "$FIRST_CHAR" != "#" ]
then
#展开文件名中的日期段
FILE_NAME=${V_FILE_NAME}
FILE_NAME=${FILE_NAME/\$YYYYMMDD\$/$DATE_YYYYMMDD} # $YYYYMMDD$ to 20070120
FILE_NAME=${FILE_NAME/\$YYMMD\$/$DATEYYMMDD} # $YYMMDD$ to 070120
FILE_NAME=${FILE_NAME/\$YYYY-MM-DD\$/$DATE_YYYY_MM_DD} # $YYYY-MM-DD$ to 2007-01-20
if [ "$V_DIRE" = "DNLD" ]
then
echo "INFO : Download ${FILE_NAME} ... " | tee -a $FTP_LOG
ftp_download ${V_REMOTE_PATH}${FILE_NAME} ${V_LOCAL_PATH}${FILE_NAME}
fi
if [ "$V_DIRE" = "UPLD" ]
then
echo "INFO : Upload ${FILE_NAME}... " | tee -a $FTP_LOG
ftp_upload ${V_LOCAL_PATH}${FILE_NAME} ${V_REMOTE_PATH}${FILE_NAME}
fi
fi
done < $FTP_CONFIG
#Author: Robin Guo
DATE_YYYYMMDD=`date +%Y%m%d`
DATE_YYMMDD=`date +%y%m%d`
DATE_YYYY_MM_DD=`date +%Y-%m-%d`
#日志文件~/log/YYYYMMDD.LOG
FTP_LOG=~/log/${DATE_YYYYMMDD}.LOG
#Function ftp_download
#Example:
#ftp_download ftp://interfs:qwerasdf@10.245.10.245:21/home/interfs/etc/config.test config.test
ftp_download()
{
if [ "$#" = "2" ]
then
if [ -f $2 ]
then
#文件已存在,说明上次下载已成功,可跳过
echo "INFO : $2 exists" | tee -a $FTP_LOG
else
echo "FROM : $1" | tee -a $FTP_LOG
echo "TO : $2" | tee -a $FTP_LOG
curl -s -P 21000 --connect-timeout 8 --max-time 60 $1 -o $2 > /dev/null 2>&1
CURL_RETCODE=$?
if [ "$CURL_RETCODE" = "0" ]
then
echo "INFO : downlaod $1 is ok" | tee -a $FTP_LOG
touch $2 > /dev/null 2>&1
else
echo "ERROR: download $1 is failed. curl return ($CURL_RETCODE)" | tee -a $FTP_LOG
rm -f $2 > /dev/null 2>&1
fi
fi
echo "" | tee -a $FTP_LOG
fi
}
#Function ftp_upload
#Example:
#ftp_upload uploadfile.txt ftp://interfs:qwerasdf@10.245.10.245:21/home/interfs/etc/upload.txt
ftp_upload()
{
if [ "$#" = "2" ]
then
if [ -f $1 ]
then
if [ -f $1.ok ]
then
#“文件.ok”存在,说明上次已成功上传,可跳过
echo "INFO : already upload $1" | tee -a $FTP_LOG
else
echo "FROM : $1" | tee -a $FTP_LOG
echo "TO : $2" | tee -a $FTP_LOG
curl -s -P 21000 --connect-timeout 8 --max-time 60 -T $1 $2 > /dev/null 2>&1
CURL_RETCODE=$?
if [ "$CURL_RETCODE" = "0" ]
then
#上传成功,做标记
touch ${1}.ok > /dev/null 2>&1
echo "INFO : upload $1 is ok" | tee -a $FTP_LOG
else
echo "ERROR: upload $1 is failed. curl return ($CURL_RETCODE)" | tee -a $FTP_LOG
fi
fi
else
echo "ERROR: upload failed. $1 not exists." | tee -a $FTP_LOG
fi
echo "" | tee -a $FTP_LOG
fi
}
date "+%y-%m-%d %H:%M" | tee -a $FTP_LOG
if [ "$#" != "1" ]
then
echo "Usage: ftp.sh ftp.cfg"
exit 1
fi
FTP_CONFIG=$1
if [ -f $FTP_CONFIG ]
then
echo "INFO : Config File: $FTP_CONFIG" | tee -a $FTP_LOG
echo "" | tee -a $FTP_LOG
else
echo "ERROR: Config File $FTP_CONFIG not exist" | tee -a $FTP_LOG
exit 1
fi
killall curl > /dev/null 2>&1
#从配置中读取条目
while read V_SYSID V_FILE_NAME V_DIRE V_REMOTE_PATH V_LOCAL_PATH
do
FIRST_CHAR=${V_SYSID:0:1}
if [ "$FIRST_CHAR" != "#" ]
then
#展开文件名中的日期段
FILE_NAME=${V_FILE_NAME}
FILE_NAME=${FILE_NAME/\$YYYYMMDD\$/$DATE_YYYYMMDD} # $YYYYMMDD$ to 20070120
FILE_NAME=${FILE_NAME/\$YYMMD\$/$DATEYYMMDD} # $YYMMDD$ to 070120
FILE_NAME=${FILE_NAME/\$YYYY-MM-DD\$/$DATE_YYYY_MM_DD} # $YYYY-MM-DD$ to 2007-01-20
if [ "$V_DIRE" = "DNLD" ]
then
echo "INFO : Download ${FILE_NAME} ... " | tee -a $FTP_LOG
ftp_download ${V_REMOTE_PATH}${FILE_NAME} ${V_LOCAL_PATH}${FILE_NAME}
fi
if [ "$V_DIRE" = "UPLD" ]
then
echo "INFO : Upload ${FILE_NAME}... " | tee -a $FTP_LOG
ftp_upload ${V_LOCAL_PATH}${FILE_NAME} ${V_REMOTE_PATH}${FILE_NAME}
fi
fi
done < $FTP_CONFIG
典型的配置范例myftp.cfg如下:
#SYSID FILE_NAME DIRECTION(DNLD/UPLD) REMOTE_PATH/ LOCAL_PATH/
0001 010YCDZ$YYYYMMDD$.txt UPLD ftp://user:passwd@10.245.62.226:21/dz/ /home/myftp/whdl/
0002 010YCJZ$YYYYMMDD$.TXT UPLD ftp://user:passwd@10.245.62.226:21/jz/ /home/myftp/whdl/
0003 010YJJS$YYYY-MM-DD$.TXT DNLD ftp://user:passwd@10.245.62.226:21/pk/js/ /home/myftp/whdl/
作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/1399/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!
评论列表