📦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
mkdir /path/to/backupsHow 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
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/backupsReplace
/path/to/backupswith your desired directory path.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.
Execute Backup The script uses
mongodumpto create a backup of the MongoDB database within the Docker container. Replace the placeholders with your MongoDB's specific details.Copy Backup to Host The created backup is then transferred from the Docker container to the specified backup directory on the host machine.
Cleanup Temporary files created during the backup process within the Docker container are removed to free up space.
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
-itoption fromdocker execcommands 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
mongorestoreand 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:
Setting Variables: It defines necessary variables including the Docker container name, backup directory, MongoDB connection details, and a timestamp.
Executing Backup: Performs a
mongodumpwithin the specified Docker container targeting the MongoDB host and port, using optional authentication if provided.Copying Backup to Host: Transfers the backup from the Docker container to the host machine's specified backup directory.
Cleanup: Removes the temporary backup files from within the container after copying.
Notification: Sends a notification with the backup completion status and location using
curlto 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