set -eu python3 - <<'PY' from pathlib import Path import subprocess,json,time,uuid,os assert os.environ.get('SSH_CONNECTION') stage=Path('/var/backups/investmoz-offers-preinstall-20261114/backup') receipt=json.loads((stage/'backup-receipt.json').read_text()) config=json.loads((stage/'delivery.json').read_text()) name='investmoz-preservation-restore-'+uuid.uuid4().hex[:10] def run(args,**kw):return subprocess.check_output(args,stderr=subprocess.STDOUT,**kw) run(['docker','image','inspect',config['postgis_image']]) # Reuse an existing pinned image; never pull software. cid=run(['docker','run','-d','--name',name,'--label','investmoz.task=offers-preinstall-20261114','--network','none','--memory','768m','--env','POSTGRES_HOST_AUTH_METHOD=trust','--env','POSTGRES_DB=offers_preinstall_restore_20261114_test',config['postgis_image'],'postgres','-p','55441']).decode().strip() assert len(cid)==64 try: for _ in range(40): if subprocess.run(['docker','exec',cid,'pg_isready','-p','55441','-U','postgres'],stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL).returncode==0:break time.sleep(1) else:raise RuntimeError('Restore database not ready') def sql(text):return run(['docker','exec','-i',cid,'psql','-p','55441','-U','postgres','-d','offers_preinstall_restore_20261114_test','-X','-q','-A','-t','-v','ON_ERROR_STOP=1'],input=text.encode()).decode() assert sql('select current_database(),inet_server_port(),current_setting(\'port\');').strip()=='offers_preinstall_restore_20261114_test||55441' # Preserve policy role names, without copying credentials or grants into the test instance. roles=run(['runuser','-u','postgres','--','psql','-XAt','-d','teste_investidor','-c',"select rolname from pg_roles where rolname !~ '^pg_' and rolname <> 'postgres'"]).decode().splitlines() for role in roles:sql('CREATE ROLE "'+role.replace('"','""')+'" NOLOGIN;') run(['docker','cp',str(stage/'database.dump'),cid+':/tmp/database.dump']) run(['docker','exec',cid,'pg_restore','-p','55441','-U','postgres','-d','offers_preinstall_restore_20261114_test','--no-owner','--no-privileges','--exit-on-error','/tmp/database.dump']) text="SET TIME ZONE 'UTC';\n" for table in receipt['tables']: quoted='.'.join('"'+part.replace('"','""')+'"' for part in table.split('.',1)) text+="select json_build_object('name','"+table+"','rows',count(*),'checksum',md5(coalesce(string_agg(row_to_json(t)::text,E'\\n' order by md5(row_to_json(t)::text) COLLATE \"C\"),''))) from "+quoted+" t;\n" restored={} for line in sql(text).splitlines(): row=json.loads(line);table=row.pop('name');restored[table]=row assert restored==receipt['tables'], 'Restore rows/checksums differ' # Extract regular files only into a new private directory, never into the active release. import tarfile,hashlib,shutil files=stage/'restore-files-rehearsal' files.mkdir(mode=0o700) recovered=0 with tarfile.open(stage/'files-before.tar.gz','r:gz') as archive: for member in archive: if not member.isfile():continue part=Path(member.name) assert not part.is_absolute() and '..' not in part.parts assert member.name in receipt['file_manifest'] destination=files/part;destination.parent.mkdir(mode=0o700,parents=True,exist_ok=True) with archive.extractfile(member) as source, destination.open('xb') as output:shutil.copyfileobj(source,output) expected=receipt['file_manifest'][member.name] assert destination.stat().st_size==expected['bytes'] and hashlib.sha256(destination.read_bytes()).hexdigest()==expected['sha256'] recovered+=1 assert recovered==receipt['file_count'] documents=json.loads(sql("select coalesce(json_agg(row_to_json(d)),'[]'::json) from (select storage_key,storage_encryption,sha256 from documents where deleted_at is null and storage_encryption='laravel' and storage_key is not null) d;").strip()) mfa=json.loads(sql("select coalesce(json_agg(row_to_json(u)),'[]'::json) from (select two_factor_secret,two_factor_recovery_codes from users where two_factor_secret is not null) u;").strip()) (files/'encrypted-records.json').write_text(json.dumps({'documents':documents,'mfa':mfa})) root=Path('/var/www/html/teste-investidor.tempo.co.mz/current').resolve() php=r"""make(Illuminate\Contracts\Console\Kernel::class)->bootstrap(); $files='/var/backups/investmoz-offers-preinstall-20261114/backup/restore-files-rehearsal'; $env=Dotenv\Dotenv::parse(file_get_contents($files.'/configuration/.env')); $keys=array_values(array_unique(array_filter([$env['APP_KEY']??null,...explode(',',$env['APP_PREVIOUS_KEYS']??'')]))); $encrypters=array_map(fn($key)=>new Illuminate\Encryption\Encrypter(str_starts_with($key,'base64:')?base64_decode(substr($key,7),true):$key,config('app.cipher')),$keys); if(!$encrypters)throw new RuntimeException('Preserved encryption configuration missing'); $decrypt=function($bytes)use($encrypters){foreach($encrypters as $encrypter){try{return $encrypter->decryptString($bytes);}catch(Throwable $error){}}throw new RuntimeException('Preserved envelope cannot be recovered');}; $records=json_decode(file_get_contents($files.'/encrypted-records.json'),true,512,JSON_THROW_ON_ERROR); $source=realpath(base_path('storage/app'));$disk=realpath(config('filesystems.disks.local.root')); if(!$source||!$disk||!str_starts_with($disk,$source))throw new RuntimeException('Unexpected private disk mapping'); $relative=ltrim(substr($disk,strlen($source)),'/');$count=0;$mfa=0; foreach($records['documents'] as $doc){$key=$doc['storage_key'];if(str_contains($key,'..')||str_starts_with($key,'/'))throw new RuntimeException('Unsafe storage key');$path=realpath($files.'/storage/app/'.$relative.'/'.$key);if(!$path||!str_starts_with($path,$files.'/storage/app/'))throw new RuntimeException('Missing recovered original');$bytes=$decrypt(file_get_contents($path));if(!hash_equals(trim($doc['sha256']),hash('sha256',$bytes)))throw new RuntimeException('Recovered original hash mismatch');$count++;} foreach($records['mfa'] as $row){foreach(['two_factor_secret','two_factor_recovery_codes'] as $field){if(!empty($row[$field]))$decrypt($row[$field]);}$mfa++;} echo json_encode(['encrypted_originals_recovered'=>$count,'mfa_envelopes_recovered'=>$mfa,'preserved_keys_verified'=>true],JSON_THROW_ON_ERROR); """ envelope=json.loads(subprocess.check_output(['php8.4'],input=php.encode(),cwd=root)) proof={'restore_verified':True,'all_table_counts_and_checksums_match':True,'tables':len(restored),'isolated_network':'none','isolated_database':'offers_preinstall_restore_20261114_test','isolated_port':55441,'schema_changes':0,'operational_data_writes':0,'files_restored_and_verified':recovered,'encryption':envelope} (stage/'restore-receipt.json').write_text(json.dumps(proof,indent=2)) print(json.dumps(proof)) finally: details=json.loads(run(['docker','inspect',cid]))[0] assert details['Config']['Labels'].get('investmoz.task')=='offers-preinstall-20261114' and details['HostConfig']['NetworkMode']=='none' run(['docker','rm','-f','-v',cid]) PY