mongodb 全量备份和恢复

./mongodump --host 127.0.0.1 --port 27017 -d test_database --out /usr/mongodb/backup
./mongorestore --host 127.0.0.1 --port 27017 -d test_database /usr/mongodb/backup/test_database

 带权限的

./mongorestore --host 127.0.0.1 --port 27017 -u root -p root --authenticationDatabase database /usr/mongodb/backup/test_database

PS:Linux上shell写的定时备份脚步

转自:http://blog.csdn.net/yuan882696yan/article/details/41523339

       http://www.cnblogs.com/tangnie/p/3148782.html

#!/bin/bash
sourcepath='/app/mongodb-linux-x86_64-2.4.1'/bin
targetpath='/backup/mongobak'
nowtime=$(date +%Y%m%d)
 
start()
{
  ${sourcepath}/mongodump --host 127.0.0.1 --port 27017 --out ${targetpath}/${nowtime}
}
execute()
{
  start
  if [ $? -eq 0 ]
  then
    echo "back successfully!"
  else
    echo "back failure!"
  fi
}
 
if [ ! -d "${targetpath}/${nowtime}/" ]
then
 mkdir ${targetpath}/${nowtime}
fi
execute
echo "============== back end ${nowtime} =============="

 定时清除,保留7天的纪录

#!/bin/bash
targetpath='/backup/mongobak'
nowtime=$(date -d '-7 days' "+%Y%m%d")
if [ -d "${targetpath}/${nowtime}/" ]
then
  rm -rf "${targetpath}/${nowtime}/"
  echo "=======${targetpath}/${nowtime}/===删除完毕=="
fi
echo "===$nowtime ==="

 

写完以上shell,

对shell赋权(chmod +x mongobak.sh)

 

准备执行时,遇到了错误:

syntax error near unexpected token '

http://jingyan.baidu.com/article/9f63fb91d014b8c8410f0e7a.html

 

crontab -e

10 04 * * * /shell/mongobak 1>/log/crontab_mongo_back.file &
10 02 * * * /shell/mongobakdelete 1>/log/crontab_mongo_delete.file &

 

posted @ 2018-02-02 10:53  小小鬼  阅读(522)  评论(0编辑  收藏  举报