69 lines
5.9 KiB
Bash
69 lines
5.9 KiB
Bash
set -eu
|
|
python3 - <<'PY'
|
|
from pathlib import Path
|
|
import os,subprocess,json,hashlib,tarfile,time
|
|
assert os.geteuid()==0 and os.environ.get('SSH_CONNECTION')
|
|
deadline=time.monotonic()+90
|
|
while subprocess.run(['systemctl','is-active','teste-investidor-deploy.service'],capture_output=True,text=True).stdout.strip() not in ['inactive','failed']:
|
|
if time.monotonic()>deadline: raise RuntimeError('Existing deployment still active; no backup or schema change performed')
|
|
time.sleep(3)
|
|
assert json.loads(Path('/var/lib/investmoz-deploy/installed.json').read_text())['commit']=='96f510965f70581a4b41dd94463f598b77dd7cf5', 'Destination advanced; reconcile first'
|
|
assert subprocess.run(['systemctl','is-active','teste-investidor-deploy.service'],capture_output=True,text=True).stdout.strip() in ['inactive','failed']
|
|
root=Path('/var/www/html/teste-investidor.tempo.co.mz/current').resolve()
|
|
stage=Path('/var/backups/investmoz-offers-preinstall-20261114/backup')
|
|
assert not stage.exists(), 'Existing backup must be reconciled before repeating'
|
|
assert __import__('shutil').disk_usage(root).free >= 5*1024**3, 'Capacity changed; active release preserved'
|
|
stage.mkdir(mode=0o700,parents=True)
|
|
php=r'''<?php
|
|
require 'vendor/autoload.php';$app=require 'bootstrap/app.php';$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
|
|
use Illuminate\Support\Facades\DB;
|
|
DB::beginTransaction(); DB::statement('SET TRANSACTION ISOLATION LEVEL REPEATABLE READ READ ONLY'); DB::statement("SET TIME ZONE 'UTC'");
|
|
try {
|
|
$identity=DB::selectOne('select current_database() as database,inet_server_port() as port');
|
|
if($identity->database!=='teste_investidor'||(int)$identity->port!==5432)throw new RuntimeException('Wrong active source');
|
|
$snapshot=DB::selectOne('select pg_export_snapshot() as id')->id;
|
|
$tables=[];$sql="BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY; SET TRANSACTION SNAPSHOT '".$snapshot."'; SET TIME ZONE 'UTC';\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){
|
|
$name=$t->schemaname.'.'.$t->tablename;$quoted='"'.str_replace('"','""',$t->schemaname).'"."'.str_replace('"','""',$t->tablename).'"';
|
|
$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";
|
|
}
|
|
$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();
|
|
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;}
|
|
$process=new Symfony\Component\Process\Process(['runuser','-u','postgres','--','pg_dump','--format=custom','--no-owner','--no-privileges','--lock-wait-timeout=10000','--snapshot='.$snapshot,'teste_investidor']);
|
|
$process->setTimeout(90)->mustRun();file_put_contents('/var/backups/investmoz-offers-preinstall-20261114/backup/database.dump',$process->getOutput());
|
|
echo json_encode(['target'=>$identity,'tables'=>$tables,'consistent_exported_snapshot'=>true,'importer_fingerprint'=>app(App\Services\TerritoryGeometryImport::class)->fingerprint(),'scanner'=>['socket'=>config('services.malware.socket'),'host'=>config('services.malware.host'),'port'=>config('services.malware.port')]],JSON_THROW_ON_ERROR);
|
|
}finally{DB::rollBack();}
|
|
'''
|
|
answer=json.loads(subprocess.check_output(['php8.4'],input=php.encode(),cwd=root))
|
|
dump=stage/'database.dump';os.chmod(dump,0o600)
|
|
listing=subprocess.check_output(['pg_restore','--list',str(dump)]).decode()
|
|
assert 'TABLE DATA public geo_locations' in listing and 'TABLE DATA public geo_geometries' in listing
|
|
storage=(root/'storage/app').resolve()
|
|
manifest={}
|
|
for p in sorted(storage.rglob('*')):
|
|
if p.is_file() and not p.is_symlink():manifest['storage/app/'+str(p.relative_to(storage))]={'bytes':p.stat().st_size,'sha256':hashlib.sha256(p.read_bytes()).hexdigest()}
|
|
env=(root/'.env').resolve();manifest['configuration/.env']={'bytes':env.stat().st_size,'sha256':hashlib.sha256(env.read_bytes()).hexdigest()}
|
|
archive=stage/'files-before.tar.gz'
|
|
with tarfile.open(archive,'w:gz') as tf:
|
|
tf.add(storage,arcname='storage/app',recursive=True);tf.add(env,arcname='configuration/.env',recursive=False)
|
|
os.chmod(archive,0o600)
|
|
verified=0
|
|
with tarfile.open(archive,'r:gz') as tf:
|
|
for member in tf:
|
|
if member.isfile():
|
|
expected=manifest[member.name];f=tf.extractfile(member);digest=hashlib.sha256(f.read()).hexdigest()
|
|
assert digest==expected['sha256'] and member.size==expected['bytes'];verified+=1
|
|
assert verified==len(manifest)
|
|
answer.update({'created_at':time.strftime('%Y-%m-%dT%H:%M:%SZ',time.gmtime()),'dump_path':str(dump),'sha256':hashlib.sha256(dump.read_bytes()).hexdigest(),'files_path':str(archive),'files_sha256':hashlib.sha256(archive.read_bytes()).hexdigest(),'files_verified':True,'file_count':verified,'file_manifest':manifest,'active_data_writes':0,'restore_verified':False,'root':str(root)})
|
|
receipt=stage/'backup-receipt.json';receipt.write_text(json.dumps(answer,indent=2));os.chmod(receipt,0o600)
|
|
assert json.loads(Path('/var/lib/investmoz-deploy/installed.json').read_text())['commit']=='96f510965f70581a4b41dd94463f598b77dd7cf5', 'Source revision changed during backup; do not install'
|
|
print(json.dumps({'stage':str(stage),'created_at':answer['created_at'],'target':answer['target'],'tables':len(answer['tables']),'files_verified':verified,'dump_sha256':answer['sha256'],'files_sha256':answer['files_sha256'],'restore_verified':False}))
|
|
|
|
import shutil
|
|
for name in ['delivery.json','schema-contract.json','ci-environment.json']:
|
|
shutil.copy2('/etc/investmoz-deploy/'+name,stage/name)
|
|
shutil.copy2('/var/lib/investmoz-deploy/installed.json',stage/'installed.json')
|
|
shutil.copy2('/var/www/deploy/investmoz-pipeline.py',stage/'pipeline.py')
|
|
|
|
PY
|