Backup and Restore MongoDB in a Docker Environment
Summary
Introduction
In our previous post, we discussed how to run a MongoDB database in a Docker container. And we learned how to create a 3-node replica set using Docker Compose. If you missed it, you can check it out here. In this post, we'll cover an essential topic: Backup and Restore MongoDB in a Docker Environment. I'll walk you through the process of creating a full backup of a MongoDB database running in a Docker container and restoring the backup to a new MongoDB container.
The importance of Backups in a Containerized Environment
Containers are designed to be ephemeral, meaning they can be created, destroyed and recreated at any time. While this offers flexibility, it also means that any data stored within a container is at risk of being lost when the container is removed, or lost due to corruption or other issues. Therefore, it's crucial to have a robust backup and restore strategy to protect your data from loss or corruption and ensure data availability and business continuity.
Why backups matter
1- Data Loss Prevention: Backups are essential for preventing data loss due to accidental deletion, corruption, or hardware failure, or other unexpected events. Having a backup ensures that you can recover your data in case of any such event.
2- Business Continuity: Backups are crucial for maintaining business continuity in the event of data loss. Having a reliable backup strategy ensures that you can quickly recover your data and resume operations without significant downtime, especially in containerized environments.
3- Disaster Recovery: Backups are an essential part of any disaster recovery plan. Having a reliable backup strategy ensures that you can quickly recover your data and resume operations in the event of a disaster.
4- Compliance and Legal Requirements: Many industries have strict compliance and legal requirements regarding data
5- Peace of Mind: Having a reliable backup strategy in place gives you peace of mind knowing that your data is protected and can be recovered in case of any event.
Backup strategies for MongoDB
MongoDB provides several options for backing up your data, including incremental backups, full backups, and differential backups.
1- Incremental Backups: Incremental backups capture only the changes made since the last backup, reducing the amount of data that needs to be backed up and speeding up the backup process, but can be complex to manage and restore.
2- Full Backups: Full backups capture the entire database, including all data and indexes. While full backups are more time-consuming and resource-intensive than incremental backups, they provide a complete snapshot of the database that can be used to restore the database to a specific point in time.
3- Differential Backups: Differential backups capture only the changes made since the last full backup, reducing the amount of data that needs to be backed up compared to a full backup. This can be a good compromise between full and incremental backups, but can be also complex.
In this post, we'll focus on the full backup strategy as it's the simplest and most reliable method for backing up your database.
Performing a Full Backup of MongoDB
Prerequisites
1- Docker and Docker Compose installed on your system. 2- Basic knowledge of Docker concepts such as volumes. 3- Time to complete the tutorial: 6 to 10 minutes.
Step 1: Start a MongoDB container
First, we'll pull the MongoDB image from Docker Hub and start a MongoDB container.
docker pull mongo:7.0-jammy # Pull the MongoDB image
docker volume create mongo-data # Create a volume to persist the MongoDB data
docker run -d --name mongodb -v mongo-data:/data/db -p 27017:27017 mongo:7.0-jammy # Start a MongoDB container
Step 2: Run the mongodump command
To create a full backup of your MongoDB database, you'll use the mongodump
command, which is a utility provided by
MongoDB. This command will create a binary export of your database that can be used to restore the data later.
Run the following from your host machine to create a full backup of your MongoDB database:
docker exec -it mongodb mongodump --uri mongodb://localhost:27017/test --gzip --archive=test.archive
In my example my container is named mongo, and my database is named test
, you should replace it with your database
name. Also, notice the --gzip
flag, which compresses the backup file to save disk space. My test database is about
18MB, and the backup file is about 627KB. This is a significant reduction in size.
Another important thing is the --uri
flag, which specifies the connection string to the MongoDB database. In this case, we're connecting to the
MongoDB database running on localhost
on port 27017
. If you followed the previous post, you may have a replica set
running on localhost:27017,localhost:27018,localhost:27019
. In this case, you should replace the connection string
with the appropriate one: mongodb://username:passwor@localhost:27017,username:passwor@locahost:27018,username:passwor@localhost:27019/?replicaSet=rs0
.
And if everything goes well, you should see a message indicating that the backup was successful. Something like this:
Step 3: Copy the backup file to your host machine
The mongodump
command will create a backup file inside the MongoDB container. But we should copy it to our host
machine because the container is ephemeral and can be destroyed at any time.
Run the following command to copy the backup file to your host machine:
docker cp mongodb:/test.archive /path/to/backup/yourdatabase.archive
Replace
/path/to/backup/yourdatabase.archive
with the path where you want to store the backup file on your host machine.
If everything goes well, you should see a message indicating that the file was copied successfully. Something like this:
Congratulations! You've successfully created a full backup of your MongoDB database. You can now store this backup file in a safe location and use it to restore your data if needed. When I say safe location, I mean a location different from your local or server, because, if your local machine or server fails, you will lose both the database and the backup.
Another option is to use a cloud storage service like AWS S3, Google Cloud Storage, Azure Blob Storage, or MinIo to store your backups. This way, your backups will be safe even if your local machine or server fails. You may also take care of the security of your backups by encrypting them before storing them in the cloud. This way, you ensure the database integrity and confidentiality.
Performing a MongoDB Restore
Just as important as creating backups is the ability to restore them when needed. The mongorestore
command is used to
restore a MongoDB backup created with mongodump
. In this section, we'll walk you through the process of restoring a
MongoDB backup in a containerized environment.
For this tutorial, we'll assume that our MongoDB container has been deleted unintentionally 😏, and the attached volume has gone with it. We'll use the backup file we created earlier to restore the data.
Step 1: Start a new MongoDB container
First, start a new MongoDB container using the same image and volume you used to create the backup.
docker run -d --name mongodb -v mongo-data:/data/db -p 27017:27017 mongo:7.0-jammy
Step 2: Copy the backup file to the new container
Next, copy the backup file from your host machine or get it from the cloud storage to the new MongoDB container. For this example, we will use the backup file we created earlier.
docker cp /path/to/backup/test.archive mongodb:/test.archive
Replace
/path/to/backup/test.archive
with the path to the backup file on your host machine.
Step 3: Run the mongorestore command
Finally, use the mongorestore
command to restore the backup file to the new MongoDB container.
docker exec -it mongodb mongorestore --uri mongodb://localhost:27017 --gzip --archive=test.archive
This command will restore the data from the backup file to the MongoDB database running in the new container. Notice
that we don't need to specify the database name because it's included in the backup file. The --gzip
flag is used to
tell mongorestore
that the backup file is compressed.
If everything goes well, you should see a message indicating that the restore was successful. Something like this:
Best Practices for MongoDB Backups in a Containerized Environment
1- Use Volumes: Store your MongoDB data on a volume to persist it even if the container is destroyed. This ensures that your data is safe and can be easily backed up and restored.
2- Regular Backups: Schedule regular backups of your MongoDB database to ensure that your data is always up-to-date and can be quickly restored in case of any event. Depending on your data, you may need to back up:
- Daily: ideal for databases with frequent changes.
- weekly: suitable for moderately active databases; with less frequent changes.
- monthly: for databases with infrequent changes.
3- Offsite Backups: Store your backups in an offsite location, such as a cloud storage service, to ensure that your data is safe even if your local machine or server fails. Remember to treat your database backups as sensitive data, meaning you should encrypt them before storing them in the cloud, and restrict access to them to authorized staff only.
3- Automate Backups: Set up automated backup jobs to run at regular intervals to ensure that your data is backed up regularly and consistently.
4- Test Restores: Regularly test your backup and restore process to ensure that your backups are valid and can be restored successfully. This will help you identify any issues with your backup strategy and make the necessary adjustments. After a backup, you should test the restore process in a sandbox environment to ensure that the backup is valid and can be restored successfully. And one more important thing, you should document the restore process, to ensure that any authorized staff can restore the database in case of any event.
Conclusion
In this article, we've covered the importance of backups in a containerized environment and walked you through the process of creating a full backup of a MongoDB database running in a Docker container. I've also shown you how to restore the backup to a new MongoDB container. By following these steps and best practices, you can ensure that your MongoDB data is safe, secure, and always available, even in the event of data loss or corruption. Remember, having a backup is essential, but being able to restore it is even more important. So, make sure to test your backup and restore processes regularly to ensure that your data is protected and can be recovered when needed.
In a future post, we'll cover an interesting topic: Automating MongoDB Backups in a Containerized Environment. I'm pretty sure you'll love it, because it will save you time and effort. Stay tuned!