首页 > 代码库 > shell小脚本实现上传目录中的所有文件到服务器

shell小脚本实现上传目录中的所有文件到服务器

前段时间,公司新开发了一套用于国外的bss系统,其中有一个功能是,让客户上传证件、以及本人手持证件的照片,系统这边进行人工审核,但是由于中国到国外的网络很慢,审核工作在国外,而我们上传端在中国,所以同步上传的时候由于网络原因失败率极高,因此必须将这些文件变成异步上传,开发的工作任务压力太大,这个功能不大,但是改起来又会很麻烦,所以,无奈,就用运维的方法处理这个问题了,废话多了,请看脚本【菜鸟一枚,如有不妥还请不吝赐教,十分感激】:



#!/bin/sh
###Warning:Not Modified
###Author:haha@company.net
###Date:2016-08-31
export HOSTNAME=host_name
export HOME=/home/testuser
export SHELL=/bin/bash
export TERM=vt100
export HISTSIZE=1000
export QTDIR=/usr/lib64/qt-3.3
export QTINC=/usr/lib64/qt-3.3/include
export JRE_HOME=${HOME}/java/jre
export USER=testuser
export LD_LIBRARY_PATH=:/usr/local/apr/lib
export MAIL=/var/spool/mail/testuser
export JAVA_HOME=${HOME}/java
export LANG=en_US.UTF-8
HOST=ftp.host.or.ip
USER=ftpuser.user
PASS=ftpuser.password
REMOTE_PATH=/home/ftpuser.user/BUSI_USER_REG_IMG_UPLOAD
REMOTE_PATH_HIS=/home/ftpuser.user/BUSI_USER_REG_IMG_UPLOAD_HIS
REMOTE_PATH_TMP=/home/ftpuser.user/BUSI_USER_REG_IMG_UPLOAD_TMP
LOCAL_PATH=${HOME}/important_image
LOCAL_PATH_HIS=${HOME}/important_image_his
LOCAL_PATH_TMP=${HOME}/important_image_tmp
count=0
#------------------------------------------------------------------------------------------------------------------------------------------------------------
#------------------------------------------------------------------------------------------------------------------------------------------------------------
#--------------------cut line up is config down is program!-------------------------------------------------------------------------------------------------
#------------------------------------------------------------------------------------------------------------------------------------------------------------
#------------------------------------------------------------------------------------------------------------------------------------------------------------
cd ${HOME}
alias cp="cp"
START_TIME=`date "+%s"`
echo "start-----------------start-----------------start-----------------start-----------------start-----------------`date`"
mkdir -p ${LOCAL_PATH}
mkdir -p ${LOCAL_PATH_HIS}
mkdir -p ${LOCAL_PATH_TMP}
for file in `find ${LOCAL_PATH} -maxdepth 1  -type f`
do
filename=`basename ${file}`
all_file_name="${all_file_name} ${filename}"
all_file="${all_file} ${file}"
echo "${file}......................................................................[yes]"
count=`expr $count + 1`
done
#判定`find ${LOCAL_PATH} -maxdepth 1  -type f` 命令是否搜索到符合条件的文件
if [[ $count = "0" ]]; then
echo "======================================NO file find in folder ${LOCAL_PATH}!======================================"
else
TIME_TAG=`date "+%Y%m%d%H%M%S%N"`
mkdir -p ${LOCAL_PATH_TMP}/${TIME_TAG}
mv ${all_file} ${LOCAL_PATH_TMP}/${TIME_TAG}
cd ${LOCAL_PATH_TMP}/${TIME_TAG}
ftp -v -n  ${HOST} <<END
user ${USER} ${PASS}
cd ${REMOTE_PATH}
bin
prompt off
mput ${all_file_name}
quit
END
cp -rf ${LOCAL_PATH_TMP}/${TIME_TAG}/* ${LOCAL_PATH_HIS}
rm -rf ${LOCAL_PATH_TMP}/${TIME_TAG}
END_TIME=`date "+%s"`
SECONDS=`expr $END_TIME - $START_TIME`
echo "++++++++++++++++++++++++++++++++++++++++++++ftp_host:${HOST}   ftp_path:${REMOTE_PATH}+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo "#####################${SECONDS}s put to ftp ${count} files!!!!###############################################"
fi
echo "stop------------------stop------------------stop------------------stop------------------stop-----------------`date`"
exit

shell小脚本实现上传目录中的所有文件到服务器