You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.1 KiB

4 years ago
  1. #!/home/alpcentaur/ProjektA/PrototypeWebApp/venv/bin/python3.5
  2. import argparse
  3. import errno
  4. import os
  5. import boto
  6. from boto.compat import json
  7. from boto.compat import six
  8. DESCRIPTION = """Dump the contents of one or more DynamoDB tables to the local filesystem.
  9. Each table is dumped into two files:
  10. - {table_name}.metadata stores the table's name, schema and provisioned
  11. throughput.
  12. - {table_name}.data stores the table's actual contents.
  13. Both files are created in the current directory. To write them somewhere else,
  14. use the --out-dir parameter (the target directory will be created if needed).
  15. """
  16. def dump_table(table, out_dir):
  17. metadata_file = os.path.join(out_dir, "%s.metadata" % table.name)
  18. data_file = os.path.join(out_dir, "%s.data" % table.name)
  19. with open(metadata_file, "w") as metadata_fd:
  20. json.dump(
  21. {
  22. "name": table.name,
  23. "schema": table.schema.dict,
  24. "read_units": table.read_units,
  25. "write_units": table.write_units,
  26. },
  27. metadata_fd
  28. )
  29. with open(data_file, "w") as data_fd:
  30. for item in table.scan():
  31. # JSON can't serialize sets -- convert those to lists.
  32. data = {}
  33. for k, v in six.iteritems(item):
  34. if isinstance(v, (set, frozenset)):
  35. data[k] = list(v)
  36. else:
  37. data[k] = v
  38. data_fd.write(json.dumps(data))
  39. data_fd.write("\n")
  40. def dynamodb_dump(tables, out_dir):
  41. try:
  42. os.makedirs(out_dir)
  43. except OSError as e:
  44. # We don't care if the dir already exists.
  45. if e.errno != errno.EEXIST:
  46. raise
  47. conn = boto.connect_dynamodb()
  48. for t in tables:
  49. dump_table(conn.get_table(t), out_dir)
  50. if __name__ == "__main__":
  51. parser = argparse.ArgumentParser(
  52. prog="dynamodb_dump",
  53. description=DESCRIPTION
  54. )
  55. parser.add_argument("--out-dir", default=".")
  56. parser.add_argument("tables", metavar="TABLES", nargs="+")
  57. namespace = parser.parse_args()
  58. dynamodb_dump(namespace.tables, namespace.out_dir)