Mongodb定时备份脚本和清除脚本
生活随笔
收集整理的這篇文章主要介紹了
Mongodb定时备份脚本和清除脚本
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Mongodb用的是可以熱備份的mongodump和對應恢復的mongorestore,在linux下面使用shell腳本寫的定時備份,代碼如下
1.定時備份
#!/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() {startif [ $? -eq 0 ]thenecho "back successfully!"elseecho "back failure!"fi }if [ ! -d "${targetpath}/${nowtime}/" ] thenmkdir ${targetpath}/${nowtime} fi execute echo "============== back end ${nowtime} =============="?2.定時清除,保留7天的紀錄
#!/bin/bash targetpath='/backup/mongobak' nowtime=$(date -d '-7 days' "+%Y%m%d") if [ -d "${targetpath}/${nowtime}/" ] thenrm -rf "${targetpath}/${nowtime}/"echo "=======${targetpath}/${nowtime}/===刪除完畢==" fi echo "===$nowtime ==="?3.服務器的時間要同步,同步的方法
微軟公司授時主機(美國)???? time.windows.com
臺警大授時中心(臺灣)??????? asia.pool.ntp.org
中科院授時中心(西安)??????? 210.72.145.44
網通授時中心(北京)?????????? 219.158.14.130
調用同步:? ntpdate asia.pool.ntp.org
4.設置上面腳本權限和定時任務
權限:chmod 777 file
定時任務:crontab -e?
?每天凌晨4點10開始進行備份, 2點10分刪除舊的備份
轉載于:https://www.cnblogs.com/tangnie/p/3148782.html
總結
以上是生活随笔為你收集整理的Mongodb定时备份脚本和清除脚本的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用GNOME桌面工具管理Linux(4
- 下一篇: 动态列表