I'm trying to take backup of documentDB using Lambda. I am able to take backup in S3 bucket as json file using below code but i have few concern where i need expert advice.
I am using the below code and able to upload json as a backup on s3 succesfully.
def lambda_handler(event, context):
bucket_folder = 'XXXXXXX'
bucket_name = 'XXXXXXXXX'
database=get_database_from_connection_string()
json_options = JSONOptions(datetime_representation=DatetimeRepresentation.ISO8601)
for collection_name in database.list_collection_names():
print(f"Collection: {collection_name}")
with open(temp_filepath, "w") as f:
for doc in database.get_collection(collection_name).find():
f.write(dumps(doc, json_options=json_options) + "\n")
s3.Bucket(bucket_name).upload_file(
temp_filepath, "{}/{}.json".format(bucket_folder, collection_name)
)
Another approach would be to programmatically use mongodump: https://docs.aws.amazon.com/documentdb/latest/developerguide/backup_restore-dump_restore_import_export_data.html