How to automate MongoDB backup and upload it to S3 storage using mongodump-s3-service docker image

Database backups are essential for protection against data loss that can completely disrupt business operations. Regular backups are crucial when you have transactional or very dynamic data in your database. Unlike SQL, MongoDB does not come with shadow paging, unless you are ready to work with replica sets (To deploy mongo in replica sets, click here).

TLDR; The docker image, developed by our CTO and Co-Founder Rohan Dhamapurkar, can be found here.

How to use the container:

  • The package will take a full database instance backup locally as well as on the server.
  • You can change the number of days the local backup stays.
  • You can take regular backups or instant backups whenever needed with the help of the regular Linux CRONs.
  • Automatic backups are possible on your specified time of day.
  • The mongodump created will be named as

Note: A sample .env file is to be made comprising of all the required configurations and keys. You can find a sample .env file in the mongodump-s3-service repository.

Below is the sample .env file:

# mongo connection credentials
# aws/digital ocean access key and secret key
# skip for provider digital ocean
# skip for provider aws
# s3 bucket details
# additional options for deleting old files
# cron string to run the back up on specified day and time
# to set timezone
  1. To run the container, use the following command:

This command when executed, will start the backup process and take regular backups of your MongoDB database at your specified day and time.

docker run --name mongodump-s3-service \
-v ~/backup_files:/usr/files \
--network "host" \
--env-file .env \
-d \


  • - -name: Name of the docker image container. You can use it when referencing the container within a Docker network.
  • -v: To bind the docker db file path to local file. For reference to volumes in dockers, click here.
  • - -network: We have used host as the network. It means this container will use the Docker host network stack.
  • - -env-file: The file path to the .env file.
  • -d: To run a container in detached mode. For more information, click here.

2. To take instant backups, use the following command:

This command is used to take instant backups.

docker exec -it mongodump-s3-service instant

Note: The docker instance needs to be running and the mongodump-s3-service container should be there on the server for this command to run.


  • exec: To run a command in a running container.
  • -it: To run an interactive container.

3. For building a local image and running the container, use the following commands:

This command is used to create a local image first and run the container locally. Run these commands only if you want to make changes to the repo code.

docker build --tag mongodump-s3-service:latest .

docker run --name mongodump-s3-service \
-v ~/backup_files:/usr/files \
--network "host" \
--env-file .env \
-d \


  • build: The docker build command builds Docker images from a DockerFile and a “context”.
  • - -tag: To tag the mongo backup image to a local image.

If we missed out on something you were looking for, drop down a message on our social media handles:

Facebook: OrderStack

We are a technology solutions startup. We started right out of college as a Chatbot service provider and later branched into bespoke software solutions.