#!/bin/bash # 模块名字 servernames=("zxwy-api" "zxwy-task") # 日志上传bucket名称 bucketname=zxwy-dev-backup-bucket # 日志所在机器IP ip=172.17.161.164 # 开始上传 for servername in ${servernames[@]} do location=/home/logs/dev/${servername} # 上传当前日志 # 前一小时日期,格式yyyy-MM-dd time=`date -d "1 hour ago" +"%Y-%m-%d-%H"` # 文件名字 filename=${location}/${servername}.log.${time} echo '上传开始: '${filename} # 判断文件是否存在 if [ ! -f "${filename}" ]; then echo '本地文件未找到: '${filename} exit 0 fi # 检查oss是否已存在 exists=`/home/apps/ossutil/ossutil -c /home/apps/ossutil/config ls oss://${bucketname}/log/${servername}/${ip}/${servername}.log.${time}.tar.gz` if [[ ${exists} =~ 'Object Number is: 1' ]]; then echo '云端文件已存在: '${filename} continue fi # 压缩 cd ${location} tar -czvf ${servername}.log.${time}.tar.gz ${servername}.log.${time} # 上传到oss /home/apps/ossutil/ossutil -c /home/apps/ossutil/config cp ${filename}.tar.gz oss://${bucketname}/log/${servername}/${ip}/${servername}.log.${time}.tar.gz # 删除 rm -f ${filename}.tar.gz echo '上传完成: '${filename} done exit 0