Skip to main content

💾 MongoDB Backup via Cron

This tutorial teaches how to configure automatic backups of MongoDB using a bash script with mongodump and scheduling via cron.


🧰 Prerequisites

  • MongoDB already installed on the server

  • User with permissions to perform mongodump

  • Permissions to edit crontab

  • Backup script configured (example below)


📜 Backup Script

By default, MongoDB environments created on SaveinCloud already include a backup script located at:

/var/lib/jelastic/bin/backup_script.sh

This script uses the utility mongodump to generate backup files automatically.

🔧 To start using it, just edit the first variables of the script and insert your credentials, as provided by email when the environment was created.

Example:

MONGO_USERNAME="admin"
MONGO_PASSWORD="sua_senha_aqui"

After configuring, you can test the script manually by running:

bash /var/lib/jelastic/bin/backup_script.sh

Or schedule it in cron for automatic backups.

Below, we provide the full script; if you no longer have it in your environment, just create it and add the content.

If you already have the correct file, skip to the next step.


#!/bin/bash



MONGO_USERNAME="admin";

MONGO_PASSWORD="<SUA_SENHA>";

DAYS_TO_STORE=3;

BACKUP_LOG="/var/log/mongodb/backup.log";

BACKUP_DIR="/var/lib/jelastic/backup"



# DATABASES_LIST=("db1" "db2"); # Descomente para backup seletivo



function createBackup(){

local backup_date=$(date +"%d-%m-%y-%H%M%S");

local MONGODUMP=$(which mongodump);

local MONGOSHELL=$(which mongo);

ARCHIVE_NAME=MongoBackup-${backup_date};

BACKUP_TARGET="$BACKUP_DIR/$ARCHIVE_NAME";



$MONGOSHELL -u "$MONGO_USERNAME" -p "$MONGO_PASSWORD" admin --eval "db" >/dev/null 2>/dev/null || {

echo "Login no MongoDB falhou";

return 0;

}



echo $(date) Iniciando backup em: $BACKUP_TARGET

[ ! -d $BACKUP_TARGET ] && mkdir -p $BACKUP_TARGET

cd $BACKUP_TARGET



if [ ! -z "$DATABASES_LIST" ]; then

for dbname in ${DATABASES_LIST[@]}; do

$MONGODUMP -u "$MONGO_USERNAME" -p "$MONGO_PASSWORD" -d $dbname >> $BACKUP_LOG 2>&1;

done

else

$MONGODUMP -u "$MONGO_USERNAME" -p "$MONGO_PASSWORD" >> $BACKUP_LOG 2>&1;

fi



echo "$(date) Compactando arquivos..." >> $BACKUP_LOG

cd ..

tar -zcvf "$BACKUP_DIR/${ARCHIVE_NAME}.tgz" "./$ARCHIVE_NAME" >> $BACKUP_LOG 2>&1;

rm -rf $BACKUP_TARGET;



echo "$(date) Removendo backups mais antigos que $DAYS_TO_STORE dias..." >> $BACKUP_LOG

find $BACKUP_DIR -name "*.tgz" -mtime "+${DAYS_TO_STORE}" -exec rm {} \;

echo "$(date) Backup concluído com sucesso." >> $BACKUP_LOG

}



createBackup;

🔐 Permissions

Make the script executable:


chmod +x /caminho/para/mongo_backup.sh



⏰ Scheduling with cron

Edit the crontab with:


crontab -e

Add the line below to run the backup every day at 2 AM:

Or change to the time you prefer.


0 2 * * * /var/lib/jelastic/bin/backup_script.sh


💡 Additional Tips

  • Backup logs will be stored in /var/log/mongodb/backup.log

  • Use .gitignore to never version your backups or scripts with passwords


✅ Conclusion

This method ensures automatic daily backups of your MongoDB databases, with automatic file rotation and low operational impact.

Ideal for production, development, and testing environments.