阿里云主机折上折
  • 微信号
Current Site:Index > The backup feature of MongoDB Atlas

The backup feature of MongoDB Atlas

Author:Chuan Chen 阅读数:14123人阅读 分类: MongoDB

Overview of MongoDB Atlas Backup Features

MongoDB Atlas, as a managed database service, provides robust backup capabilities to ensure data security and recoverability. Its backup solution supports automated snapshots, point-in-time recovery, and cross-region replication, meeting data protection needs across various business scenarios.

Automatic Backup Configuration

Atlas enables automatic backups by default, creating daily cluster snapshots retained for 7 days. Users can adjust backup policies via UI or API:

// Example of updating backup policy via Atlas API
const updateBackupPolicy = async () => {
  const response = await fetch('https://cloud.mongodb.com/api/atlas/v1.0/groups/{groupId}/clusters/{clusterName}/backup/schedule', {
    method: 'PATCH',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${API_KEY}`
    },
    body: JSON.stringify({
      "autoExportEnabled": true,
      "export": {
        "exportBucketId": "5f4d7a9e6f8b7a001a3b3b3b",
        "frequencyType": "weekly"
      },
      "policies": [{
        "id": "default",
        "policyItems": [{
          "frequencyInterval": 1,
          "retentionUnit": "days",
          "retentionValue": 14
        }]
      }]
    })
  });
  return response.json();
};

Key configuration parameters include:

  • Snapshot retention period (1-35 days)
  • Backup frequency (6/8/12/24 hours)
  • Export to AWS S3 bucket option
  • Backup window settings

Point-in-Time Recovery (PITR)

Atlas provides second-level precision point-in-time recovery based on oplog:

// Example of executing PITR query using MongoDB Driver
const { MongoClient } = require('mongodb');

async function queryHistoricalData() {
  const client = new MongoClient(ATLAS_URI);
  await client.connect();
  
  const session = client.startSession({
    snapshot: true,
    atClusterTime: new Timestamp(1658761200, 1) // Specify point in time
  });

  try {
    const db = client.db('production');
    const orders = db.collection('orders');
    return await orders.find({ status: 'pending' }).session(session).toArray();
  } finally {
    await session.endSession();
    await client.close();
  }
}

PITR features:

  • Supports up to 35-day recovery window
  • Recovery process creates new cluster without affecting original
  • API-triggered recovery operations supported
  • Automatic event notifications upon completion

Cross-Region Backup Replication

For global deployments, Atlas supports replicating backups to other regions:

  1. Configure cross-region replication policy in AWS console
  2. Set target regions (e.g., from us-east-1 to eu-west-1)
  3. Define replication latency threshold (default 15 minutes)
  4. Monitor replication status via Atlas Metrics API
// Example of checking cross-region backup status
const checkBackupReplication = async () => {
  const res = await fetch('https://cloud.mongodb.com/api/atlas/v1.0/groups/{groupId}/clusters/{clusterName}/backup/snapshots', {
    headers: { 'Authorization': `Bearer ${API_KEY}` }
  });
  const snapshots = await res.json();
  return snapshots.results.map(s => ({
    id: s.id,
    created: s.createdAt,
    regions: s.replicaSetName ? [s.replicaSetName] : s.shards.map(sh => sh.regionName),
    status: s.status
  }));
};

Backup Encryption and Compliance

Atlas backups provide multi-layer security:

  • Encryption at rest: Using AWS KMS or Azure Key Vault for key management
  • Encryption in transit: TLS 1.2+ protocols ensure secure transmission
  • Compliance certifications: Supports HIPAA, GDPR requirements
  • Access control: Role-based backup management permissions (RBAC)

Encryption configuration example:

# Terraform configuration for backup encryption
resource "mongodbatlas_encryption_at_rest" "backup_encryption" {
  project_id     = var.project_id
  aws_kms = {
    enabled             = true
    customer_master_key = aws_kms_key.backup_key.arn
    region              = "us-east-1"
    role_id             = mongodbatlas_cloud_provider_access_setup.setup.role_id
  }
}

Backup Monitoring and Alerts

Atlas provides multiple ways to monitor backup status:

  1. Built-in dashboards display:

    • Backup storage usage
    • Last successful backup time
    • Recovery Point Objective (RPO) achievement rate
  2. Configurable alerts:

    // Setting up backup failure alert
    const createBackupAlert = async () => {
      await fetch('https://cloud.mongodb.com/api/atlas/v1.0/groups/{groupId}/alertConfigs', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'Authorization': `Bearer ${API_KEY}`
        },
        body: JSON.stringify({
          eventTypeName: "BACKUP_AGENT_FAILURE",
          enabled: true,
          notifications: [{
            typeName: "EMAIL",
            intervalMin: 5,
            delayMin: 0,
            emailAddress: "dba@example.com"
          }]
        })
      });
    };
    
  3. Third-party monitoring integration:

    • Send backup events to Slack via Webhook
    • Export backup metrics using Prometheus
    • Configure CloudWatch alarm rules

Backup Cost Optimization

Strategic backup planning can significantly reduce costs:

  1. Tiered storage policy example:

    • Last 3 days: High-frequency snapshots (every 6 hours)
    • 4-7 days: Daily snapshots
    • 8-35 days: Weekly snapshots
  2. Lifecycle management:

    // Script for automatic cleanup of expired backups
    const cleanOldBackups = async () => {
      const snapshots = await listSnapshots();
      const expirationDate = new Date();
      expirationDate.setDate(expirationDate.getDate() - 30);
      
      for (const snap of snapshots) {
        if (new Date(snap.createdAt) < expirationDate) {
          await deleteSnapshot(snap.id);
        }
      }
    };
    
  3. Cost estimation tools:

    • Atlas billing API provides backup storage predictions
    • AWS Cost Explorer analyzes cross-region replication costs
    • Use Atlas CLI to estimate cost differences across retention periods

Disaster Recovery Drills

Regular validation of backup effectiveness is critical:

  1. Standard drill process:

    • Select non-critical business periods monthly
    • Randomly choose recovery points
    • Execute full cluster recovery
    • Verify data consistency and application connectivity
  2. Automated drill script example:

    # Using PyMongo for recovery validation
    def test_restore(cluster_name, restore_time):
        restore_job = atlas_client.create_restore_job(
            cluster_name=cluster_name,
            target_cluster_name=f"dr-{cluster_name}",
            point_in_time_utc=restore_time)
        
        while restore_job.status != 'completed':
            time.sleep(300)
            restore_job = atlas_client.get_restore_job(restore_job.id)
        
        test_client = MongoClient(f"mongodb+srv://dr-{cluster_name}.mongodb.net")
        assert test_client.admin.command('ping')['ok'] == 1.0
        return test_client
    
  3. Drill documentation:

    • Recovery time statistics
    • Data discrepancy reports
    • Improvement action tracking table

本站部分内容来自互联网,一切版权均归源网站或源作者所有。

如果侵犯了你的权益请来信告知我们删除。邮箱:cc@cccx.cn

Front End Chuan

Front End Chuan, Chen Chuan's Code Teahouse 🍵, specializing in exorcising all kinds of stubborn bugs 💻. Daily serving baldness-warning-level development insights 🛠️, with a bonus of one-liners that'll make you laugh for ten years 🐟. Occasionally drops pixel-perfect romance brewed in a coffee cup ☕.