mongdb遭遇勒索,用备份进行数据恢复
mongdb遭遇勒索,用备份进行数据恢复
1、背景:
某台MongoDB服务器,没有配置用户名密码,放到公网不到一天,遭到删库勒索
All your data is a backed up. You must pay 0.05 BTC to 1K4DdqZ7sWUkhtfRqAGi7VcRck3itcNH17 48 hours for recover it. After 48 hours expiration we will sell all your data on dark markets and the database dump will be dropped from our server!You can buy bitcoin here, https://localbitcoins.com or https://buy.moonpay.io/ After paying write to us in the mail with your DB IP: rambler+1kgup@onionmail.org and you will receive a link to download your database dump.
2、查找是否有备份,发现存在JSON格式数据备份
[
{
"_id": "5d17452d5548b118d86ee882",
"A_id": "636974317259022022",
"ArtCard_MaxSize": "0",
"AuthorName": "xxx作者",
"Create_time": "29/6/2019 11:02:05.902",
"Email": "aaaa@163.com",
"EndDate": "21/12/2025 16:00:00",
"Goods_footer_img": "/upload/xxx.jpg",
"Goods_header_img": "/upload/xxx.jpg",
"StartDate": "21/12/2019 16:00:00",
"Userhead": "/upload/xxx.jpg",
"Username": "aaaa",
},
{
"_id": "5f4b40ac7341f020cb5ebfa8",
"A_id": "637343928761936160",
"ArtCard_MaxSize": "0",
"AuthorName": "xxx作者",
"Create_time": "29/6/2019 11:02:05.902",
"Email": "aaaa@163.com",
"EndDate": "21/12/2025 16:00:00",
"Goods_footer_img": "/upload/xxx.jpg",
"Goods_header_img": "/upload/xxx.jpg",
"StartDate": "21/12/2019 16:00:00",
"Userhead": "/upload/xxx.jpg",
"Username": "aaaa",
},
3.、尝试用“mongoimport”导入数据:
mongoimport --uri="mongodb://user:user_pwd@127.0.0.1:27017/DBManage" -c "userInfo" --file="C:\userInfo.json"
报错如下:
Failed: error unmarshaling bytes on document #0: JSON decoder out of sync
原因为,导入JSON为数组,需要添加参数“mongoimport --jsonArray”
再次导入:
mongoimport --uri="mongodb://user:user_pwd@127.0.0.1:27017/DBManage" -c "userInfo" --jsonArray --file="C:\userInfo.json"
导入成功,但发现程序不能跑,查看数据,发现日期格式并未导入成预期格式:
预期的格式
{
"A_id": NumberLong(636974317259022022),
"Create_time" : ISODate("2019-06-24T16:27:14.537Z"),
"StartDate" : ISODate("2019-06-24T16:00:00.537Z"),
"EndDate" : ISODate("2019-06-24T16:00:00.537Z"),
}
数据库里的格式
{
"A_id": "636974317259022022",
"Create_time" : "24/6/2019 16:27:14.537",
"StartDate": "21/12/2019 16:00:00",
"EndDate": "21/12/2025 16:00:00",
}
4、PyCharm开起来,修正数据格式【Date/Long/Array】,并插入数据库:
import json
import datetime
import pymongo
#备份JSON文件所在目录
basePath = "C:\JSON"
myclient = pymongo.MongoClient("mongodb://127.0.0.1:27017")
mydb = myclient["DBManage"]
tabAdmin_userInfo = mydb["userInfo_Test"]
strJson = ""
with open(basePath + '\\userInfo.json', encoding='utf8') as file:
strJson = file.read()
str_json = str(strJson)
jsonList = json.loads(str_json)
newJsonList = []
for ite in jsonList:
for ke in ite.keys():
# String转成ISO Date
if ke in ["Create_time", "StartDate", "EndDate","Sale_time"] and ite[ke] is not None:
dateTimeValue = ite[ke]
dateFor = None
# "24/6/2019 16:27:14.537" 格式转换成 ISODate("2019-06-24T16:27:14.537Z")
if "." in dateTimeValue:
dateFor = datetime.datetime.strptime(dateTimeValue, '%d/%m/%Y %H:%M:%S.%f')
else:
dateFor = datetime.datetime.strptime(dateTimeValue, '%d/%m/%Y %H:%M:%S')
print(dateTimeValue, dateFor, dateFor.time())
ite[ke] =dateFor
# String转成Long ,python3 后long和int 都为int()转换
if ke in ["A_id", "C_id", "New_id", "P_id","Join_userId"]:
ite[ke] = int( ite[ke])
# String转成ISO Date
if ke in ["UserType"]:
#"636974317259022022" 转换成 NumberLong(636974317259022022)
ite[ke]=int(ite[ke])
# String转成对象
if ke in ["Goods_Detail_image"]:
ite[ke]=json.loads(ite[ke])
print(ite)
#按条插入数据库
insREs = tabAdmin_userInfo.insert_one(ite)
print(insREs)
查看userInfo_Test表数据,并和userInfo 表数据对比,userInfo_Test 表数据达到预期要求;
删除userInfo表,将userInfo_Test表重命名为userInfo,测试应用
应用正常运行,此次应急完成