Skip to content

Instantly share code, notes, and snippets.

@mustafaturan
Created June 6, 2012 13:35
Show Gist options
  • Save mustafaturan/2881892 to your computer and use it in GitHub Desktop.
Save mustafaturan/2881892 to your computer and use it in GitHub Desktop.
S3 Nginx Log rotation file
#!/bin/bash
BUCKETNAME="your_s3_bucket"
LOGDIR="/opt/nginx/logs"
LOGDATE=$(date +"%Y%m%d")
LOGFILES=( "access" "ssl-access" )
BOT_LOGFILES=( "bots-access" "bots-ssl-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 "Moving bots access logs to backup logs.."
for LOGFILE in "${BOT_LOGFILES[@]}"
do
CURFILE="$LOGDIR/$LOGFILE.log"
NEWFILE="$LOGDIR/$LOGFILE-backup.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.log"
FILE="$LOGDIR/$FILENAME"
gzip $FILE
s3cmd put $FILE.gz s3://$BUCKETNAME/$FILENAME.gz
rm $FILE.gz
done
echo "done!.."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment