📦MongoDB Backup/Restore

Script automates the process of creating a backup of a MongoDB database in a Docker container, copying the backup to the host machine, and cleaning up temporary files, with an optional notification at the end

Backup Script

  1. Creating a Backup Directory

mkdir /path/to/backups

How to Perform a MongoDB Backup from a Docker Container

This guide provides step-by-step instructions on how to backup a MongoDB database that is running within a Docker container. The script automates this process, including exporting the backup to a host machine and clearing temporary files.

Prerequisites

  • Docker daemon running

  • MongoDB running within a Docker container

  • Access to a terminal or command prompt

Step-by-Step Guide

  1. Create a Backup Directory on Host Ensure there is a directory on the host machine where the backup will be stored. If not, create it using:

    mkdir /path/to/backups

    Replace /path/to/backups with your desired directory path.

  2. Set Parameters Configure the script with your MongoDB Docker container name, backup directory, MongoDB connection details (if authentication is needed), and a timestamp for the backup filename.

  3. Execute Backup The script uses mongodump to create a backup of the MongoDB database within the Docker container. Replace the placeholders with your MongoDB's specific details.

  4. Copy Backup to Host The created backup is then transferred from the Docker container to the specified backup directory on the host machine.

  5. Cleanup Temporary files created during the backup process within the Docker container are removed to free up space.

  6. Optional: Send Notification A notification can be sent to a specified endpoint to signify the completion of the backup process, providing details like the backup location.

Automation and Considerations

  • Automation with Cron: To automate this backup process, a cron job can be created. It's important to remove the -it option from docker exec commands for compatibility with cron, since cron jobs do not have an interactive shell.

  • Security: Ensure proper security measures are in place when dealing with backups, especially if they contain sensitive information. Secure the backup directory and use secured connections when transferring the backup.

  • Restoration: Know the process for restoring from the backup if needed, which typically involves using mongorestore and pointing it to the backup file location.

Backup Script

MongoDB Backup Script Summary

This script automates the backup process for a MongoDB database running in a Docker container. Here's how it works:

  1. Setting Variables: It defines necessary variables including the Docker container name, backup directory, MongoDB connection details, and a timestamp.

  2. Executing Backup: Performs a mongodump within the specified Docker container targeting the MongoDB host and port, using optional authentication if provided.

  3. Copying Backup to Host: Transfers the backup from the Docker container to the host machine's specified backup directory.

  4. Cleanup: Removes the temporary backup files from within the container after copying.

  5. Notification: Sends a notification with the backup completion status and location using curl to a specified endpoint.

Note: For automating this script with cron jobs, it's recommended to remove the -it option from docker exec commands to avoid issues, as cron jobs do not have an interactive shell.

Last updated