What I m trying to do

Rotate my logs everyday at midnight, archive the rotated files. then upload them to an S3 bucket. all archived files on S3 will be referenced with a timestamp, to make the search process easy

Enviroment

Tools

Basic Steps

  1. configure logrotate
  2. create bash script to upload files to S3
  3. setup init.d to upload files in case of (restart or shutdown)

Configuration

1. logrotate

/var/log/apache2/*.log {
        daily
        missingok
        rotate 10
        compress
        delaycompress
        ifempty
        create 640 root adm
        prerotate
               /bin/bash /home/ubuntu/backup_current_logs.sh
        endscript
        postrotate
                if /etc/init.d/apache2 status > /dev/null ; then \
                    /etc/init.d/apache2 reload > /dev/null; \
                fi;
        endscript

}

2. Uploading to S3

# this script will create an archive file of current apache logs
tar -C/var --warning=no-file-changed  -zcf /tmp/log.tar.gz log/apache2/error.log log/apache2/access.log

# then upload the archived file to S3 bucket
EC2_INSTANCE_ID="`wget -q -O - http://instance-data/latest/meta-data/instance-id`"
su - ubuntu -c "aws s3 cp /tmp/log.tar.gz s3://my-bucket-name/environment/`date +%Y-%m-%d`_${EC2_INSTANCE_ID}.tar.gz "

3. Managing shutdown/restart