This commit is contained in:
Madzone
2026-09-24 16:39:49 +02:00
parent 9da9ec58cc
commit 3555432cd7
3 changed files with 58 additions and 1 deletions
+38
View File
@@ -38,6 +38,12 @@ JAVA_IMAGE = 'eclipse-temurin:17-jdk'
PG_IMAGE = 'postgres:17'
BASE = Path('/root/ejbca-issuing-migration')
WORK = None
EXISTING_BACKUPS = (
Path('/root/ejbca-backup/ejbca_issuing.tar.gz'),
Path('/mnt/HEIMLAN/001-Zertifikate/EJBCA-Backup/2026-09-24/ejbca_issuing.tar.gz'),
)
EXISTING_ARCHIVE_SHA256 = '63720cd393037e2d4a618891f4bd0616e29249ca82e3184b96b08f4bddeb136c'
EXISTING_H2_SHA256 = 'f7402033720c4acfa81669edc744fb39a70ca08ed089c4632ab80f3cc71f580e'
class MigrationError(Exception): pass
@@ -76,6 +82,30 @@ def absent(kind, names):
for n in names:
p=subprocess.run(['docker',kind,'inspect',n],stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)
check(p.returncode != 0, 'Bereits vorhanden: '+n+'. Keine automatische Wiederholung oder Loeschung.')
def verify_existing_backups():
records=[]
for path in EXISTING_BACKUPS:
if not path.exists():
records.append({'path':str(path),'status':'nicht verfuegbar'})
continue
check(path.is_file(),'Sicherung ist keine Datei: '+str(path))
archive_hash=digest(path)
check(archive_hash==EXISTING_ARCHIVE_SHA256,
'Vorhandene Issuing-Sicherung weicht vom im Chat geprueften Archiv ab: '+str(path))
with tarfile.open(path,'r:gz') as tar:
matches=[m for m in tar.getmembers() if m.name.removeprefix('./')=='ejbcadb.mv.db']
check(len(matches)==1 and matches[0].isfile(),'Keine eindeutige H2-Datei im alten Archiv.')
h=hashlib.sha256()
with tar.extractfile(matches[0]) as source:
for block in iter(lambda:source.read(1024*1024),b''): h.update(block)
check(h.hexdigest()==EXISTING_H2_SHA256,'H2-Pruefsumme im alten Archiv weicht ab.')
records.append({'path':str(path),'status':'verifiziert','archive_sha256':archive_hash,
'h2_sha256':h.hexdigest()})
log('Vorhandene Issuing-Sicherung verifiziert: '+str(path))
check(any(r['status']=='verifiziert' for r in records),
'Keine der bekannten Issuing-Sicherungen erreichbar. Mindestens einen Sicherungspfad bereitstellen.')
(WORK/'existing-backups.json').write_text(json.dumps(records,indent=2))
def mount(src,dst,readonly=True,kind='bind'):
return ['--mount',f'type={kind},src={src},dst={dst}'+(',readonly' if readonly else '')]
@@ -402,6 +432,7 @@ def main():
(WORK/'credentials.private.json').write_text(json.dumps({'h2':H2_PASSWORD,'test_pg':TEST_PASSWORD,'issuing_pg':FINAL_PASSWORD},indent=2))
log('Arbeitsverzeichnis: '+str(WORK))
log('1/9 Vorpruefung und Werkzeuge. Root-CA bleibt unberuehrt.')
verify_existing_backups()
for img in (JAVA_IMAGE,PG_IMAGE):
p=subprocess.run(['docker','image','inspect',img],stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)
if p.returncode: docker('pull',img,timeout=1800)
@@ -426,6 +457,13 @@ def main():
check(hashlib.sha256(tar.extractfile(matches[0]).read()).hexdigest()==digest(WORK/'original.mv.db'),
'H2-Datei und Volumesicherung stimmen nicht ueberein.')
(WORK/'original.sha256').write_text(digest(WORK/'original.mv.db')+' original.mv.db\n'+digest(WORK/'original-volume.tar')+' original-volume.tar\n')
current_hash=digest(WORK/'original.mv.db')
unchanged=current_hash==EXISTING_H2_SHA256
(WORK/'backup-comparison.json').write_text(json.dumps({
'old_h2_sha256':EXISTING_H2_SHA256,'current_h2_sha256':current_hash,
'byte_identical':unchanged,'migration_source':'original.mv.db'},indent=2))
log('Aktuelle H2-Datei ist bytegleich mit der alten Sicherung.' if unchanged else
'Aktuelle H2-Datei unterscheidet sich von der alten Sicherung. Migriert wird der frisch gesicherte Stand.')
shutil.copyfile(WORK/'original.mv.db',WORK/'recover/ejbcadb.mv.db')
log('3/9 Recover auf Arbeitskopie; Binaerspalten automatisch korrigieren.')
docker('run','--rm','--network','none',*mount(WORK/'recover','/recover',False),*mount(WORK/'drivers','/drivers'),