#!/bin/bash
#backup mongoDB
#option
day=$(date +%Y_%m_%d)
delDay=$(date -d '1 day ago' +%Y_%m_%d)
delMon=$(date -d '1 month ago' +%Y_%m_%d)
containerID='44d320454502'
#backup in backup
dcDelFile="/backup/mongo/${delDay}.sql"
dcBackFile="/backup/mongo/${day}.sql"
docker exec ${containerID} rm -rf ${dcDelFile} > /dev/null
docker exec ${containerID} mongodump -o ${dcBackFile} > /dev/null
#backup in server
delFile="/backup/mongo/${delMon}.tar.gz"
backFile="/backup/mongo/${day}.sql"
rm -rf ${delFile}
docker cp ${containerID}:${backFile} ${backFile}
cd /backup/mongo && tar -czf ${day}.tar.gz ./${day}.sql && rm -rf ./${day}.sql
echo "success"