windows服务器和linux服务器ftp取数脚本
来源:程序员人生 发布时间:2016-06-27 09:00:27 阅读次数:2800次
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
#!/bin/sh
########################################################################
#
# 此便可手工履行脚本,需要传入履行日期,也可自动履行
#
########################################################################
# 机构号(需要写死本来日期可写死TIMETMP便可,如你需要20150202当天的数据,重跑需删除jksj文件夹下当天的文件)
set -e
ORGCODE=883000
# 自动输入前1天的日期
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}
#310天之前的日期
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"
# 前1天日期格式为:2010-05⑵0
#DAYAGO=$(date -d "$PROCDATE ⑴ days" +%Y-%m-%d)
#AGODATE=$(date -d "$PROCDATE ⑴ days" +%Y%m%d)
#nMonBegDate=$(date -d "$PROCDATE" +%Y%m)"01"
#nMEndLaDate=$(date -d "$nMonBegDate ⑴ 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
linux(ip假设为11.11.0.1)从windows(ip假设为11.11.0.2)那末需要在11.11.0.2上装ftp服务端,在11.11.0.1装ftp客户端,如果取文件夹则用下面的方法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}
#310天之前的日期
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 用户名 密码 自行更改 取前1天日期的文件夹
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上使用手动输入日期循环取数据
#!/bin/sh
########################################################################
# 此便可手工履行脚本,需要传入履行日期,也可自动履行
#
########################################################################
# 机构号(需要写死本来日期可写死TIMETMP便可,如你需要20150202当天的数据,重跑需删除jksj文件夹下当天的文件)
set -e
ORGCODE=883000
# 自动输入前1天的日期
#TIMETMP=$(date -d"yesterday" +%Y%m%d)
#liulei 20160531 add begin
read var
read endvar
datebeg=$var
dateend=$endvar
beg_s=`date -d "$datebeg" +%s`
end_s=`date -d "$dateend" +%s`
while [ "$beg_s" -le "$end_s" ]
do
TIMETMP=$beg_s
#重跑需删除当天文件数据
cd /home/pasdb/doload/addpasdata/jksj/
rm -rf $TIMETMP
#系统当前日期
SYSTIME=$(date '+%Y-%m-%d %H:%M:%S')
NOWDATE=${SYSTIME:0:10}
NOWTIME=${SYSTIME:11:8}
#10天之前的日期
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"
# 前1天日期格式为:2010-05⑵0
#DAYAGO=$(date -d "$PROCDATE ⑴ days" +%Y-%m-%d)
#AGODATE=$(date -d "$PROCDATE ⑴ days" +%Y%m%d)
#nMonBegDate=$(date -d "$PROCDATE" +%Y%m)"01"
#nMEndLaDate=$(date -d "$nMonBegDate ⑴ 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"/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
#mkdir log"/CORE/"$logYear
#mkdir log"/CORE/"$logYear"/"$logMonth
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"
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
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
WriteLog "copy required file to jksj success"
#验证文件是不是齐全
cd /home/pasdb/doload/addpasdata/jksj"/"$INDATE
if [ 1 != $(ls -A | wc -l) ]
then
WriteLog "today jksj file incomplete data"
cd /home/pasdb/doload/addpasdata/jksj
rm -rf $INDATE
else
touch ok.dat
WriteLog "today jksj file complete data and create ok.dat file"
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
如果是windwos上循环取数据就使用,目前只能想到日期范围在1个月之内的,由于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
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠