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
不过多介绍,请自行搜索。