Skip to content

Instantly share code, notes, and snippets.

@eduardohki
Last active October 31, 2017 09:37
Show Gist options
  • Save eduardohki/aca7327ff436e6c6b66ed3bec29a0a80 to your computer and use it in GitHub Desktop.
Save eduardohki/aca7327ff436e6c6b66ed3bec29a0a80 to your computer and use it in GitHub Desktop.
Script para automação do Backup do MongoDB
#!/bin/bash
# mongo-backup.sh
# Script to automate MongoDB Backups
#
# Requirements:
# mongodump
#
# Author:
# Eduardo Hernacki <eduardohki@gmail.com>
#
# Version:
# 1.1 - 2017/08/08
#
# NOTE:
# In case of permission errors in mongodump with "admin" user,
# please add the following commands in "mongo" shell:
#
# > use admin
# > db.auth("admin", "<your_password>")
# > db.grantRolesToUser( "admin", [{ role: "backup", db: "admin" }])
# > exit
#
# Backup destination
DUMP_DIR="/var/backup/mongodb"
# Destino do arquivo de logfile
LOG_FILE=$DUMP_DIR/backup.log
# Credenciais MongoDB
MONGO_USER=admin
MONGO_PASS=<senha_admin_mongodb>
MONGO_AUTHDB=admin
# Registra inicio de execução no arquivo de log
echo "[$(date +%Y/%m/%d\ %H:%M:%S)] Starting mongodump" >> $LOG_FILE
# Executa o dump do MongoDB no diretorio especificado
mongodump --host localhost --username $MONGO_USER --password $MONGO_PASS --authenticationDatabase $MONGO_AUTHDB --out $DUMP_DIR >> $LOG_FILE 2>&1
# Checks the dump execution
if [ $? -eq 0 ]; then
echo "[$(date +%Y/%m/%d\ %H:%M:%S)] mongodump successfully created" >> $LOG_FILE
OUTPUT="OK: MongoDB Dump successfully created"
EXITCODE=0
else
echo "[$(date +%Y/%m/%d\ %H:%M:%S)] mongodump finished with errors" >> $LOG_FILE
OUTPUT="CRITICAL: Errors creating MongoDB Dump! Please verify the log \"$LOG_FILE\" for more details"
EXITCODE=2
fi
echo $OUTPUT
exit $EXITCODE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment