// One-shot import into a NEW Docker volume. Stop the source server before running. import { existsSync, copyFileSync, chmodSync, constants } from 'node:fs' import path from 'node:path' import Database from 'better-sqlite3' import { decryptSecret } from './secrets.js' const source = '/import', target = path.dirname(process.env.DB_PATH || '/app/data/syncova.db') if (existsSync(path.join(target, 'syncova.db'))) throw Error('Ziel enthält bereits eine Datenbank. Es wird nichts überschrieben.') if (!existsSync(path.join(source, 'syncova.db'))) throw Error('Quelldatei /import/syncova.db fehlt.') for (const file of ['syncova.db', 'syncova.db-wal', 'smtp.key']) if (existsSync(path.join(source, file))) { copyFileSync(path.join(source, file), path.join(target, file), constants.COPYFILE_EXCL) chmodSync(path.join(target, file), 0o600) } const db = new Database(path.join(target, 'syncova.db')) try { for (const table of ['queue_mail', 'queue_inbound']) if (db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name=?").get(table)) { const row = db.prepare(`SELECT password FROM ${table} WHERE password!='' LIMIT 1`).get() if (row && !process.env.SMTP_ENCRYPTION_KEY && !existsSync(path.join(target, 'smtp.key'))) throw Error('Mail-Schlüssel fehlt. smtp.key übernehmen oder SMTP_ENCRYPTION_KEY setzen.') if (row && !decryptSecret(row.password)) throw Error('Mail-Schlüssel fehlt oder passt nicht.') } if (db.pragma('integrity_check', { simple: true }) !== 'ok') throw Error('Datenbankprüfung fehlgeschlagen.') } finally { db.close() } console.log( 'Import erfolgreich: Datenbank, Nachrichten, Anhänge, Konfiguration und Mail-Schlüssel übernommen. Bestehende Benutzerkonten bleiben gültig.', )