Linux使用Shell指令碼實現FTP自動下載檔案,並記錄下載檔案日誌
阿新 • • 發佈:2019-08-06
Linux使用Shell指令碼實現FTP自動下載檔案,並記錄下載檔案日誌
最近初學shell程式設計,記一下自己寫的第一個指令碼。有問題可以找我一起討論,交流下。
- Email: [email protected]
首先這個shell指令碼,必須使用命令列vim編輯器建立這個shell指令碼,不然ftp連線那裡會出現錯誤。
[root@localhost /]# vim file_from_ftp.sh
#!/bin/bash
usage(){
echo "############################################################################################################"
echo -e " Usage: $0 [OPTIONS]"
echo -e " xxxx-xx-xx: 檔案後加入日期引數 xxxx-xx-xx,例如:2019-08-01會下載當天的FTP檔案。 ."
echo -e " all: 檔案後加入*號,會下在所有的FTP檔案。預設會覆蓋所有檔案"
echo "############################################################################################################"
exit 1
}
HOST=192.168.0.10 #遠端FTP伺服器地址
PORT=21
USER=xxx #遠端FTP伺服器使用者名稱
PASSWD=xxx123 #遠端FTP伺服器密碼
REMOTE_DIR=/ #遠端FTP伺服器目錄
LOCAL_DIR=/soft/test1 #對應的本地伺服器目錄
LOCAL_DIR_LOG=$LOCAL_DIR/logs #對應的本地伺服器目錄
#獲取前一天的時間
date1=`date -d last-day +%Y%m%d`
date2=`date -d last-day +%Y-%m-%d`
#date1=`date +%Y%m%d --date="-7 day"`
#date2=`date +%Y-%m-%d --date="-7 day"`
shlog(){
local line_no msg
line_no=$1
msg=$2
echo "[file_from_ftp.sh][$line_no]["`date "+%Y-%m-%d %H:%M:%S"`"] $msg ">> ${LOCAL_DIR_LOG}/file_from_ftp${date2}.log
}
shlogAllFile(){
local line_no msg
line_no=$1
msg=$2
echo "[file_all_from_ftp.sh][$line_no]["`date "+%Y-%m-%d %H:%M:%S"`"] $msg ">> ${LOCAL_DIR_LOG}/file_all_from_ftp${date2}.log
}
##下載所有全部檔案校驗##
function checkFilePros()
{
echo "%%%%%%%%%%%%%%下載所有全部檔案##############%$%%%%%%%%%%%"
shlogAllFile $LINENO "%%%%%%%%%%%%%%%%%FTP伺服器下載所有全部檔案%%%%%%%%%%%%%%%%%%%%%%%%"
let "fileNumFromFtp = 0"
let "fileNumFromFtpError = 0"
for file in $LOCAL_DIR/$1
do
if test -f $file
then
fileName=${file##*/}
FTPSIZE=`cat $LOCAL_DIR_LOG/tmp_all_from_ftp${date2}.log |sed -n '/'${fileName}'/p' |awk '{print $5}'`
LOCALSIZE=`ls -l $LOCAL_DIR/${fileName} |awk '{print $5}'`
echo "%%%%%%%%%%%%%%% ${file##*/} FTPSIZE:==== $FTPSIZE LOCALSIZE:==== $LOCALSIZE"
if [ "$FTPSIZE" != "" ] && [ "$FTPSIZE" == "$LOCALSIZE" ]; then
let "fileNumFromFtp+=1"
shlogAllFile $LINENO "FTP伺服器 ${fileName}檔案下載到本地 ${file}成功"
else
let "fileNumFromFtpError+=1"
shlogAllFile $LINENO "FTP伺服器 ${fileName}檔案下載到本地 ${file}失敗"
fi
fi
done
echo "FTP下載的檔案成功success個數: $fileNumFromFtp 個數"
echo "FTP下載的檔案失敗error個數: $fileNumFromFtpError 個數"
shlogAllFile $LINENO "FTP下載的檔案成功success個數: $fileNumFromFtp 個數"
shlogAllFile $LINENO "FTP下載的檔案失敗error個數: $fileNumFromFtpError 個數"
rm -rf ${LOCAL_DIR_LOG}/tmp_all_from_ftp${date2}.log
}
##下載某一天的檔案校驗##
function checkFilePros1()
{
let "fileNumFromFtp = 0"
let "fileNumFromFtpError = 0"
for file in $LOCAL_DIR/$1
do
if test -f $file
then
#echo "%%%%%%%%%%%%%%% ${file##*/}"
fileName=${file##*/}
FTPSIZE=`cat $LOCAL_DIR_LOG/tmp_from_ftp${date1}.log |sed -n '/'${fileName}'/p' |awk '{print $5}'`
LOCALSIZE=`ls -l $LOCAL_DIR/${fileName} |awk '{print $5}'`
echo "%%%%%%%%%%%%%%% ${fileName} FTPSIZE:==== $FTPSIZE LOCALSIZE:==== $LOCALSIZE"
if [ "$FTPSIZE" != "" ] && [ "$FTPSIZE" == "$LOCALSIZE" ]; then
let "fileNumFromFtp+=1"
shlog $LINENO "FTP伺服器 ${fileName}檔案下載到本地 ${file}成功"
else
let "fileNumFromFtpError+=1"
shlog $LINENO "FTP伺服器 ${fileName}檔案下載到本地 ${file}失敗"
fi
fi
done
for file in $LOCAL_DIR/$2
do
if test -f $file
then
#echo "%%%%%%%*_$date2.csv%%%%%%%% ${file##*/}"
fileName=${file##*/}
FTPSIZE=`cat $LOCAL_DIR_LOG/tmp_from_ftp${date2}.log |sed -n '/'${fileName}'/p' |awk '{print $5}'`
LOCALSIZE=`ls -l $LOCAL_DIR/${fileName} |awk '{print $5}'`
echo "%%%%%%%%%%%%%%% ${fileName} FTPSIZE:==== $FTPSIZE LOCALSIZE:==== $LOCALSIZE"
if [ "$FTPSIZE" != "" ] && [ "$FTPSIZE" == "$LOCALSIZE" ]; then
let "fileNumFromFtp+=1"
shlog $LINENO "伺服器 ${fileName}檔案下載到本地 ${file}成功"
else
let "fileNumFromFtpError+=1"
shlog $LINENO "FTP伺服器 ${fileName}檔案下載到本地 ${file}失敗"
fi
fi
done
echo "FTP下載的檔案成功success個數: $fileNumFromFtp 個數"
echo "FTP下載的檔案失敗error個數: $fileNumFromFtpError 個數"
shlog $LINENO "FTP下載的檔案成功success個數: $fileNumFromFtp 個數"
shlog $LINENO "FTP下載的檔案失敗error個數: $fileNumFromFtpError 個數"
rm -rf ${LOCAL_DIR_LOG}/tmp_from_ftp${date1}.log
rm -rf ${LOCAL_DIR_LOG}/tmp_from_ftp${date2}.log
}
function downloadFtpFile()
{
echo "下載FTP存放的本地路徑: $LOCAL_DIR"
echo "ftp mutl file start ......"
ftp -v -n $HOST<<EOF
user $USER $PASSWD
binary
cd $REMOTE_DIR
lcd $LOCAL_DIR
prompt
mget $1
ls $REMOTE_DIR$1 ${LOCAL_DIR_LOG}/tmp_all_from_ftp${date2}.log
bye
EOF
checkFilePros $1
#列印環境變數資訊
echo "download successfully from ftp all file ending......"
}
function downloadFtpFile1()
{
echo "下載FTP存放的本地路徑: $LOCAL_DIR"
echo "ftp mutl file start ......$1 "
echo "ftp mutl file start ......$2 "
matchStr1=$1
matchStr2=$2
ftp -v -n $HOST<<EOF
user $USER $PASSWD
binary
cd $REMOTE_DIR
lcd $LOCAL_DIR
prompt
mget $matchStr1
mget $matchStr2
ls $REMOTE_DIR$1 ${LOCAL_DIR_LOG}/tmp_from_ftp${date1}.log
ls $REMOTE_DIR$2 ${LOCAL_DIR_LOG}/tmp_from_ftp${date2}.log
bye
EOF
checkFilePros1 ${matchStr1} ${matchStr2}
#列印環境變數資訊
echo "download successfully from ftp all file ending......"
}
function createFileDir() {
shlog $LINENO "******************檢查待接收下載檔案根目錄是否存在,不存在則建立該目錄************************"
if [ ! -d ${LOCAL_DIR_LOG} ]; then
mkdir -p ${LOCAL_DIR_LOG}
fi
}
echo "Input Param Is [$1]"
if [ ! -n "$1" ] ;then
shlog $LINENO ""
shlog $LINENO "*****************************${date2}資料檔案下載開始****************************************"
createFileDir
echo "###########################################"
echo " *_${date1}.csv *_${date2}.csv"
echo "###########################################"
param1=*_${date1}.csv
param2=*_${date2}.csv
downloadFtpFile1 ${param1} ${param2}
shlog $LINENO "*****************************${date2}資料檔案下載結束****************************************"
elif [ -n "$1" ];then
#echo "這裡是指定輸入的日期,例如選擇要下載一天的檔案"
if echo $1 | grep -Eq "[0-9]{4}-[0-9]{2}-[0-9]{2}" && date -d $1 +%Y%m%d > /dev/null 2>&1
then
echo "輸入的日期格式正確: $1";
param1=$1
echo "######param1#param1###### $param1"
param2=${param1//-/}
echo "######param2#param2###### $param2"
echo "*_${param2}.csv *_${param1}.csv"
shlog $LINENO ""
shlog $LINENO "*****************************${param1}資料檔案下載開始****************************************"
createFileDir
downloadFtpFile1 *_${param2}.csv *_${param1}.csv
shlog $LINENO "*****************************${param1}資料檔案下載結束****************************************"
elif [ $1 == 'all' ];then
echo "all 下載所有全部檔案##############%$%%%%%%........................."
downloadFtpFile *.csv
else
usage
fi
else
echo "這裡沒有用,永遠不會進來的"
fi
exit 0