Skip to content

Instantly share code, notes, and snippets.

@chen206
Last active December 5, 2023 18:10
Show Gist options
  • Save chen206/9ec4d898f51e7e15e1e12443aad5d477 to your computer and use it in GitHub Desktop.
Save chen206/9ec4d898f51e7e15e1e12443aad5d477 to your computer and use it in GitHub Desktop.
Logrotate and upload Nginx logfile to S3

安装awscli

sudo apt-get install -y awscli

测试

sudo logrotate -d -f /etc/logrotate.d/nginx

触发logrotate

sudo logrotate -f /etc/logrotate.d/nginx

# sudo vi /etc/logrotate.d/nginx
/var/log/nginx/logs/*.log {
su root adm
daily
missingok
rotate 10
compress
delaycompress
notifempty
sharedscripts
postrotate
[ ! -f /var/log/nginx/logs/nginx.pid ] || kill -USR1 `cat /var/log/nginx/logs/nginx.pid`
BUCKET=your_s3_bucket_name
PUBLIC_IP=`curl --silent http://169.254.169.254/latest/meta-data/public-ipv4`
gzip -c /var/log/nginx/logs/access.log.1 > /tmp/access.log.gz
aws s3 cp /tmp/access.log.gz s3://$BUCKET/access_`date +%Y%m%dT%H%M%SZ`_$PUBLIC_IP.log.gz --region cn-north-1
endscript
}
@leejaycoke
Copy link

Thanks. How about that using $1 variable instead of access.log.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment