linux下每天定时备份mongo

来源:互联网 发布:初学者买哪款单片机好 编辑:程序博客网 时间:2024/06/07 10:18

linux下每天定时备份mongo,经过了一番折腾终于完成了


  1. 写shell脚本

1.1 定时备份

#!/bin/bashsourcepath='/app/mongodb-linux-x86_64-2.4.1'/bintargetpath='/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}fiexecuteecho "============== back end ${nowtime} =============="
1.2 定时清除,保留7天的纪录

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

以上代码参考:http://www.cnblogs.com/tangnie/p/3148782.html


写完以上shell,

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


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

syntax error near unexpected token '

我很想跟踪下,看看shell执行的每一步都有什么结果,但不太懂shell,于是乎baidu,

还真找到一篇文章,顿时兴奋了,

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

看了上面的文章,觉得notepad++,居然还有这作用,点赞!!!


至此,shell的编写和执行完成了,工作完成了60%。


2. 写crontab表达式

2.1 执行crontab -e

2.2 按i键,进入编辑模式。

       写表达式

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

写完后,却不知如何保存并退出了,凭着以前的经验,

按了下ctrl+c,果然退出了编辑模式,又baidu,

http://www.linuxidc.com/Linux/2011-05/35644.htm


参考上面的文章,

又按了 :x,保存并退出了crontab   !!!


涉及到的知识很多啊,一波好几折。


0 0
原创粉丝点击