Last active
January 13, 2023 06:16
-
-
Save gcalcettebr/c7a6a8d12b8d03dc3729397cad3be9e9 to your computer and use it in GitHub Desktop.
MongoDB Backup on S3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# mongo-backup.sh | |
# Script to automate MongoDB Backups | |
# | |
# Requirements: | |
# mongodump | |
# s3cmd | |
# | |
# Author: | |
# Eduardo Hernacki <eduardohki@gmail.com> | |
# | |
# Modify: | |
# Guilherme Calcette <guilherme.calcette@gmail.com> | |
# | |
# Version: | |
# 1.0 - 2017/10/18 | |
# Bucket destino aws s3 | |
TIMESTAMP=`date +%F-%H-%M` | |
S3_BUCKET_NAME="< bucket name >" #replace with your bucket name on Amazon S3 | |
S3_BUCKET_PATH="< bucket path name >" #replace with your bucket path | |
# Limite de arquivos backups no bucket | |
RM_OLD_FILES=30 | |
# Backup destination | |
DUMP_DIR="< /diretorio/backup >" #replace with your backup DIR | |
DUMP_FILE="< filename >-$TIMESTAMP" #replace with your backup file name | |
mkdir $DUMP_DIR/$DUMP_FILE | |
# Destino do arquivo de logfile | |
LOG_FILE=$DUMP_DIR/$DUMP_FILE/$DUMP_FILE.log | |
# Credenciais MongoDB | |
MONGO_USER=adminmongo | |
MONGO_PASS=passadminmongo | |
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/$DUMP_FILE >> $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 | |
echo $OUTPUT | |
# Compactando | |
echo Compactando os arquivos | |
cd $DUMP_DIR && tar -cf $DUMP_FILE.tar $DUMP_FILE | |
#Evniando para AWS S3 | |
echo Enviando Backup para AWS S3 | |
s3cmd put -p $DUMP_DIR/$DUMP_FILE.tar s3://$S3_BUCKET_NAME/$S3_BUCKET_PATH/ | |
rm -rf $DUMP_DIR/$DUMP_FILE | |
#Removendo Backup com mais de 30 DIAS | |
echo Removendo arquivos com mais de $RM_OLD_FILES Dias. | |
s3cmd ls s3://$S3_BUCKET_NAME/$S3_BUCKET_PATH/ | while read -r line; | |
do | |
createDate=`echo $line|awk {'print $1" "$2'}` | |
createDate=`date -d"$createDate" +%s` | |
olderThan=`date -d"$RM_OLD_FILES" +%s` | |
if [ $createDate -lt $olderThan ] | |
then | |
fileName=`echo $line|awk {'print $4'}` | |
echo $fileName | |
if [ $fileName != "" ] | |
then | |
echo removendo aquivos | |
echo $filename | |
s3cmd del "$fileName" | |
fi | |
fi | |
done; | |
exit $EXITCODE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment