We recently covered the use case of Percona Backup for MongoDB and Disk Snapshots in Google Cloud Platform; now it’s time to do the same for Amazon AWS.

For this demo, I have created a 2-shard MongoDB cluster (where each shard is a 3-node PSA Replica Set) deployed on EC2 instances. Each instance has an extra EBS volume attached for storing the MongoDB data, and the PBM agent is installed as per the documentation.

Manual example

Let’s start an external backup. This can be done from any host running pbm client.

As we can see, PBM selects one node of each shard and one config server to be backed up.

At this point, we need to create snapshots of all the hosts listed above. We could use the AWS console or run aws commands as shown below.

Let’s list the attached disks of one of the instances:

Once we know the volume IDs of the attached disks, we can snapshot them. For example:

After repeating the procedure for all the instances listed, we need to complete the backup:

Finally, let’s check the backup status:

reasons to switch from Mongodb to percona for mongodb

 

 

Automating the backup

We covered the manual approach, but let’s see how we can automate the backup process.

Creating an IAM role

We are going to use an instance profile with the required permissions to handle backup/restore. Another possibility could be creating an IAM user with an associated access/secret key pair.

Here are the steps to create the service account:

1. Create an IAM policy that grants the required permissions

2. Create an IAM role that can be assumed by EC2 instances

3. Attach the policy to the created role

4. Create an instance profile and add the role to it

5. Attach the role to our EC2 instance. In this case, I am using aws-test-mongodb-cfg00 to run the backup.

We can verify the role is correctly attached to the EC2 instance by querying the metadata:

With the role ready, we can now start working on automation.

Sample script

To automate the process, we basically need to loop through the list of hosts selected by PBM, get all the attached volumes, and create snapshots with descriptive names.

The following script requires AWS CLI to be installed. Keep in mind it is just a proof of concept, so don’t use it for production environments, as there is only basic error checking.

Conclusion

Percona Backup for MongoDB provides the interface for making snapshot-based physical backups and restores. Database owners benefit from faster and more cost-efficient backups while ensuring that their data remains consistent.

We have seen how the process works in detail for an AWS environment. Check out part two on restore and point-in-time recovery.

If you have any suggestions for feature requests or bug reports, please let us know by creating a ticket in our public issue tracker. Pull requests are also welcome!


MongoDB Performance Tuning is a collection of insights, strategies, and best practices from Percona’s MongoDB experts. Use it to diagnose — and correct — the issues that may be affecting your database’s performance.

 

Download MongoDB Performance Tuning today!

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments