Aayush Kothari
Aayush Kothari

Reputation: 504

What is the best way to take snapshots of an EC2 instance running MongoDB?

I wanted to automate taking snapshots of the volume attached to an EC2 instance running the primary node of our production MongoDB replicaSet. While trying to gauge the pitfalls and best practices over Google, I came across the fact that data inconsistency and corruption are very much possible while creating a snapshot but not of journaling is enabled, which it is in our case.

So my question is - is it safe to go ahead and execute aws ec2 create-snapshot --volume-id <volume-id> to get clean backups of my data? Moreover, I plan on running the same command via a cron job that runs once every week. Is that a good enough plan to have scheduled backups?

Upvotes: 1

Views: 1465

Answers (1)

Mark B
Mark B

Reputation: 201088

For MongoDB on an EC2 instance I do the following:

  1. mongodump to a backup directory on the EBS volume
  2. zip the mongodump output directory
  3. copy the zip file to an S3 bucket (with encryption and versioning enabled)
  4. initiate a snapshot of the EBS volume

I write a script to perform the above tasks, and schedule it to run daily via cron. Now you will have a backup of the database on the EC2 instance, in the EBS snapshot, and on S3. You could go one step further by enabling cross region replication on the S3 bucket.

This setup provides multiple levels of backup. You should now be able to recover your database in the event of an EC2 failure, an EBS failure, an AWS Availability Zone failure or even a complete AWS Region failure.

I would recommend reading the official MongoDB documentation on EC2 deployments: https://docs.mongodb.org/ecosystem/platforms/amazon-ec2/ https://docs.mongodb.org/ecosystem/tutorial/backup-and-restore-mongodb-on-amazon-ec2/

Upvotes: 2

Related Questions