海南(ftp远程主机下载文件)
#!/bin/bash
# 配置文件路径
CONFIG_FILE="/data3/ftp_config.conf"
# 需要修改的section id
SECTION_ID="server2"
# 接口名称
LOAD_NAME="846_Schedule"
# 下载文件名称,使用通配符
fileName="${LOAD_NAME}_{MTCS_DATA_DAY}.txt"
# 本地目录
SRCDIR="/data3/ftp/{MTCS_DATA_DAY}/${LOAD_NAME}"
# 远程目录
DESDIR="/data1/IPTV/DOWN/"
FTP_USER='ip'
FTP_PASS='FFcsbapd$^416'
FTP_HOST='135.125.162.86'
FTP_PORT='22'
# 使用awk提取特定section的配置信息,并去除可能的前后空格
# FTP_USER=$(awk -F'=' -v RS='\\n' "/\[$SECTION_ID\]/{found=1; next} found && /FTP_USER/{print \$2; found=0}" $CONFIG_FILE | xargs)
# FTP_PASS=$(awk -F'=' -v RS='\\n' "/\[$SECTION_ID\]/{found=1; next} found && /FTP_PASS/{print \$2; found=0}" $CONFIG_FILE | xargs)
# FTP_HOST=$(awk -F'=' -v RS='\\n' "/\[$SECTION_ID\]/{found=1; next} found && /FTP_HOST/{print \$2; found=0}" $CONFIG_FILE | xargs)
# FTP_PORT=$(awk -F'=' -v RS='\\n' "/\[$SECTION_ID\]/{found=1; next} found && /FTP_PORT/{print \$2; found=0}" $CONFIG_FILE | xargs)
echo "【日志】FTP_USER 的值为: ${FTP_USER}"
echo "【日志】FTP_PASS 的值为: $FTP_PASS"
echo "【日志】FTP_HOST 的值为: $FTP_HOST"
echo "【日志】FTP_PORT 的值为: $FTP_PORT"
echo $fileName
# 检查是否成功获取所有配置信息
if [ -z "$FTP_USER" ] || [ -z "$FTP_PASS" ] || [ -z "$FTP_HOST" ] || [ -z "$FTP_PORT" ]; then
echo "FTP configuration for server ID '$SECTION_ID' is incomplete."
exit 1
fi
# 创建本地目录
if [ ! -d ${SRCDIR} ]; then
mkdir -p ${SRCDIR} && echo "${SRCDIR} directory created successfully"
fi
# 清理历史文件,以妨重复下载
echo "Deleting local .gz files..."
find "$SRCDIR" -name "$fileName" -type f -delete
echo "Starting download..."
# 使用lftp进行FTP下载
if lftp -u "$FTP_USER","$FTP_PASS" sftp://"$FTP_HOST":"$FTP_PORT" << EOF
lcd $SRCDIR
cd $DESDIR
mget $fileName
bye
EOF
then
echo "Download completed successfully."
else
echo "Download failed."
exit 1
fi
# 解压文件
echo "Decompressing files..."
for gzFile in "$SRCDIR"/$fileName; do
mv "${gzFile%}" "${SRCDIR}/${LOAD_NAME}_{MTCS_DATA_DAY}.txt"
echo "mv ${gzFile%} ${SRCDIR}/${LOAD_NAME}_{MTCS_DATA_DAY}.txt "
done
echo "All operations completed."
保存一下