从 LevelDB“.ldb”文件恢复数据

问题描述 投票:0回答:3

我正在尝试从 .ldb 文件中提取数据。 Chrome 扩展 OneTab 出现故障,我正在尝试恢复它保存的链接。我相信我已经从一篇旧博客文章中找到了解决方案,但我对编码了解不够,无法弄清楚如何遵循。

以下是博客文章的链接: https://antimatter15.com/2015/12/recovering-deleted-data-from-leveldb/

我相信我已经按照他的建议正确完成了构建 Go 应用程序的所有操作。它创建了一个名为“ldbdump”的文件,没有文件扩展名。下一步是我遇到麻烦的地方。我尝试在 Jupyter Notebook 中运行他的 Python 代码(这是我唯一使用过 Python 的软件,而且经验有限),但只会出现错误。

我正在使用的原始代码可以在上面的“恢复”标题下的链接中找到。我将“base”的定义从“test-stuff copy”更改为指向包含我尝试读取的 .ldb 文件以及“ldbdump”文件的文件夹。出现错误后,我还更改了底部打印命令的语法。其他一切都保持原样。

base = "D:\\Downloads\\ldb archive"

import os
from subprocess import Popen, PIPE
import json
import ast

for f in os.listdir(base):
  if f.endswith(".ldb"):
    process = Popen(["ldbdump", os.path.join(base, f)], stdout=PIPE)
    (output, err) = process.communicate()
    exit_code = process.wait()
    for line in (output.split("\n")[1:]):
      if line.strip() == "": continue
      parsed = ast.literal_eval("{" + line + "}")
      key = parsed.keys()[0]
      print(json.dumps({ "key": key.encode('string-escape'), "value": parsed[key] }))

如果我正确理解这篇博客文章,这应该在将 .ldb 文件的内容转换为 JSON 文件后打印其内容(尽管我不确定该 JSON 文件将保存在哪里)。之后,我可以继续下一步,清理结果以使其更具可读性。相反,我收到一个错误。我不知道我做错了什么,因为我一开始几乎不知道自己在做什么。我真正理解的是顶部的“文件未找到”。看起来像这样:

FileNotFoundError                         Traceback (most recent call last)
<ipython-input-2-26ff29e32d63> in <module>
      1 for f in os.listdir(base):
      2   if f.endswith(".ldb"):
----> 3     process = Popen(["ldbdump", os.path.join(base, f)], stdout=PIPE)
      4     (output, err) = process.communicate()
      5     exit_code = process.wait()

~\Anaconda3\lib\subprocess.py in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds, encoding, errors, text)
    767                                 c2pread, c2pwrite,
    768                                 errread, errwrite,
--> 769                                 restore_signals, start_new_session)
    770         except:
    771             # Cleanup if the child failed starting.

~\Anaconda3\lib\subprocess.py in _execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, unused_restore_signals, unused_start_new_session)
   1170                                          env,
   1171                                          os.fspath(cwd) if cwd is not None else None,
-> 1172                                          startupinfo)
   1173             finally:
   1174                 # Child is launched. Close the parent's copy of those pipe

FileNotFoundError: [WinError 2] The system cannot find the file specified

就像我说的,我不确定我做错了什么。我最好的猜测是,我要么没有正确修改博客文章的代码以将其指向我的电脑上的文件,要么原始代码错误地假设了有关我的机器的某些信息(意味着我有错误的操作系统,我不应该运行它在笔记本中,我缺少一些依赖项等)

作为参考,我使用的是运行 Windows 10 的台式电脑,我尝试使用 Jupyter Notebook 5.7.4 运行此代码,除了上面代码部分中列出的包之外,我没有导入任何其他包,我几乎不知道自己在做什么。对不起。请帮忙。

python go leveldb
3个回答
4
投票

在我的研究中我添加了一些修复

import os
import subprocess
import json
import ast
for f in os.listdir(base):
  if f.endswith(".ldb"):
    process = subprocess.Popen(["ldbdump", os.path.join(base, f)], stdout=subprocess.PIPE, shell = True)
    (output, err) = process.communicate()
    exit_code = process.wait()
    for line in (output.split("\n")[1:]):
      if line.strip() == "": continue
      parsed = ast.literal_eval("{" + line + "}")
      key = parsed.keys()[0]
      print json.dumps({ "key": key.encode('string-escape'), "value": parsed[key] })

0
投票

有点晚了,但我遇到了同样的问题。解决方案实际上很简单:Go 在 Windows 下构建的

ldbdump
文件(无扩展名)是一个正确的 Windows 可执行文件。只需添加 .exe 扩展名,即可直接在 Windows 命令提示符中运行它,例如
ldbdump 000005.ldb
(ldbdump 文件必须具有 .exe 扩展名,否则它是未知命令)。它将输出 ldb 转储作为逗号分隔的键值对列表(纯文本)。您可以简单地将输出复制并粘贴到文本文件中,或者将输出通过管道传输到文件中,按照您想要的方式对其进行格式化等。

底线是:它是一个普通的 Windows 可执行文件,您不需要 python - 使用您喜欢的任何工具来处理输出或只是手动执行。


0
投票

当我尝试使用Python 3.11.3运行它时遇到一些错误,因此我修改了代码以打印ldb文件的内容并将其内容保存到ldbdump_output.txt

import os
import subprocess
import json
import ast

base = "D:\\Downloads\\ldb archive"
output_file_path = "D:\\Downloads\\ldb archive\\ldbdump_output.txt"

try:
    with open(output_file_path, 'w') as output_file:
        for f in os.listdir(base):
            if f.endswith(".ldb"):
                command = ["ldbdump", os.path.join(base, f)]
                try:
                    process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
                    output, err = process.communicate()
                    exit_code = process.wait()
                    if exit_code == 0:
                        output = output.decode('utf-8')
                        for line in output.split("\n")[1:]:
                            if line.strip() == "": continue
                            try:
                                parsed = ast.literal_eval("{" + line + "}")
                                key = next(iter(parsed.keys()))
                                output_str = json.dumps({"key": key, "value": parsed[key]})
                                print(output_str)
                                output_file.write(output_str + "\n")
                            except Exception as e:
                                error_message = f"Error processing line: {line}, Error: {str(e)}"
                                print(error_message)
                                output_file.write(error_message + "\n")
                    else:
                        error_message = f"Error executing command: {' '.join(command)}, Error: {err.decode('utf-8')}"
                        print(error_message)
                        output_file.write(error_message + "\n")
                except subprocess.CalledProcessError as e:
                    print(f"Subprocess error: {e}")
                except Exception as e:
                    print(f"Unexpected error during subprocess execution: {e}")
except IOError as e:
    print(f"IOError: Failed to open or write to file {output_file_path}: {e}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")
© www.soinside.com 2019 - 2024. All rights reserved.