Forked from mustafaturan/s3-nginx-log-rotate.sh
Last active
February 11, 2021 20:27
-
-
Save neeravkumar/7298372 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
BUCKETNAME="your_s3_bucket" | |
LOGDIR="/var/log/nginx" | |
LOGDATE=$(date +"%Y%m%d") | |
HOST=$(wget -q -O- http://169.254.169.254/latest/meta-data/instance-id) | |
LOGFILES=("access") | |
echo "Moving access logs to dated logs.." | |
for LOGFILE in "${LOGFILES[@]}" | |
do | |
CURFILE="$LOGDIR/$LOGFILE.log" | |
NEWFILE="$LOGDIR/$LOGFILE-$LOGDATE.log" | |
mv $CURFILE $NEWFILE | |
done | |
echo "done!.." | |
echo "Sending rotate signal to nginx.." | |
NGINX_MASTER_PID=`ps aux | grep nginx | grep master | awk '{ printf $2" "}'` | |
kill -USR1 $NGINX_MASTER_PID | |
echo "done!.." | |
sleep 1 | |
echo "Uploading log files to s3.." | |
for LOGFILE in "${LOGFILES[@]}" | |
do | |
FILENAME="$LOGFILE-$LOGDATE-$HOST.log" | |
FILE="$LOGDIR/$FILENAME" | |
gzip $FILE | |
s3cmd put $FILE.gz s3://$BUCKETNAME/$FILENAME.gz | |
rm $FILE.gz | |
rm $FILE | |
done | |
echo "done!.." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment