引言:
windows ftp 循环获取文件夹的.bat脚本 linux ftp循环获取文件夹的shell脚本(即获取输入的两个时间段之间下发的文件)
测试环境&&工具:
linux(ip假如为11.11.0.1)从linux(ip假如为11.11.0.2)那么需要在11.11.0.2上装vsftp也就是ftp服务端,在11.11.0.1装ftp客户端。
上述安装软件的下载地址http://download.csdn.net/detail/u012209894/9537732
一、linux下获取文件夹
PS:(文件夹用下面的方法wget获取,取文本文件用第四点的方法)
#!/bin/sh
########################################################################
# 脚本说明:绩效考核系统日终脚本文件
# 此即可手工执行脚本,需要传入执行日期,也可自动执行
#
########################################################################
#(需要写死原先日期可写死TIMETMP即可,如你需要20150202当天的数据,重跑需删除jksj文件夹下当天的文件)
set -e
#自动获取昨天的日期
TIMETMP=$(date -d"yesterday" +%Y%m%d)
#手工输入昨天日期
#read var
#TIMETMP=$var
#当天重跑获取昨天文件 需删除昨天文件数据
if [ ! -e /xxpas/doload/jksj/ ]
then
mkdir -p /xxpas/doload/jksj/
else
cd /xxpas/doload/jksj/
rm -rf $TIMETMP
fi
#系统当前日期
SYSTIME=$(date '+%Y-%m-%d %H:%M:%S')
NOWDATE=${SYSTIME:0:10}
#NOWTIME=${SYSTIME:11:8}
#三十天以前的日期
tendays=$(date -d"30 days ago" +%Y%m%d)
# 如果格式为:2010-07-07
if test "${#TIMETMP}" = 10
then
YYY=${TIMETMP:0:4}
MMM=${TIMETMP:5:2}
DDD=${TIMETMP:8:9}
INDATE=$YYY$MMM$DDD
# 如果输入日期格式为:20100707
else
INDATE=$TIMETMP
fi
# 修改日期格式为:2010-07-07
YYY=${INDATE:0:4}
MMM=${INDATE:4:2}
DDD=${INDATE:6:7}
PROCDATE=$YYY"-"$MMM"-"$DDD
YEARBEGIN=$YYY"-01-01"
if [ "$PROCDATE" \< "$NOWDATE" ]
then
#脚本存放目录
DATAPATH=/xxpas/doload
#初始化标记值为0
LOADFLAG=0
# 创建工作目录
createWorkSpace()
{
if [ ! -e $DATAPATH"/jksj" ]
then
mkdir jksj
# log 存放日志文件
mkdir log
else
cd $DATAPATH"/jksj"
if [ ! -e log ]
then
mkdir log
fi
fi
DATAPATH=$DATAPATH"/jksj"
}
#创建程序实现功能所需的目录空间
createWorkSpace $DATAPATH
LOGFILE=$DATAPATH"/log/xxjk_"$INDATE".log"
#写日志
WriteLog()
{
if [ ! -e $LOGFILE ]
then
touch $LOGFILE
fi
if [ "$1" != "" ]
then
echo $(date '+%Y-%m-%d %H:%M:%S')"=======>"$1 | tee -a $LOGFILE
fi
}
# ftp download data ip 用户名 密码 自行更改 取前一天日期的文件夹
wget -r -nH -P/xxpas/doload/jksj ftp://11.11.0.2"/"$INDATE --ftp-user=admini1 --ftp-password=123
cd $DATAPATH"/"$INDATE
WriteLog "download ftp file is end."
#tar -zxvf $FTPFILE_CORE -C /xxpas/doload/jksj"/"$INDATE
#gunzip -n $FTPFILE_CORE
WriteLog "download ftp file is success and tar file to jksj success"
#删除10天前文件
cd /xxpas/doload/jksj/log
rm -f "xxjk_"$tendays".log"
WriteLog "remove ten days before log file successfully"
cd /xxpas/doload/jksj
rm -rf $tendays
WriteLog "remove ten days before sjff file successfully"
LOADFLAG=1
else
WriteLog "download ftp file is fail,please check the ftp file is exist?"
LOADFLAG=0
fi
二、linux上FTP使用手动输入日期循环取数据
(即输入开始日期和结束日期,从而取该时间段下发的所有文件,用于追漏发或者需要更正的数据)
#!/bin/sh
########################################################################
# 此即可手工执行脚本,需要传入执行日期,也可自动执行
#存放脚本目录:/home/pasdb/doload (没有就新建)
########################################################################
set -e
ORGCODE=883000
# 自动输入前一天的日期
#TIMETMP=$(date -d"yesterday" +%Y%m%d)
echo -n "please enter the datebeg:"
read var
echo -n "please enter the dateend and waiting:"
read endvar
datebeg=$var
dateend=$endvar
#是你电脑左上角1前面的点 不是单引号
beg_s=`date -d "$datebeg" +%s`
end_s=`date -d "$dateend" +%s`
while [ "$beg_s" -le "$end_s" ]
do
TIMETMP=$(date -d "@$beg_s" +%Y%m%d)
#liulei 20160531 add end
#重跑需删除当天文件数据
if [ ! -e /home/pasdb/doload/pasdata/jksj/ ]
then
mkdir -p /home/pasdb/doload/pasdata/jksj/
else
cd /home/pasdb/doload/pasdata/jksj/
rm -rf $TIMETMP
fi
#系统当前日期
SYSTIME=$(date '+%Y-%m-%d %H:%M:%S')
NOWDATE=${SYSTIME:0:10}
NOWTIME=${SYSTIME:11:8}
#十天以前的日期
tendays=$(date -d"30 days ago" +%Y%m%d)
# 如果格式为:2010-07-07
if test "${#TIMETMP}" = 10
then
YYY=${TIMETMP:0:4}
MMM=${TIMETMP:5:2}
DDD=${TIMETMP:8:9}
INDATE=$YYY$MMM$DDD
# 如果输入日期格式为:20100707
else
INDATE=$TIMETMP
fi
# 修改日期格式为:2010-07-07
YYY=${INDATE:0:4}
MMM=${INDATE:4:2}
DDD=${INDATE:6:7}
PROCDATE=$YYY"-"$MMM"-"$DDD
if [ "$PROCDATE" \< "$NOWDATE" ]
then
#脚本存放目录
DATAPATH=/home/pasdb/doload
#FTP信息:IP地址:FTPIP;用户名:FTPNAME;密码:FTPPWD;FTP目录:FTPDATAPATH;FTP保存文件名:FTPFILE
FTPIP="11.11.0.2"
FTPNAME="test"
FTPPWD="test"
FTPDATAPATH="/home/sjff/sdata/S-999000/CORE/ADD/"$INDATE
FTPFILE_CORE="CORE_"$ORGCODE"_"$INDATE"_ADD.tar.Z"
#初始化标记值为0
LOADFLAG=0
# 创建工作目录
createWorkSpace()
{
if [ ! -e $DATAPATH"/addpasdata" ]
then
mkdir addpasdata
cd addpasdata
# dbresult 存放执行结果
mkdir dbresult
# zipdata 存放ftp下载的zip数据文件
mkdir zipdata
mkdir zipdata/sjff
mkdir zipdata/sjff"/"$INDATE
# unzipdata 存放执行当天M包解压临时存放目录
mkdir unzipdata
# log 存放日志文件
mkdir log
#CORE 存放CORE日志文件
mkdir CORE
mkdir jksj
mkdir jksj"/"$INDATE
else
cd $DATAPATH"/addpasdata"
if [ ! -e dbresult ]
then
mkdir dbresult
fi
if [ ! -e zipdata ]
then
mkdir zipdata
fi
if [ ! -e zipdata/sjff ]
then
mkdir zipdata/sjff
fi
if [ ! -e zipdata/sjff/$INDATE ]
then
mkdir zipdata/sjff/$INDATE
fi
if [ ! -e unzipdata ]
then
mkdir unzipdata
fi
if [ ! -e log ]
then
mkdir log
fi
if [ ! -e jksj/$INDATE ]
then
mkdir jksj/$INDATE
fi
fi
DATAPATH=$DATAPATH"/addpasdata"
}
#创建程序实现功能所需的目录空间
createWorkSpace $DATAPATH
LOGFILE=$DATAPATH"/log/CORE_"$INDATE".log"
#写日志
WriteLog()
{
if [ ! -e $LOGFILE ]
then
touch $LOGFILE
fi
if [ "$1" != "" ]
then
echo $(date '+%Y-%m-%d %H:%M:%S')"=======>"$1 | tee -a $LOGFILE
fi
}
# ftp download data
/usr/bin/ftp -n<<!
open $FTPIP
user $FTPNAME $FTPPWD
binary
prompt off
cd $FTPDATAPATH
lcd $DATAPATH"/zipdata/sjff/"$INDATE
mget "$FTPFILE_CORE"
bye
!
cd $DATAPATH"/zipdata/sjff/"$INDATE
WriteLog "download ftp file is end."
if [ ! -f "$FTPFILE_CORE" ]
then
#chmod 777 "$INDATE"
tar -zxvf $FTPFILE_CORE -C /home/pasdb/doload/addpasdata/unzipdata
WriteLog "download ftp file is success and tar file to unzipdata success"
mv "/home/pasdb/doload/addpasdata/unzipdata/CORE_BWFMATMD_"$INDATE"_ADD_883.del" /home/pasdb/doload/addpasdata/jksj"/"$INDATE/BWFMATMD.DEL
cp /home/pasdb/doload/addpasdata/jksj"/"$INDATE/BWFMATMD.DEL /home/pasdb/doload/pasdata/jksj"/"$INDATE/
WriteLog "copy required file to jksj success"
#验证文件是否齐全
cd /home/pasdb/doload/addpasdata/jksj"/"$INDATE
if [ 1 = $(ls -A | wc -l) ]
then
rm -f *.del
WriteLog "today add jksj file complete data"
fi
cd /home/pasdb/doload/addpasdata/unzipdata
rm -f *.del
WriteLog "delete unzipdata file successfully"
cd /home/pasdb/doload/addpasdata/log
rm -f "CORE_"$tendays".log"
WriteLog "remove ten days before log file successfully"
cd /home/pasdb/doload/addpasdata/zipdata/sjff
rm -rf $tendays
WriteLog "remove ten days before sjff file successfully"
LOADFLAG=1
else
WriteLog "download ftp file is fail,please check the ftp file is exist?"
LOADFLAG=0
fi
fi
beg_s=$((beg_s+86400))
done
三、windows下循环获取文件脚本
::windwos上循环取数(思路和第二点的一致)目前只能想到日期范围在一个月以内的,因为windowns上的日期date算超出月外的就比较繁琐了
@echo on
::------------------------------------------------------------------
:: example :call test.bat 20160101 20160110
::------------------------------------------------------------------
setlocal enabledelayedexpansion
set startdate=%1
set enddate=%2
set FTP_SERVER=11.11.0.2
set FTP_USER=admin1
set FTP_PASSWORD=admin1
echo 当前输入日期为1:%startdate%
rem pause
set dy=%startdate:~,4%
set dm=%startdate:~4,2%
set dd=%startdate:~6,2%
echo 当前输入日期为:%dy%%dm%%dd%
set StartTime=%Time:~0,2%:%Time:~3,2%:%Time:~6,2%
rem pause
set PAS_DATE=%dy%%dm%%dd%
:start
D:
set PAS_ROOT=D:\DATA\JKSJ
if not exist %PAS_ROOT% mkdir %PAS_ROOT%
set FTP_CMD_FILE=%PAS_ROOT%\ftpcmd.txt
set PAS_DATA_DIR=%PAS_ROOT%\%PAS_DATE%
if not exist %PAS_DATA_DIR% mkdir %PAS_DATA_DIR%
echo 文件夹地址%PAS_DATA_DIR%
rem 从FTP获取数据
echo open %FTP_SERVER%>%FTP_CMD_FILE%
echo %FTP_USER%>>%FTP_CMD_FILE%
echo %FTP_PASSWORD%>>%FTP_CMD_FILE%
echo ascii>>%FTP_CMD_FILE%
echo literal pasv>>%FTP_CMD_FILE%
echo prompt >>%FTP_CMD_FILE%
echo cd %PAS_DATE%>>%FTP_CMD_FILE%
echo lcd %PAS_DATE%>>%FTP_CMD_FILE%
echo mget *.*>>%FTP_CMD_FILE%
echo close>>%FTP_CMD_FILE%
echo bye>>%FTP_CMD_FILE%
ftp -s:%FTP_CMD_FILE%
c:
echo 解压开始 >>%PAS_DATA_DIR%\GetOK.dat
C:\"Program Files"\WinRAR\winrar.exe e %PAS_DATA_DIR%\*.zip %PAS_DATA_DIR%
echo 解压完成 >>%PAS_DATA_DIR%\GetOK.dat
set EndTime=%Time:~0,2%:%Time:~3,2%:%Time:~6,2%
echo FTP下载数据开始时间:%StartTime%,结束时间:%EndTime%
echo FTP下载数据开始时间:%StartTime%,结束时间:%EndTime% >>%PAS_DATA_DIR%\GetOK.dat
D:
cd %PAS_DATA_DIR%
if exist *.ok (
echo %PAS_DATE%>>%PAS_DATA_DIR%\JKSJOK.dat
)
echo 存在>>%PAS_DATA_DIR%\GetOK.dat
set /a "PAS_DATE+=1"
echo now date:%PAS_DATE%
echo now enddate:%enddate%
rem pause
if "%enddate%" neq "%PAS_DATE%" goto start
exit
四、linux ftp获取文本类文件的shell脚本
#!/bin/sh
########################################################################
#
# 此即可手工执行脚本,需要传入执行日期,也可自动执行
# 当然直接使用scp命令来取更简洁
########################################################################
# 机构号(需要写死原先日期可写死TIMETMP即可,如你需要20150202当天的数据,重跑需删除jksj文件夹下当天的文件)
set -e
ORGCODE=883000
# 自动输入前一天的日期
TIMETMP=$(date -d"yesterday" +%Y%m%d)
#read var
#TIMETMP=$var
#重跑需删除当天文件数据
cd /home/pasdb/doload/pasdata/jksj/
rm -rf $TIMETMP
#系统当前日期
SYSTIME=$(date '+%Y-%m-%d %H:%M:%S')
NOWDATE=${SYSTIME:0:10}
NOWTIME=${SYSTIME:11:8}
#三十天以前的日期
tendays=$(date -d"30 days ago" +%Y%m%d)
# 如果格式为:2010-07-07
if test "${#TIMETMP}" = 10
then
YYY=${TIMETMP:0:4}
MMM=${TIMETMP:5:2}
DDD=${TIMETMP:8:9}
INDATE=$YYY$MMM$DDD
# 如果输入日期格式为:20100707
else
INDATE=$TIMETMP
fi
# 修改日期格式为:2010-07-07
YYY=${INDATE:0:4}
MMM=${INDATE:4:2}
DDD=${INDATE:6:7}
PROCDATE=$YYY"-"$MMM"-"$DDD
YEARBEGIN=$YYY"-01-01"
# 前一天日期格式为:2010-05-20
#DAYAGO=$(date -d "$PROCDATE -1 days" +%Y-%m-%d)
#AGODATE=$(date -d "$PROCDATE -1 days" +%Y%m%d)
#nMonBegDate=$(date -d "$PROCDATE" +%Y%m)"01"
#nMEndLaDate=$(date -d "$nMonBegDate -1 days +1 months" +%Y%m%d)
logYear=$YYY
logMonth=$MMM
if [ "$PROCDATE" \< "$NOWDATE" ]
then
#脚本存放目录
DATAPATH=/home/pasdb/doload
#LOADPATH=/home/pasdb/doload/pas_load_CORE.sql
PROCPATH=/home/pasdb/doload/pas_pro_file.sh
#FTP信息:IP地址:FTPIP;用户名:FTPNAME;密码:FTPPWD;FTP目录:FTPDATAPATH;FTP保存文件名:FTPFILE
FTPIP="11.11.0.2"
FTPNAME="sjff"
FTPPWD="sjff"
FTPDATAPATH="/home/sjff/CORE/ADD/"$INDATE
FTPFILE_CORE="CORE_"$ORGCODE"_"$INDATE"_ADD.tar.Z"
#初始化标记值为0
LOADFLAG=0
# 创建工作目录
createWorkSpace()
{
if [ ! -e $DATAPATH"/pasdata" ]
then
mkdir pasdata
cd pasdata
# dbresult 存放执行结果
mkdir dbresult
# zipdata 存放ftp下载的zip数据文件
mkdir zipdata
mkdir zipdata/sjff
mkdir zipdata/sjff"/"$INDATE
# unzipdata 存放执行当天M包解压临时存放目录
mkdir unzipdata
# log 存放日志文件
mkdir log
#CORE 存放CORE日志文件
mkdir CORE
mkdir jksj
mkdir jksj"/"$INDATE
#mkdir log"/CORE/"$logYear
#mkdir log"/CORE/"$logYear"/"$logMonth
else
cd $DATAPATH"/pasdata"
if [ ! -e dbresult ]
then
mkdir dbresult
fi
if [ ! -e zipdata ]
then
mkdir zipdata
fi
if [ ! -e zipdata/sjff ]
then
mkdir zipdata/sjff
fi
if [ ! -e zipdata/sjff/$INDATE ]
then
mkdir zipdata/sjff/$INDATE
fi
if [ ! -e unzipdata ]
then
mkdir unzipdata
fi
if [ ! -e log ]
then
mkdir log
fi
if [ ! -e jksj/$INDATE ]
then
mkdir jksj/$INDATE
fi
fi
DATAPATH=$DATAPATH"/pasdata"
}
#创建程序实现功能所需的目录空间
createWorkSpace $DATAPATH
LOGFILE=$DATAPATH"/log/CORE_"$INDATE".log"
DBRESULT=$DATAPATH"/dbresult"
DAILY_BATCH_LAST_DATE_CORE=$DBRESULT"/daily_batch_last_date_core"
DAILY_BATCH_BEGIN_STATE_CORE=$DBRESULT"/daily_batch_begin_state_core"
SQL_RESULT_CORE_LOAD=$DBRESULT"/sql_result_core_load"
if [ ! -e $DAILY_BATCH_LAST_DATE_CORE ]
then
touch $DAILY_BATCH_LAST_DATE_CORE
fi
if [ ! -e $DAILY_BATCH_BEGIN_STATE_CORE ]
then
touch $DAILY_BATCH_BEGIN_STATE_CORE
fi
if [ ! -e $SQL_RESULT_CORE_LOAD ]
then
touch $SQL_RESULT_CORE_LOAD
fi
#写日志
WriteLog()
{
if [ ! -e $LOGFILE ]
then
touch $LOGFILE
fi
if [ "$1" != "" ]
then
echo $(date '+%Y-%m-%d %H:%M:%S')"=======>"$1 | tee -a $LOGFILE
fi
}
# ftp download data
/usr/bin/ftp -n<<!
open $FTPIP
user $FTPNAME $FTPPWD
binary
prompt off
cd $FTPDATAPATH
lcd $DATAPATH"/zipdata/sjff/"$INDATE
mget "$FTPFILE_CORE"
close
bye
!
cd $DATAPATH"/zipdata/sjff/"$INDATE
WriteLog "download ftp file is end."
if [ -f "$FTPFILE_TPDM" ]
then
#FILESIZE=$(ls -l $FTPFILE_CORE|awk '{printf $5}')
#FILESIZE=$(awk BEGIN'{printf "%.3f", '$FILESIZE'/(1024*1024)}')
#cd $DATAPATH"/pasdata/zipdata/sjff/"$INDATE
#chmod 777 "$INDATE"
tar -zxvf $FTPFILE_CORE -C /home/pasdb/doload/pasdata/unzipdata
WriteLog "download ftp file is success and tar file to unzipdata success"
mv "/home/pasdb/doload/pasdata/unzipdata/CORE_BDFMHQAA_"$INDATE"_ADD_883.del" /home/pasdb/doload/pasdata/jksj"/"$INDATE/BDFMHQAA.DEL
WriteLog "copy required file to jksj success"
#验证文件是否齐全
cd /home/pasdb/doload/pasdata/jksj"/"$INDATE
if [ 1 != $(ls -A | wc -l) ]
then
WriteLog "today jksj file incomplete data"
cd /home/pasdb/doload/pasdata/jksj
rm -rf $INDATE
else
touch ok.dat
WriteLog "today jksj file complete data and create ok.dat file"
fi
cd /home/pasdb/doload/pasdata/unzipdata
rm -f *.del
WriteLog "delete unzipdata file successfully"
cd /home/pasdb/doload/pasdata/log
rm -f "CORE_"$tendays".log"
WriteLog "remove ten days before log file successfully"
cd /home/pasdb/doload/pasdata/zipdata/sjff
rm -rf $tendays
WriteLog "remove ten days before sjff file successfully"
LOADFLAG=1
else
WriteLog "download ftp file is fail,please check the ftp file is exist?"
LOADFLAG=0
fi
fi