Point-in-Time Data Recovery on MongoDB Atlas
PythonPoint-in-Time Data Recovery (PITR) allows you to quickly recover your MongoDB Atlas databases to a previous state in time. This can be incredibly valuable in scenarios such as accidental data deletion, data corruption, or other unforeseen data loss incidents. To leverage this feature, you need to configure your MongoDB Atlas cluster with PITR capabilities.
Below I will guide you through setting up a MongoDB Atlas cluster with PITR enabled using Pulumi, which programmatically defines your cloud infrastructure. For this task, we'll use the Pulumi MongoDB Atlas provider.
Essentially, we're going to create a MongoDB cluster and ensure the
pitEnabled
property is set toTrue
. This action tells MongoDB Atlas to enable Point-in-Time Recovery for the cluster.Here's a step-by-step program in Python that demonstrates how to do this with Pulumi:
import pulumi import pulumi_mongodbatlas as mongodbatlas # The MongoDB Atlas API token must be configured with Pulumi beforehand. # This can be set using the `pulumi config set --secret` command with the appropriate tokens. # Configuration variables for the MongoDB Atlas project and cluster. project_id = 'your-atlas-project-id' # Replace with your MongoDB Atlas Project ID. cluster_name = 'your-cluster-name' # Replace with your desired Cluster Name. provider_name = 'AWS' # Replace with your cloud provider, i.e., 'AWS', 'GCP', or 'AZURE'. instance_size = 'M10' # Replace with your desired instance size. region = 'us-west-2' # Replace with your desired region. # Create a MongoDB Cluster with PITR enabled. cluster = mongodbatlas.Cluster( "pulumi-cluster", project_id=project_id, name=cluster_name, cluster_type="REPLICASET", # We're creating a replica set. replication_specs=[{ "num_shards": 1, "regions_configs": [{ "region_name": region, "electable_nodes": 3, "priority": 7, "read_only_nodes": 0, "analytics_nodes": 0, }], }], backup_enabled=True, # Ensure backup is enabled. pit_enabled=True, # Enable Point-in-Time Data Recovery. auto_scaling_disk_gb_enabled=True, # Optional: Enable auto scaling for storage. provider_instance_size_name=instance_size, provider_name=provider_name, mongo_db_major_version="4.4", # Specify your MongoDB version, e.g., "4.4". ) # Export the connection strings to connect to the cluster. # This output will provide you with the details needed to connect to the cluster after its creation. pulumi.export('connection_strings', cluster.connection_strings) pulumi.export('mongo_uri', cluster.mongo_uri) pulumi.export('mongo_uri_with_options', cluster.mongo_uri_with_options)
In this Pulumi program, we create a new
mongodbatlas.Cluster
resource with PITR enabled. Make sure you replace the placeholder values with your own MongoDB Atlas Project ID, your desired cluster name, cloud provider, instance size, and region.After setting up the cluster, we export the connection strings which will provide the necessary details to connect to your MongoDB cluster. Remember that the exported values will be visible in the Pulumi console after the cluster resources have been successfully provisioned.
Please note that you need a MongoDB Atlas account, and you are expected to have configured your Pulumi MongoDB Atlas provider with the necessary API tokens to authenticate requests.
If you have any questions about this code or how to run it within your environment, feel free to ask!