Automating MongoDB Backups: A Step-by-Step Guide
In this blog post, we’ll walk you through the process of creating automated backups for your Dockerized MongoDB instance. We'll provide scripts for local backups and for storing backups in an Amazon S3 bucket. Finally, we'll show you how to set up a cron job to run these backups daily.
Prerequisites
Ensure you have the following in place before proceeding:
A Linux server on which Docker is already installed. A running MongoDB container. If you plan to use Amazon S3 for your backups, make sure the AWS CLI is installed and configured.
Step 1: Create a Local Storage Backup Script
Before we create a script, let's back up our MongoDB database to a local store on your server.
-
Create the Backup Script:
Create a new shell script file called
backup_mongo_local.sh
:nano backup_mongo_local.sh
-
Add the Following Script to the File:
#!/bin/bash # Define variables BACKUP_DIR="/path/to/backup/directory" # Change this to your desired backup directory CONTAINER_NAME="projects-mongo-1" # The name of your MongoDB Docker container TIMESTAMP=$(date +"%F_%T") BACKUP_NAME="mongo_backup_$TIMESTAMP.gz" # Create backup directory if it doesn't exist mkdir -p "$BACKUP_DIR" # Run mongodump to create a backup docker exec "$CONTAINER_NAME" mongodump --archive="/data/db/$BACKUP_NAME" --gzip # Copy the backup from the container to the host docker cp "$CONTAINER_NAME:/data/db/$BACKUP_NAME" "$BACKUP_DIR/" # Remove the backup file from the container docker exec "$CONTAINER_NAME" rm "/data/db/$BACKUP_NAME" # Print success message echo "Backup completed successfully. Backup file: $BACKUP_DIR/$BACKUP_NAME"
-
Save and Close the File.
-
Make the Script Executable:
chmod +x backup_mongo_local.sh
Step 2: Write the Storage Backup Script for Amazon S3
Here are a few steps to follow on how to create the backup script, in case your setup requires that the backups be sent to the Amazon S3 service:
-
Install the AWS CLI:
sudo apt-get update sudo apt-get install awscli -y
-
Configure the AWS CLI:
aws configure
Follow the prompts to enter your AWS Access Key ID, Secret Access Key, default region name, and output format.
-
Create the Backup Script:
Create a new shell script file called
backup_mongo_s3.sh
:nano backup_mongo_s3.sh
-
Add the Following Script to the File:
#!/bin/bash # Define variables BACKUP_DIR="/tmp/mongo_backup" # Temporary directory for storing the backup before uploading CONTAINER_NAME="projects-mongo-1" # The name of your MongoDB Docker container TIMESTAMP=$(date +"%F_%T") BACKUP_NAME="mongo_backup_$TIMESTAMP.gz" S3_BUCKET_NAME="your-s3-bucket-name" # Replace with your S3 bucket name # Create temporary backup directory if it doesn't exist mkdir -p "$BACKUP_DIR" # Run mongodump to create a backup docker exec "$CONTAINER_NAME" mongodump --archive="/data/db/$BACKUP_NAME" --gzip # Copy the backup from the container to the host docker cp "$CONTAINER_NAME:/data/db/$BACKUP_NAME" "$BACKUP_DIR/" # Remove the backup file from the container docker exec "$CONTAINER_NAME" rm "/data/db/$BACKUP_NAME" # Upload the backup to S3 aws s3 cp "$BACKUP_DIR/$BACKUP_NAME" "s3://$S3_BUCKET_NAME/$BACKUP_NAME" # Check if the upload was successful if [ $? -eq 0 ]; then echo "Backup uploaded to S3 successfully. Backup file: s3://$S3_BUCKET_NAME/$BACKUP_NAME" # Remove the local backup file rm "$BACKUP_DIR/$BACKUP_NAME" else echo "Failed to upload backup to S3." fi # Cleanup rmdir "$BACKUP_DIR" # Print completion message echo "Backup process completed."
-
Save and Close the File.
-
Make the Script Executable:
chmod +x backup_mongo_s3.sh
Step 3: Setting Up a Cron Job to Automate the Backup
To automate the execution of the backup script daily, we’ll create a cron job.
-
Edit the Crontab File:
Open the crontab file for editing:
crontab -e
-
Add a New Cron Job:
To schedule the backup script to run daily at 2:00 AM, add the following line to the crontab file:
For local backup:
0 2 * * * /path/to/backup_mongo_local.sh >> /path/to/backup_log.txt 2>&1
For S3 backup:
0 2 * * * /path/to/backup_mongo_s3.sh >> /path/to/backup_log.txt 2>&1
Replace
/path/to/backup_mongo_local.sh
or/path/to/backup_mongo_s3.sh
with the actual path to your backup script, and/path/to/backup_log.txt
with the desired path for the log file. -
Save and Exit the Editor:
After adding the cron job, save the file and exit the editor. The changes will be applied, and the cron job will be scheduled.
-
Verify the Cron Job:
To verify that the cron job has been added, list the current cron jobs for your user by running:
crontab -l
Conclusion
Well, hopefully this part showed you how to set automated backups for your MongoDB, Dockerized or not. This way, you can store your backups there and be confident that your data is safe so you can restore them in case something goes wrong. Following the scripts and adding the needed Cron jobs in your server enabled bonuses for regular database management.
Sami Rahimi
Innovate relentlessly. Shape the future..
Recent Comments