创建mongodb备份和还原用户并添加定时备份mongodb脚本

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

你可能感兴趣的:(mongodb,服务器技术)