linux定期上传nginx日志至oss备份并清空脚本

参考链接:【Shell实战】定期清理日志文件的shell脚本

nginx日志上传oss备份并清空脚本编写

oss上传文件shell

该脚本可以将文件上传只oss中

#!/bin/bash

host="oss-cn-beijing-internal.aliyuncs.com"
bucket="bucket-name"
Id="ossId"
Key="ossKey"
# 参数1,PUT:上传,GET:下载
method=$1
# 参数2,上传时为本地源文件路径,下载时为oss源文件路径
source=$2
# 参数3,上传时为OSS目标文件路径,下载时为本地目标文件路径
dest=$3

osshost=$bucket.$host

# 校验method
if test -z "$method"
then
    method=GET
fi

if [ "${method}"x = "get"x ] || [ "${method}"x = "GET"x ]
then
    method=GET
elif [ "${method}"x = "put"x ] || [ "${method}"x = "PUT"x ]
then
    method=PUT
else
    method=GET
fi

#校验上传目标路径
if test -z "$dest"
then
    dest=$source
fi

echo "method:"$method
echo "source:"$source
echo "dest:"$dest

#校验参数是否为空
if test -z "$method" || test -z "$source" || test -z "$dest"
then
    echo $0 put localfile objectname
    echo $0 get objectname localfile
    exit -1
fi

if [ "${method}"x = "PUT"x ]
then
    resource="/${bucket}/${dest}"
    contentType=`file -ib ${source} |awk -F ";" '{print $1}'`
    dateValue="`TZ=GMT env LANG=en_US.UTF-8 date +'%a, %d %b %Y %H:%M:%S GMT'`"
    stringToSign="${method}\n\n${contentType}\n${dateValue}\n${resource}"
    signature=`echo -en $stringToSign | openssl sha1 -hmac ${Key} -binary | base64`
    echo $stringToSign
    echo $signature
    url=http://${osshost}/${dest}
    echo "upload ${source} to ${url}"
    curl -i -q -X PUT -T "${source}" \
      -H "Host: ${osshost}" \
      -H "Date: ${dateValue}" \
      -H "Content-Type: ${contentType}" \
      -H "Authorization: OSS ${Id}:${signature}" \
      ${url}
else
    resource="/${bucket}/${source}"
    contentType=""
    dateValue="`TZ=GMT env LANG=en_US.UTF-8 date +'%a, %d %b %Y %H:%M:%S GMT'`"
    stringToSign="${method}\n\n${contentType}\n${dateValue}\n${resource}"
    signature=`echo -en ${stringToSign} | openssl sha1 -hmac ${Key} -binary | base64`
    url=http://${osshost}/${source}
    echo "download ${url} to ${dest}"
    curl --create-dirs \
      -H "Host: ${osshost}" \
      -H "Date: ${dateValue}" \
      -H "Content-Type: ${contentType}" \
      -H "Authorization: OSS ${Id}:${signature}" \
      ${url} -o ${dest}
fi

判断是否需要上传shell

该脚本可以根据当前磁盘使用量,和某个日志文件的大小进行判断是否要执行上传,这里使用当文件大于5G是进行上传

#!/bin/bash
 
# 所属系统名称
systemName=icetea
# 应用名称
appName=nginx;
# 日志文件路径
filePath="/usr/local/nginx/logs/access.log"

# 判断是否使用率达到90%,是则返回1,否则返回0
isDiskUsageOK()
{
    # 获取实际使用率,去掉%后的数字
    usagepcent=`df / -h | awk '$6=="/"{sub("%","",$5);print $5}'`
    # 判断是否大于90%
    echo "$usagepcent > 90" | bc
}

# 判断是否够5G,是则返回1,否则返回0
isFileByteNumOK()
{
    fileByteNum=`ls -l $filePath | awk '{print $5}'`
    echo "$fileByteNum >= 5368709120" | bc
}

isOK=`isFileByteNumOK`

if [ $isOK = "1" ]
then
	# 执行上传oss
	sh /root/oss_put.sh PUT $filePath ${systemName}/${appName}/`date +%Y-%m-%d`-access.log;
	# 清空日志
	echo "" > $filePath
fi

配置linux定时任务

配置为每6个小时执行一次

# 编写crontab
crontab -e
# 在vim中编写每6个小时执行一次脚本进行判断
0 */6 * * * /bin/sh /root/run_oss_put.sh

#备注 -l可以查看已配置的定时任务
crontab -l
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值