MongoDB脚本备份

shell脚本直接采用mongodump进行备份,直接上代码:

#!/bin/bash
sourcepath='/usr/bin/'
destpath='./mongoback/'
nowtime=$(date +%Y%m%d)
databasename=Hello
collectionname=World
username=test
password=test

start()
{
        ${sourcepath}/mongodump --host 127.0.0.1 --port 27017 --db ${databasename} --collection ${collectionname} --out ${destpath}/${nowtime} -u ${username} -p ${password}
}

execute()
{
        start
        if [ $? -eq 0 ]
        then
                echo "back successfully"
        else
                echo "back failure"
        fi
}

if [! -d "${destpath}/${nowtime}/"]
then
        mkdir ${destpath}/${nowtime}
fi
execute
echo "==========back end ${nowtime}================="

注意,请采用unix格式保存代码上传,否则会出错。mongodump其他option可以自行添加。

另外,可以采用crontab命令来执行上述shell脚本,使得脚本可以定时执行。crontab不过多介绍,请自行搜索。

猜你喜欢

转载自blog.csdn.net/xl890727/article/details/79042220