mongodb备份还原单表
来源:互联网 发布:python 并行编程 编辑:程序博客网 时间:2024/06/04 19:11
mongodb备份整个数据库
mongodump -h 192.168.10.111--port 27017 -u username -p passwd -d wechat -o wechat
还原这个数据库
mongorestore --host 192.168.10.111 -u username -p password --authenticationDatabase admin -d wechat --dir=/home/server/backup/mongodb/wechat
如果要删除原本的库再还原
mongorestore --host 192.168.10.111 -u username -p password --authenticationDatabase admin -d wechat --dir=/home/server/backup/mongodb/wechat --drop
mongo备份单个表
mongoexport --host 10.9.0.1 --port 27017 -u mongobf -p 03a4b868ff -d wechat -c article -o article
根据时间段备份表
mongo时间戳转换
date -d 2016-06-10 +%s 或date -d "2016-06-10 00:00:00" +%s //输出:1465488000
date -d @1465488000 //输出:2016年 06月 10日 星期五 00:00:00 CST
db.article.find({"lastpopTime":{"$gte":1465488000}}).count() //统计2016-06-10之后的数据
查询某个时间段的数据
db.article.find({"lastpopTime":{"$gte":1465488000,"lt":1470466800}}).count()
备份wechat数据库article表中2016-06-10之后的数据
mongodump --host 10.9.0.1 --port 27017 -u username -p userpasswd -d wechat -c article -q '{"lastpopTime":{"$gte":1465488000}}' -o 6-10
还原数据库中某个表
mongorestore -d wechat -c media 6-10/wechat/article.bson --drop //还原bson格式数据
增量某个表的数据:
mongorestore -d wechat -c article 8-6/wechat/article.bson
通过Objectid备份数据
方法一:
mongodb shell中执行以下函数
function objectIdWithTimestamp(timestamp) {
if (typeof(timestamp) == 'string') {
timestamp = new Date(timestamp);
}
var hexSeconds = Math.floor(timestamp/1000).toString(16);
var constructedObjectId = ObjectId(hexSeconds + "0000000000000000");
return constructedObjectId
}
查询2016/08/01号之后新入库的数据,或objectIdWithTimestamp('2016/08/01 00:00:00')
调用上面的函数:objectIdWithTimestamp转换成时间戳
db.collectionName.find({_id:{$gte:objectIdWithTimestamp('2016/08/01')}})
collectionName为集合名
如查询article集合中2016/08/01号之后新入库的数据
db.article.find({_id:{$gte:objectIdWithTimestamp('2016/08/01')}})
输出Objectid
objectIdWithTimestamp('2016/08/01')或
print(objectIdWithTimestamp('2016/08/01'))
使用Objectid备份数据
mongodump --host 10.9.0.1 --port 27017 -u username -p userpasswd -d wechat -c article -q '{"_id":{"$gte":ObjectId("579e20800000000000000000")}' -o 8-01
方法二:
objectid前4个字节表示数据入库的时间,即前8位。前4个字节为16进制数
我们要查询2016-08-01号新入库的数据
首先:把日期变成时间戳
#date -d "2016-08-01" +%s
1469980800
然后:把时间戳变成16进制
#echo 'obase=16;ibase=10;1469980800'|bc
579E2080 //这个就是objectid的前8位了,在后面添上000000 0000 000000即为obiectid
ObjectId("579e20800000000000000000")
16进制转10进制
#echo $((0x579E2080))
1469980800
mongodump -h 192.168.10.111--port 27017 -u username -p passwd -d wechat -o wechat
还原这个数据库
mongorestore --host 192.168.10.111 -u username -p password --authenticationDatabase admin -d wechat --dir=/home/server/backup/mongodb/wechat
如果要删除原本的库再还原
mongorestore --host 192.168.10.111 -u username -p password --authenticationDatabase admin -d wechat --dir=/home/server/backup/mongodb/wechat --drop
mongo备份单个表
mongoexport --host 10.9.0.1 --port 27017 -u mongobf -p 03a4b868ff -d wechat -c article -o article
根据时间段备份表
mongo时间戳转换
date -d 2016-06-10 +%s 或date -d "2016-06-10 00:00:00" +%s //输出:1465488000
date -d @1465488000 //输出:2016年 06月 10日 星期五 00:00:00 CST
db.article.find({"lastpopTime":{"$gte":1465488000}}).count() //统计2016-06-10之后的数据
查询某个时间段的数据
db.article.find({"lastpopTime":{"$gte":1465488000,"lt":1470466800}}).count()
备份wechat数据库article表中2016-06-10之后的数据
mongodump --host 10.9.0.1 --port 27017 -u username -p userpasswd -d wechat -c article -q '{"lastpopTime":{"$gte":1465488000}}' -o 6-10
还原数据库中某个表
mongorestore -d wechat -c media 6-10/wechat/article.bson --drop //还原bson格式数据
增量某个表的数据:
mongorestore -d wechat -c article 8-6/wechat/article.bson
通过Objectid备份数据
方法一:
mongodb shell中执行以下函数
function objectIdWithTimestamp(timestamp) {
if (typeof(timestamp) == 'string') {
timestamp = new Date(timestamp);
}
var hexSeconds = Math.floor(timestamp/1000).toString(16);
var constructedObjectId = ObjectId(hexSeconds + "0000000000000000");
return constructedObjectId
}
查询2016/08/01号之后新入库的数据,或objectIdWithTimestamp('2016/08/01 00:00:00')
调用上面的函数:objectIdWithTimestamp转换成时间戳
db.collectionName.find({_id:{$gte:objectIdWithTimestamp('2016/08/01')}})
collectionName为集合名
如查询article集合中2016/08/01号之后新入库的数据
db.article.find({_id:{$gte:objectIdWithTimestamp('2016/08/01')}})
输出Objectid
objectIdWithTimestamp('2016/08/01')或
print(objectIdWithTimestamp('2016/08/01'))
使用Objectid备份数据
mongodump --host 10.9.0.1 --port 27017 -u username -p userpasswd -d wechat -c article -q '{"_id":{"$gte":ObjectId("579e20800000000000000000")}' -o 8-01
方法二:
objectid前4个字节表示数据入库的时间,即前8位。前4个字节为16进制数
我们要查询2016-08-01号新入库的数据
首先:把日期变成时间戳
#date -d "2016-08-01" +%s
1469980800
然后:把时间戳变成16进制
#echo 'obase=16;ibase=10;1469980800'|bc
579E2080 //这个就是objectid的前8位了,在后面添上000000 0000 000000即为obiectid
ObjectId("579e20800000000000000000")
16进制转10进制
#echo $((0x579E2080))
1469980800
阅读全文
0 0
- mongodb备份还原单表
- MongoDB之整库备份还原单表collection备份还原
- mongodb备份及还原
- mongodb备份和还原
- Mongodb 备份迁移 还原
- MongoDB 备份与还原
- mongodb 备份 还原
- MongoDB 备份/还原
- Mongodb备份和还原
- 安装、备份、还原mongodb
- mongodb备份和还原
- mongodb 备份还原
- MongoDB备份还原
- mongodb备份和还原
- MongoDB备份和还原备份
- mongodb 的备份与还原
- mongodb的备份还原案例
- mongodb的备份与还原
- Replugin使用
- DNS原理
- Jump Game II
- Search in Rotated Sorted Array II:带重复与转折的升序数列搜索
- 导航中的robot footprint和各种radius
- mongodb备份还原单表
- 视频物体分割--One-Shot Video Object Segmentation 2017年cvpr
- 数据结构(C语言)线性表 顺序存储
- 【转】数据库基本知识:(七)函数和表达式的使用
- 随机森林 Random Forest
- Navicat premium连接Oracle中遇到的一些问题
- leetcode 661 Image Smoother
- android:代码中动态添加子布局
- eclipse 常用的快捷键