zi663227 发表于 2015-7-8 11:23:57

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()
{
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} =============="

  2.定时清除,保留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 ==="

  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




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

  每天凌晨4点10开始进行备份, 2点10分删除旧的备份
页: [1]
查看完整版本: Mongodb定时备份脚本和清除脚本