16 lines
2.0 KiB
Python
16 lines
2.0 KiB
Python
from pathlib import Path
|
|
import subprocess,json,hashlib,shutil
|
|
out=Path(__file__).parent.resolve();private=Path('C:/Users/macbook/.config/investmoz/navegacao-consolidacao-QA-20261016');qa=private/'source';db='navigation_consolidation_20261016_test';restore='navigation_consolidation_restore_20261016_test';pg=Path('C:/Program Files/PostgreSQL/17/bin')
|
|
assert db.endswith('_test')and restore.endswith('_test')and db!=restore
|
|
def run(name,*args):
|
|
r=subprocess.run([str(pg/name),'-h','127.0.0.1','-p','55441','-U','postgres',*args],capture_output=True,text=True,encoding='utf-8');assert r.returncode==0,r.stderr;return r.stdout
|
|
dump=private/'after-use-QA.dump';run('pg_dump.exe','-Fc','--no-owner','--no-acl','-f',str(dump),db)
|
|
exists=run('psql.exe','-d','postgres','-At','-c',"SELECT count(*) FROM pg_database WHERE datname='"+restore+"'").strip();assert exists=='0','Restore clone already exists; never replace automatically'
|
|
run('createdb.exe',restore);run('pg_restore.exe','--no-owner','--no-acl','-d',restore,str(dump))
|
|
boot=(out/'boot-QA.php').read_text('utf-8').replace("if($v!==null){putenv", "if($v!==null){if($k==='DB_DATABASE')$v='"+restore+"';putenv").replace("$c['database']!=='"+db+"'", "$c['database']!=='"+restore+"'")
|
|
(out/'boot-restored-QA.php').write_text(boot,'utf-8')
|
|
store=private/'restored-storage';shutil.copytree(qa/'storage/app/private',store);sha=lambda p:hashlib.sha256(p.read_bytes()).hexdigest();files=[p for p in(qa/'storage/app/private').rglob('*')if p.is_file()]
|
|
for f in files:assert sha(f)==sha(store/f.relative_to(qa/'storage/app/private'))
|
|
(out/'RESTORE-QA.json').write_text(json.dumps({'source':db+':55441','restored':restore+':55441','dump_sha256':sha(dump),'dump_bytes':dump.stat().st_size,'private_files_restored_and_verified':len(files),'active_backup_performed':False,'scope':'Restauro do clone QA após utilização; o backup/restauro fresco do destino ativo continua para uma execução futura autorizada.'},ensure_ascii=False,indent=2)+'\n','utf-8')
|
|
print('Restored after-use QA clone and private files without any active database connection')
|