Created
April 23, 2015 19:59
-
-
Save jayzeng/ce6a040e07d75e3d37b1 to your computer and use it in GitHub Desktop.
get & download rds logs
This file contains hidden or 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
import subprocess | |
import json | |
import os, errno | |
import argparse | |
def mkdir_p(path): | |
try: | |
os.makedirs(path) | |
except OSError as exc: | |
if exc.errno == errno.EEXIST and os.path.isdir(path): | |
pass | |
else: raise | |
def main(target_db): | |
# get available logs | |
output = subprocess.Popen(['aws','rds', 'describe-db-log-files', '--db-instance-identifier', target_db],stdout=subprocess.PIPE).communicate()[0] | |
dict_output = json.loads(output) | |
log_file_names = map(lambda l:l['LogFileName'], dict_output['DescribeDBLogFiles']) | |
mkdir_p('logs/error') | |
# aws rds download-db-log-file-portion --db-instance-identifier prod-api --log-file-name error/postgresql.log.2015-04-20-18 --output text > logs/error/postgresql.log.2015-04-20-18 | |
for log_file_name in log_file_names: | |
content = subprocess.Popen(['aws','rds', 'download-db-log-file-portion', '--db-instance-identifier', target_db, '--log-file-name', log_file_name, '--output', 'text'],stdout=subprocess.PIPE).communicate()[0] | |
with open('logs/%s' % log_file_name, 'wb') as fw: | |
fw.write(content) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser() | |
parser.add_argument("--db", dest='target_db', help="which db do you want to export logs?", default='prod-api') | |
args = parser.parse_args() | |
main(args.target_db) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment