Using Cron Jobs in Ubuntu to Schedule Backup and Restore MongoDB Database

Using Cron Jobs in Ubuntu to Schedule Backup and Restore MongoDB Database

Prerequisite: Installing MongoDB Database Tools

To backup MongoDB database, you need the mongodump utility, which is part of the mongodb-database-tools package.

Step 1: Check if mongodb-database-tools is installed:

sudo dpkg -l mongodb-database-tools

If the package is installed, you’ll see the details. Otherwise, you’ll get an error message like:

dpkg-query: no packages found matching mongodb-database-tools

Step 2: Install mongodb-database-tools:

If the package is not installed, download and install it:

  1. Go to the Official MongoDB Tools Page at link below:
Download MongoDB Command Line Database Tools
Command line tools available for working with MongoDB deployments. Tools include mongodump, mongorestore, mongoimport, and more. Download now.
  1. Select the Correct Options:
  1. Copy the Download Link: Once you’ve selected the options, copy the download link provided.
  2. Download and Install the Package:
curl -o mongodb-database-tools.deb <copied-download-link>
sudo apt install ./mongodb-database-tools.deb
  1. Verify Installation:
mongodump --version

You should receive message like below:


Backing Up MongoDB: A Step-by-Step Guide

Once you have mongodump installed, follow these steps to securely back up your MongoDB database.

Step 1: Create a backup directory:

mkdir -p /path/to/backup/

Step 2: Securely Configure the Backup

MongoDB credentials should never be stored in plain text within a script. Use a config.yml file for secure configuration.

a. Create a config.yml file:

uri: "mongodb://<db_username>:<db_password>@<host>:<portnumber>"

Replace <placeholders> with your actual credentials:

b. Create a Backup Script

#!/bin/bash

# Set database name, backup directory and MongoDB credentials
DATABASE_NAME="your_database_name"
BACKUP_DIR="/path/to/backup/$DATABASE_NAME/$(date +%F_%H-%M-%S)"
CONFIG_FILE = "/path/to/backup/config/config.yml"

# Create backup directory
mkdir -p "$BACKUP_DIR"

# Run mongodump
mongodump --config="$CONFIG_FILE" \
  --authenticationDatabase=admin \
  --db="$DATABASE_NAME" \
  --out "$BACKUP_DIR" \
  --gzip

# Show notification
echo "$DATABASE_NAME has been scucesfully backed up to $BACKUP_DIR"

# Optional: Remove backups older than 7 days
find /path/to/backup/directory -type d -mtime +7 -exec rm -rf {} \;

Replace /path/to/backup/ and your_database_name with your actual paths and database name.

Step 3: Make the Script Executable Set the appropriate permissions to allow execution:

chmod +x /usr/local/bin/mongodb_backup.sh

Automating Backups with crontab:

Step 1: Edit Crontab:

crontab -e

Step 2: Add a Cron Job: Add the following line to schedule the backup (e.g., daily at 2 AM):

0 2 * * * /usr/local/bin/mongodb_backup.sh >> /var/log/mongodb_backup.log 2>&1
  • 0 2 * * *: Specifies the time (2 AM daily).
  • /usr/local/bin/mongodb_backup.sh: Path to the backup script.
  • >> /var/log/mongodb_backup.log 2>&1: Logs output and errors to a log file.

Step 3: Save and close the crontab editor.


Manual Testing and Verification:

  1. Test the script manually:
/usr/local/bin/mongodb_backup.sh
  1. Verify the Backup
ls /path/to/backup/

Ensure the expected files and directories are present.


Conclusion

This setup ensures that your MongoDB database is backed up securely and automatically. By following these steps:

  • You’ve installed the necessary tools.
  • Configured a secure backup script.
  • Automated the process with cron.

This approach not only secures sensitive credentials but also simplifies the management of backup retention, keeping your database safe and recoverable in case of emergencies.


Read more