Created
July 8, 2015 18:26
-
-
Save jayzeng/91a20e522da5bfcfa3db to your computer and use it in GitHub Desktop.
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] | |
new_log_file_name = 'logs/%s' % log_file_name | |
print "writing %s" % new_log_file_name | |
with open(new_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