1.创建备份和还原所需要的用户
> use admin
> db.createUser(
{
user: "backup",
pwd: "backup_your_password",
roles: [ { role: "readWrite", db: "要备份的库名,如test" } ]
}
)
2.创建备份脚本:
#删除2天以前的备份
back_path=/mnt/data/mongobk
find ${back_path}/ -type d -name `date +"%Y%m%d" -d "-2 day"` | xargs rm -rf
date=`date "+%Y%m%d"`
nowtime=`date "+%H%M%S"`
mongodump=/mnt/local/mongodb/bin/mongodump
mkdir -p ${back_path}/${date}/${nowtime}
${mongodump} -h 127.0.0.1:20388 -d test -u backup -p "backup_your_password" --authenticationDatabase admin -o ${back_path}/${date}/${nowtime}/test.dmp
3.添加进cron任务
#crontab -e
#每两个小时备份mongodb库
0 */2 * * * sh /mnt/data/backup_mongo.sh
版权声明:本文为ligaofeng原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。