16 lines
3.1 KiB
Bash
16 lines
3.1 KiB
Bash
set -eu
|
|
python3 - <<'REMOTE'
|
|
from pathlib import Path
|
|
import subprocess,json,hashlib,os
|
|
assert os.environ.get('SSH_CONNECTION')
|
|
root=Path('/var/www/html/teste-investidor.tempo.co.mz/current').resolve()
|
|
stage=Path('/var/backups/investmoz-offers-preinstall-20261114/backup')
|
|
php='<?php\nrequire \'vendor/autoload.php\';$app=require \'bootstrap/app.php\';$app->make(Illuminate\\Contracts\\Console\\Kernel::class)->bootstrap();\nuse Illuminate\\Support\\Facades\\DB;\nDB::beginTransaction(); DB::statement(\'SET TRANSACTION ISOLATION LEVEL REPEATABLE READ READ ONLY\'); DB::statement("SET TIME ZONE \'UTC\'");\ntry {\n $identity=DB::selectOne(\'select current_database() as database,inet_server_port() as port\');\n if($identity->database!==\'teste_investidor\'||(int)$identity->port!==5432)throw new RuntimeException(\'Wrong active source\');\n $snapshot=DB::selectOne(\'select pg_export_snapshot() as id\')->id;\n $tables=[];$sql="BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY; SET TRANSACTION SNAPSHOT \'".$snapshot."\'; SET TIME ZONE \'UTC\';\\n";\n foreach(DB::select("select schemaname,tablename from pg_tables where schemaname not in (\'pg_catalog\',\'information_schema\') and schemaname not like \'pg_%\' order by schemaname,tablename") as $t){\n $name=$t->schemaname.\'.\'.$t->tablename;$quoted=\'"\'.str_replace(\'"\',\'""\',$t->schemaname).\'"."\'.str_replace(\'"\',\'""\',$t->tablename).\'"\';\n $sql.="select json_build_object(\'name\',\'".$name."\',\'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";\n }\n $sql.=\'ROLLBACK;\';$reader=new Symfony\\Component\\Process\\Process([\'runuser\',\'-u\',\'postgres\',\'--\',\'psql\',\'-X\',\'-q\',\'-A\',\'-t\',\'-v\',\'ON_ERROR_STOP=1\',\'-d\',\'teste_investidor\']);$reader->setInput($sql)->setTimeout(90)->mustRun();\n foreach(explode("\\n",trim($reader->getOutput())) as $line){$row=json_decode($line,true,512,JSON_THROW_ON_ERROR);$name=$row[\'name\'];unset($row[\'name\']);$tables[$name]=$row;}\n echo json_encode([\'target\'=>$identity,\'tables\'=>$tables],JSON_THROW_ON_ERROR);\n}finally{DB::rollBack();}\n'
|
|
actual=json.loads(subprocess.check_output(['php8.4'],input=php.encode(),cwd=root,timeout=120))
|
|
receipt=json.loads((stage/'backup-receipt.json').read_text())
|
|
changes=[name for name in set(receipt['tables'])|set(actual['tables']) if receipt['tables'].get(name)!=actual['tables'].get(name)]
|
|
files=json.loads((stage/'release-files-private.json').read_text());code_changes=[name for name,want in files.items() if not (root/name).is_file() or hashlib.sha256((root/name).read_bytes()).hexdigest()!=want]
|
|
proof={'database_tables_rechecked':len(actual['tables']),'tables_changed_since_backup':sorted(changes),'release_files_rechecked':len(files),'release_files_changed':code_changes,'installed_revision':json.loads(Path('/var/lib/investmoz-deploy/installed.json').read_text())['commit'],'active_data_writes':0,'source_read_only_transactions':True}
|
|
(stage/'preservation-receipt.json').write_text(json.dumps(proof,indent=2));print(json.dumps(proof))
|
|
REMOTE
|