mongdb遭遇勒索,用備份進行數據恢復

mongdb遭遇勒索,用備份進行數據恢復

1、背景

某臺MongoDB服務器,沒有配置用戶名密碼,放到公網不到一天,遭到刪庫勒索

image

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: [email protected] 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": "[email protected]",
    "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": "[email protected]",
    "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:[email protected]: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:[email protected]: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,測試應用

應用正常運行,此次應急完成

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章