Search code examples
python-3.xmongodbmongoosepymongoaws-documentdb

documentdb backup using lambda function


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.

  1. Using AWS Lambda It may take long time for backup of many collections, So i want to approche like it trigger some code and upload in S3 bucket, once it uploaded succesfully it raise me an update as status in a collection.
  2. How can i take backup as a bson format and upload in S3 bucket.

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)
        )

Solution

  • Another approach would be to programmatically use mongodump: https://docs.aws.amazon.com/documentdb/latest/developerguide/backup_restore-dump_restore_import_export_data.html