z230
This commit is contained in:
28
recovery.py
28
recovery.py
@@ -11,8 +11,8 @@ the original directory structure.
|
||||
|
||||
import os
|
||||
import sys
|
||||
import shutil
|
||||
from indexer.config import DB_CONFIG, BACKUP_PATH
|
||||
import pyzipper
|
||||
from indexer.config import BACKUP_PATH, BACKUP_PASSWORD
|
||||
from indexer.db import get_connection
|
||||
from indexer.backup import blob_path
|
||||
|
||||
@@ -37,21 +37,37 @@ def recover(run_id: int, output_dir: str):
|
||||
print(f"Recovering {len(rows)} files from run #{run_id} to {output_dir}")
|
||||
recovered = 0
|
||||
missing = 0
|
||||
password = BACKUP_PASSWORD.encode("utf-8")
|
||||
|
||||
for relative_path, content_hash in rows:
|
||||
source = blob_path(BACKUP_PATH, content_hash)
|
||||
target = os.path.join(output_dir, relative_path.replace("/", os.sep))
|
||||
|
||||
if not os.path.exists(source):
|
||||
print(f" MISSING blob: {content_hash.hex()} for {relative_path}")
|
||||
print(f" MISSING zip: {content_hash.hex()} for {relative_path}")
|
||||
missing += 1
|
||||
continue
|
||||
|
||||
os.makedirs(os.path.dirname(target), exist_ok=True)
|
||||
shutil.copy2(source, target)
|
||||
recovered += 1
|
||||
|
||||
print(f"\nRecovered: {recovered} Missing blobs: {missing}")
|
||||
try:
|
||||
with pyzipper.AESZipFile(source, "r") as zf:
|
||||
zf.setpassword(password)
|
||||
names = zf.namelist()
|
||||
if not names:
|
||||
print(f" WARN: empty zip: {source}")
|
||||
missing += 1
|
||||
continue
|
||||
data = zf.read(names[0])
|
||||
with open(target, "wb") as f:
|
||||
f.write(data)
|
||||
recovered += 1
|
||||
except Exception as e:
|
||||
print(f" ERROR extracting {source} for {relative_path}: {e}")
|
||||
missing += 1
|
||||
continue
|
||||
|
||||
print(f"\nRecovered: {recovered} Missing/errors: {missing}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user