基于docker的mongodump / mongorestore 备份恢复

一、下载mongodump镜像

docker pull  mongo:latest

二、备份mongodb所有的数据

#!/bin/bash
source /etc/profile

#定义脚本变量
mongo_user=root
mongo_password=xxxxxxxx
mongo_host=192.168.11.210:27017
backup_path=/data/mongo_backup/data
backup_time=`date +%Y%m%d_%H%M%S`

#改变备份目录的权限(写入)
chmod 777 $backup_path

#执行备份
docker run --rm -i \
--network host \
-v $backup_path:$backup_path \
--name mongodump  mongo:latest \
mongodump --uri mongodb://$mongo_user:$mongo_password@$mongo_host -o $backup_path/$backup_time

三、恢复mongodb数据

#!/bin/bash
#定义脚本变量
mongo_user=root
mongo_password=xxxxxxxx
mongo_host=192.168.11.210:27017
backup_path=/data/mongo_backup/data
#恢复的目录
#使用方法:  bash -x restore_mongo.sh 20201027_172549
backup_time=$1

#恢复dbs,除了admin
dbs=`ls -1 $backup_path/$1|grep -v admin`

for dbname in $dbs
do
   docker run --rm -i \
   --network host \
   -v $backup_path:$backup_path \
   --name mongorestore  mongo:latest \
   mongorestore -d $dbname  $backup_path/$1/$dbname  --uri mongodb://$mongo_user:$mongo_password@$mongo_host \
   --authenticationDatabase admin --drop
done

四、crontab自动备份(每天0:30分进行备份)

#mongodb自动备份
30 0  * * *  /bin/bash  /data/mongo_backup/backup_mongo.sh


©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页