Add complete backup manifest and recovery instructions: restore.py
Browse files
snapshots/20261014-main-full/restore.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
import argparse,hashlib,json,os,subprocess,tempfile
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
|
| 5 |
+
def digest(p):
|
| 6 |
+
with p.open('rb') as f:return hashlib.file_digest(f,'sha256').hexdigest()
|
| 7 |
+
|
| 8 |
+
def main():
|
| 9 |
+
parser=argparse.ArgumentParser(description='Restore this complete RSEBench backup into an empty directory.')
|
| 10 |
+
parser.add_argument('destination',type=Path);parser.add_argument('--key-file',type=Path,required=True);args=parser.parse_args()
|
| 11 |
+
here=Path(__file__).resolve().parent;manifest=json.loads((here/'MANIFEST.json').read_text())
|
| 12 |
+
dest=args.destination.resolve();dest.mkdir(parents=True,exist_ok=True)
|
| 13 |
+
if any(dest.iterdir()):raise SystemExit('Destination must be empty.')
|
| 14 |
+
if not args.key_file.is_file():raise SystemExit('Recovery key file is missing.')
|
| 15 |
+
# Verify all downloaded volumes before extracting anything.
|
| 16 |
+
for volume in manifest['volumes']:
|
| 17 |
+
path=here/volume['name']
|
| 18 |
+
if not path.is_file() or path.stat().st_size!=volume['bytes'] or digest(path)!=volume['sha256']:
|
| 19 |
+
raise SystemExit('Missing or damaged volume: '+volume['name'])
|
| 20 |
+
for volume in manifest['volumes']:
|
| 21 |
+
path=here/volume['name']
|
| 22 |
+
print('Restoring',volume['name'],flush=True)
|
| 23 |
+
if volume['encrypted']:
|
| 24 |
+
with tempfile.TemporaryDirectory(prefix='rsebench-restore-',dir=dest) as tmp:
|
| 25 |
+
plain=Path(tmp)/volume['plain_archive_name'];home=Path(tmp)/'gnupg';home.mkdir(mode=0o700)
|
| 26 |
+
subprocess.run(['gpg','--homedir',str(home),'--batch','--yes','--pinentry-mode','loopback','--passphrase-file',str(args.key_file.resolve()),'--output',str(plain),'--decrypt',str(path)],check=True)
|
| 27 |
+
if digest(plain)!=volume['plain_archive_sha256']:raise SystemExit('Decrypted checksum mismatch: '+volume['name'])
|
| 28 |
+
subprocess.run(['tar','-I','zstd','-xf',str(plain),'-C',str(dest)],check=True)
|
| 29 |
+
else:
|
| 30 |
+
if volume['sha256']!=volume['plain_archive_sha256']:raise SystemExit('Invalid plain archive checksum')
|
| 31 |
+
subprocess.run(['tar','-I','zstd','-xf',str(path),'-C',str(dest)],check=True)
|
| 32 |
+
print('Restored:',dest/manifest['project'])
|
| 33 |
+
if __name__=='__main__':main()
|