本文共 2282 字,大约阅读时间需要 7 分钟。
目标:把一台linux机上mongodb的数据定时备份到另一台机上:
过程:
一开始打算使用mongoexport和mongoimport,但是总是会报“\x00”字符串不能识别的问题,后来就改成了mongodump和mongorestart,成功实现目标。
(\x00的问题还在寻找解决办法);
脚本代码:
#获取系统时间并转换成毫秒数current=`date "+%Y-%m-%d %H:%M:%S"`;timeStamp=`date -d "$current" +%s`;#当前时间的毫秒数currentTimeStamp=$((timeStamp*1000));#一个小时前这个时候的毫秒数yestodayTimeStamp=$((currentTimeStamp-43*60*60*1000));echo $current;echo $timeStamp;echo $currentTimeStamp;echo $yestodayTimeStamp;echo "File not exist!dump `date "+%Y-%m-%d %H:%M:%S"` $currentTimeStamp $yestodayTimeStamp" >>/home/admin/testlog.log#备份新产生表/home/admin/mongodb305/mongodb305/bin/mongodump --port 47017 -u admin -p admin -d admin -c alarm -q '{"recTime":{$gte:'$yestodayTimeStamp',$lt:'$currentTimeStamp'}}' -o /home/admin/dumptest;/home/admin/mongodb305/mongodb305/bin/mongodump --port 47017 -u admin -p admin -d admin -c filtering_alarm -q '{"recTime":{$gte:'$yestodayTimeStamp',$lt:'$currentTimeStamp'}}' -o /home/admin/dumptest;/home/admin/mongodb305/mongodb305/bin/mongodump --port 47017 -u admin -p admin -d admin -c combine_alarm -q '{"recTime":{$gte:'$yestodayTimeStamp',$lt:'$currentTimeStamp'}}' -o /home/admin/dumptest;/home/admin/mongodb305/mongodb305/bin/mongodump --port 47017 -u admin -p admin -d admin -c processed_alarm -q '{"recTime":{$gte:'$yestodayTimeStamp',$lt:'$currentTimeStamp'}}' -o /home/admin/dumptest/home/admin/mongodb305/mongodb305/bin/mongorestore -h 192.168.0.213 --port 27017 -u admin -p admin -d admin -c alarm --keepIndexVersion --maintainInsertionOrder /home/admin/dumptest/admin/alarm.bson;/home/admin/mongodb305/mongodb305/bin/mongorestore -h 192.168.0.213 --port 27017 -u admin -p admin -d admin -c filtering_alarm --keepIndexVersion --maintainInsertionOrder /home/admin/dumptest/admin/filtering_alarm.bson;/home/admin/mongodb305/mongodb305/bin/mongorestore -h 192.168.0.213 --port 27017 -u admin -p admin -d admin -c combine_alarm --keepIndexVersion --maintainInsertionOrder /home/admin/dumptest/admin/combine_alarm.bson;/home/admin/mongodb305/mongodb305/bin/mongorestore -h 192.168.0.213 --port 27017 -u admin -p admin -d admin -c processed_alarm --keepIndexVersion --maintainInsertionOrder /home/admin/dumptest/admin/processed_alarm.bsoncrontab定时任务代码:
39 10 * * * root /home/admin/dumptest2.sh备份之后根据条件查看两个mongodb中的数据一致,备份成功。
转载地址:http://nxmra.baihongyu.com/