Skip to content

Commit

Permalink
Alpha release, tested briefly
Browse files Browse the repository at this point in the history
  • Loading branch information
mbabineau committed Feb 11, 2009
1 parent 043410d commit 7e4af71
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README
@@ -0,0 +1,6 @@
logrotate_s3
Mike Babineau <michael.babineau@gmail.com>

Usage: logrotate_s3.py -c <config> -f <file>

Rotates, compresses, and uploads a file to S3. Read the comments in logrotate_s3.conf and insert your own values as needed.
8 changes: 8 additions & 0 deletions logrotate_s3.conf
@@ -0,0 +1,8 @@
[AWS]
# These will be provided by Amazon
aws_access_key_id = <access key id>
aws_secret_access_key = <secret access key>

[Log]
bucket = <bucket name> ; Ex: "userbucket"
key_prefix = <prefix> ; Ex: "prefix_" or "somedir/"
77 changes: 77 additions & 0 deletions logrotate_s3.py
@@ -0,0 +1,77 @@
#!/usr/bin/env python
"""
logrotate_s3.py
Author: Mike Babineau <michael.babineau@gmail.com>
Licensed under the GPL
"""
import os, gzip, ConfigParser, time, shutil
from optparse import OptionParser
from boto.s3.connection import S3Connection, Key

def rotate(file_src):
"""Rotate log"""
# Build new name ("<unix time>.<hostname>.r.log") and move file
file_dst = "%s/%s.%s.r.log" % (os.path.dirname(file_src), int(time.time()), os.uname()[1])
shutil.move(file_src, file_dst)

# Touch src file using append instead of write just in case
open(file_src, 'a').close()

return file_dst


def compress(file_in, file_out):
"""Compress a file using gzip"""
f = open(file_in, 'rb')
zf = gzip.open(file_out, 'wb')
zf.writelines(f)
f.close()
zf.close()
# Uncomment to delete uncompressed file
# os.remove(file_in)


def upload_to_s3(file, key, bucket, aws_access_key_id, aws_secret_access_key):
"""Upload a file to S3"""
# Build S3 connector
conn = S3Connection(aws_access_key_id, aws_secret_access_key)

# Use specified bucket, creating it if it does not exist
b = conn.create_bucket(bucket)

# Upload file
k = Key(b)
k.key = key
k.set_contents_from_filename(file)


def main():
# Parse arguments
parser = OptionParser()
parser.add_option("-c", "--config", dest="configfile", help="configuration file")
parser.add_option("-f", "--file", dest="file", help="file to be compressed and uploaded")
(options, args) = parser.parse_args()
file_in = options.file
configfile = options.configfile

# Parse config file
config = ConfigParser.ConfigParser()
config.read(configfile)

# Set vars based on values from config file
aws_access_key_id = config.get("AWS", "aws_access_key_id")
aws_secret_access_key = config.get("AWS", "aws_secret_access_key")
bucket = config.get("Log", "bucket")
key_prefix = config.get("Log", "key_prefix")

# Rotate, compress, and upload to S3
file_rot = rotate(file_in)
file_out = file_rot+".gz"
key = key_prefix+os.path.basename(file_out)
compress(file_rot, file_out)
upload_to_s3(file_out, key, bucket, aws_access_key_id, aws_secret_access_key)


if __name__ == "__main__":
main()

0 comments on commit 7e4af71

Please sign in to comment.