#!/bin/bash #maintainer : hatem ben tayeb # usage : ./backup.sh 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 | docker run --rm -i mongo mongorestore --host --archive --gzip --drop # add it as a cronJob (EDITOR=vim crontab -e) : 0 3 */3 * * /root/backup.sh mongo