Mongodb自动备份脚本
备份
#!/bin/bash
sourcepath='/data/mongo/mongodb'/bin
targetpath='/data/mongo/mongobackups'
nowtime=$(date +%Y%m%d)
start()
{
${sourcepath}/mongodump -h 127.0.0.1:32762 -d test -o ${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} =============="
删除30天之前的备份
#!/bin/bash
targetpath='/data/mongo/mongobackups'
nowtime=$(date -d '-30 days' "+%Y%m%d")
if [ -d "${targetpath}/${nowtime}/" ]
then
rm -rf "${targetpath}/${nowtime}/"
echo "=======${targetpath}/${nowtime}/===?~H| ?~Y??~L?~U=="
fi
echo "===$nowtime ==="
创建定时任务
服务器的时间要同步,同步的方法
微软公司授时主机(美国) time.windows.com
台警大授时中心(台湾) asia.pool.ntp.org
中科院授时中心(西安) 210.72.145.44
网通授时中心(北京) 219.158.14.130
调用同步: ntpdate asia.pool.ntp.org
权限:chmod 777 file
crontab -e
10 04 * * * /data/shell/mongobak 1>/data/log/crontab_mongo_back.file &
10 02 * * * /shell/mongobakdelete 1>/data/log/crontab_mongo_delete.file &