Last active
August 5, 2020 14:06
-
-
Save hatembentayeb/d10248f1576cd55a8c833c34b678a2bc to your computer and use it in GitHub Desktop.
effective mongodb backup using docker containers
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 | |
#maintainer : hatem ben tayeb <hatemtayeb2@gmail.com> | |
# usage : ./backup.sh <container name> <container network> | |
if [ ! -z $1 ] | |
then | |
declare log_path="/backups/backup.log" # make sure to create them manually or change it with your customized path | |
declare IP=$(docker inspect $1 | jq ".[].NetworkSettings.Networks.$2.IPAddress" -r) | |
ping -c2 $IP 2>/dev/null 1>/dev/null | |
if [ $? = 0 ] | |
then | |
docker run --rm --net=$2 mongo:latest mongodump --host $IP --archive --gzip | cat > /backups/dump_prod_$(date '+%d-%m-%Y_%H-%M').gz && \ | |
echo "$(date '+%d/%m/%Y %H:%M') [+] SUCCESS : backup created on /backups/dump_prod_$(date '+%d-%m-%Y_%H-%M').gz" >> $log_path \ | |
|| echo "$(date '+%d/%m/%Y %H:%M') [-] ERR : when trying to gather backup on $1 with ip address : $IP " >> $log_path | |
else | |
echo "$(date '+%d/%m/%Y %H:%M') [-] ERR : mongo container is down or wrong container name !" >> $log_path | |
fi | |
else | |
echo -e "\n[-] ERR: you must specify mongo container name !\n" | |
fi | |
# | |
#cat <gzip_file> | docker run --rm -i mongo mongorestore --host <mongo_ip> --archive --gzip --drop | |
# add it as a cronJob (EDITOR=vim crontab -e) : 0 3 */3 * * /root/backup.sh mongo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment