[GH-ISSUE #6993] [Bug]: SQLite-to-PostgreSQL import ignores Keep this machine's settings #4610

Closed
opened 2026-10-31 20:40:53 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @HamedSanaei on GitHub (Oct 28, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/6993

Describe the bug

On a PostgreSQL-backed 3x-ui panel, importing a SQLite .db backup or SQLite SQL migration .dump ignores the "Keep this machine's settings" checkbox (checked by default). Unlike the native PostgreSQL pg_dump restore path, it silently replaces the destination panel's host-specific settings with values from the source machine.

This may overwrite the destination's panel port, web base path, bind addresses, certificate paths and node identity, making the panel unreachable after migration.

Steps to reproduce

  1. Configure PostgreSQL-backed destination B with webPort=2053, webBasePath=/destination/.
  2. Prepare a valid SQLite .db backup from source A with webPort=2087, webBasePath=/source/.
  3. On B, open Backup & Restore, leave Keep this machine's settings checked and import the SQLite backup.
  4. After the import/restart, examine B's web port/base path. Repeat using a SQLite SQL migration .dump.

Expected behavior

With Keep this machine's settings enabled, destination B retains its own host-bound settings. Inbounds, clients, users and ordinary settings are imported from A. When the checkbox is disabled, importing A's host-bound settings is intentional.

Actual behavior (verified by code inspection)

Both SQLite input types bypass the flag and replace the destination settings table. The host settings are not captured/restored. A dedicated end-to-end reproduction is still needed.

Root cause

internal/web/service/server.go:

func (s *ServerService) importPostgresDB(file multipart.File, keepHostSettings bool) error {
    // ...
    switch kind {
    case importKindPgDump:
        return s.restorePostgresDump(file, keepHostSettings)
    case importKindSQLiteDB:
        return s.migrateSQLiteIntoPostgres(file, false)
    case importKindSQLiteDump:
        return s.migrateSQLiteIntoPostgres(file, true)
    }
    // ...
}

migrateSQLiteIntoPostgres(file, isSQLDump) does not accept keepHostSettings, does not call captureHostBoundSettings(), and does not call restoreHostBoundSettings(). Instead it calls database.MigrateData, which truncates and repopulates destination tables, including settings. In contrast, the native PostgreSQL dump restore path and the SQLite-backed import path honor the flag.

The frontend already sends the checkbox value; the controller defaults it to true.

Suggested fix

  • Forward keepHostSettings to migrateSQLiteIntoPostgres for both SQLite formats.
  • If enabled, snapshot host-bound settings before closing/migrating the DB and restore them only after successful migration and reopening it, reusing the existing helpers.
  • Preserve correct semantics for absent/empty destination settings and lazily generated node mTLS material.
  • Keep keepHostSettings=false as the intentional full-clone mode.
  • Preserve transaction rollback and failure behavior; avoid unrelated changes.

Regression tests

Cover both .db and SQLite SQL .dump → PostgreSQL, each with keep=true/false. Assert that port, web base path, bind/certificate settings and node identity stay local when keep=true while normal data is imported. Include absent/empty setting keys and failed migrations.

  • #5004: original host-settings portability request.
  • #6227: introduced the keepHostSettings option; review discussion previously flagged the SQLite-to-PostgreSQL branch.

Scope: SQLite → PostgreSQL migration only, not native PostgreSQL pg_dump restore.

Originally created by @HamedSanaei on GitHub (Oct 28, 2026). Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/6993 ### Describe the bug On a PostgreSQL-backed 3x-ui panel, importing a SQLite `.db` backup or SQLite SQL migration `.dump` **ignores the "Keep this machine's settings" checkbox** (checked by default). Unlike the native PostgreSQL `pg_dump` restore path, it silently replaces the destination panel's host-specific settings with values from the source machine. This may overwrite the destination's panel port, web base path, bind addresses, certificate paths and node identity, making the panel unreachable after migration. ### Steps to reproduce 1. Configure PostgreSQL-backed destination B with `webPort=2053`, `webBasePath=/destination/`. 2. Prepare a valid SQLite `.db` backup from source A with `webPort=2087`, `webBasePath=/source/`. 3. On B, open **Backup & Restore**, leave **Keep this machine's settings** checked and import the SQLite backup. 4. After the import/restart, examine B's web port/base path. Repeat using a SQLite SQL migration `.dump`. ### Expected behavior With **Keep this machine's settings** enabled, destination B retains its own host-bound settings. Inbounds, clients, users and ordinary settings are imported from A. When the checkbox is disabled, importing A's host-bound settings is intentional. ### Actual behavior (verified by code inspection) Both SQLite input types bypass the flag and replace the destination `settings` table. The host settings are not captured/restored. A dedicated end-to-end reproduction is still needed. ### Root cause [`internal/web/service/server.go`](https://github.com/MHSanaei/3x-ui/blob/main/internal/web/service/server.go): ```go func (s *ServerService) importPostgresDB(file multipart.File, keepHostSettings bool) error { // ... switch kind { case importKindPgDump: return s.restorePostgresDump(file, keepHostSettings) case importKindSQLiteDB: return s.migrateSQLiteIntoPostgres(file, false) case importKindSQLiteDump: return s.migrateSQLiteIntoPostgres(file, true) } // ... } ``` `migrateSQLiteIntoPostgres(file, isSQLDump)` does not accept `keepHostSettings`, does not call `captureHostBoundSettings()`, and does not call `restoreHostBoundSettings()`. Instead it calls `database.MigrateData`, which truncates and repopulates destination tables, including `settings`. In contrast, the native PostgreSQL dump restore path and the SQLite-backed import path honor the flag. The frontend already sends the checkbox value; the controller defaults it to true. ### Suggested fix - Forward `keepHostSettings` to `migrateSQLiteIntoPostgres` for **both** SQLite formats. - If enabled, snapshot host-bound settings **before** closing/migrating the DB and restore them **only after successful migration and reopening** it, reusing the existing helpers. - Preserve correct semantics for absent/empty destination settings and lazily generated node mTLS material. - Keep `keepHostSettings=false` as the intentional full-clone mode. - Preserve transaction rollback and failure behavior; avoid unrelated changes. ### Regression tests Cover both `.db` and SQLite SQL `.dump` → PostgreSQL, each with keep=true/false. Assert that port, web base path, bind/certificate settings and node identity stay local when keep=true while normal data is imported. Include absent/empty setting keys and failed migrations. ### Related - #5004: original host-settings portability request. - #6227: introduced the keepHostSettings option; review discussion previously flagged the SQLite-to-PostgreSQL branch. **Scope:** SQLite → PostgreSQL migration only, not native PostgreSQL `pg_dump` restore.
Author
Owner

@github-actions[bot] commented on GitHub (Oct 28, 2026):

Confirmed: on a PostgreSQL-backed panel, importing a SQLite .db or a SQLite migration .dump ignores Keep this machine's settings. It replaces the destination's whole settings table with the source's, including webPort, webBasePath, secret, panelGuid and the node mTLS material.

The checkbox value does reach the service. BackupModal.tsx:49 sends formData.append('keepHostSettings', String(keepHostSettings)), and the controller reads it at internal/web/controller/server.go:391 with keepHostSettings := c.Request.FormValue("keepHostSettings") != "false". importPostgresDB then forwards the flag only to the pg_dump branch and drops it for both SQLite branches:

// internal/web/service/server.go:2099-2104 (importPostgresDB)
case importKindPgDump:
    return s.restorePostgresDump(file, keepHostSettings)
case importKindSQLiteDB:
    return s.migrateSQLiteIntoPostgres(file, false)
case importKindSQLiteDump:
    return s.migrateSQLiteIntoPostgres(file, true)

The false/true passed there is isSQLDump, not the keep flag. migrateSQLiteIntoPostgres (server.go:2186) has no keepHostSettings parameter. It goes straight from database.CloseDB() to database.MigrateData(dbPath, config.GetDBDSN()) (server.go:2231) and then database.InitDB (server.go:2233), without calling captureHostBoundSettings() or restoreHostBoundSettings(). MigrateData lists &model.Setting{} in migrationModels() (internal/database/migrate_data.go:41). It runs truncatePostgresTables(tx, migrationModels()) and then copyTable for each model (migrate_data.go:128-137), so the destination's settings rows are deleted and replaced with the source's.

The other two import paths both do it correctly. The SQLite-panel import captures at server.go:1878-1879 and restores at server.go:1937. The pg_dump restore captures at server.go:2151-2152 and restores at server.go:2171.

Workaround until this is fixed: after a SQLite → PostgreSQL import, the panel will listen on the source machine's port and base path. Reach it there, or reset them from the shell with x-ui setting (e.g. x-ui setting -port 2053 -webBasePath /destination/). Then check the certificate paths and the sub-server settings.

Verdict: Confirmed bug
Severity: High
Confidence: High
Root cause: internal/web/service/server.go:2101-2104, importPostgresDB, drops keepHostSettings for importKindSQLiteDB/importKindSQLiteDump. migrateSQLiteIntoPostgres (server.go:2186-2247) never snapshots or restores host-bound settings around database.MigrateData, which truncates and recopies settings. This happens whenever the panel runs on PostgreSQL and the upload is a SQLite file, whatever the checkbox says. The SQLite → PostgreSQL branch came in with 30b61161 "feat: import SQLite migration dumps through the PostgreSQL panel restore", and the flag came in with 6e80a468 "feat(server): keep this machine's own settings when importing a database (#6227)". Both are first in v3.10.0, and the flag was never wired into this branch.
Same class: None found. ImportDB (SQLite panel, server.go:1878/1937) and restorePostgresDump (server.go:2151/2171) both honour the flag. The only other MigrateData caller, x-ui migrate-db --dsn (main.go:703), is a deliberate CLI full clone with no keep option.
Already fixed: still present on the default branch (v3.11.0 tip)
Duplicate of: None (Related: #6227 introduced the option; #5004 the original request)
Evidence: server.go:2102 return s.migrateSQLiteIntoPostgres(file, false); server.go:2231 migrateErr := database.MigrateData(dbPath, config.GetDBDSN()); migrate_data.go:41 &model.Setting{},; migrate_data.go:128 truncatePostgresTables(tx, migrationModels()); contrast server.go:2151-2152 / 2171 in restorePostgresDump.
Not determined: I could not run this end to end. A Postgres-backed integration test importing a SQLite file with webPort set differently on each side would confirm the overwrite at runtime.

Suggested fix: Pass keepHostSettings from importPostgresDB into migrateSQLiteIntoPostgres. When it is set, call captureHostBoundSettings() before database.CloseDB(), and call restoreHostBoundSettings() after database.InitDB succeeds, the same way restorePostgresDump does. The existing helper already handles absent keys and the lazily minted mTLS keys (server.go:1794-1803), so no new logic is needed there.

@MHSanaei

This reply was generated automatically by an issue analysis bot; a maintainer may follow up.

<!-- gh-comment-id:6470557931 --> @github-actions[bot] commented on GitHub (Oct 28, 2026): Confirmed: on a PostgreSQL-backed panel, importing a SQLite `.db` or a SQLite migration `.dump` ignores **Keep this machine's settings**. It replaces the destination's whole `settings` table with the source's, including `webPort`, `webBasePath`, `secret`, `panelGuid` and the node mTLS material. The checkbox value does reach the service. `BackupModal.tsx:49` sends `formData.append('keepHostSettings', String(keepHostSettings))`, and the controller reads it at `internal/web/controller/server.go:391` with `keepHostSettings := c.Request.FormValue("keepHostSettings") != "false"`. `importPostgresDB` then forwards the flag only to the `pg_dump` branch and drops it for both SQLite branches: ```go // internal/web/service/server.go:2099-2104 (importPostgresDB) case importKindPgDump: return s.restorePostgresDump(file, keepHostSettings) case importKindSQLiteDB: return s.migrateSQLiteIntoPostgres(file, false) case importKindSQLiteDump: return s.migrateSQLiteIntoPostgres(file, true) ``` The `false`/`true` passed there is `isSQLDump`, not the keep flag. `migrateSQLiteIntoPostgres` (`server.go:2186`) has no `keepHostSettings` parameter. It goes straight from `database.CloseDB()` to `database.MigrateData(dbPath, config.GetDBDSN())` (`server.go:2231`) and then `database.InitDB` (`server.go:2233`), without calling `captureHostBoundSettings()` or `restoreHostBoundSettings()`. `MigrateData` lists `&model.Setting{}` in `migrationModels()` (`internal/database/migrate_data.go:41`). It runs `truncatePostgresTables(tx, migrationModels())` and then `copyTable` for each model (`migrate_data.go:128-137`), so the destination's settings rows are deleted and replaced with the source's. The other two import paths both do it correctly. The SQLite-panel import captures at `server.go:1878-1879` and restores at `server.go:1937`. The `pg_dump` restore captures at `server.go:2151-2152` and restores at `server.go:2171`. **Workaround until this is fixed:** after a SQLite → PostgreSQL import, the panel will listen on the source machine's port and base path. Reach it there, or reset them from the shell with `x-ui setting` (e.g. `x-ui setting -port 2053 -webBasePath /destination/`). Then check the certificate paths and the sub-server settings. Verdict: Confirmed bug Severity: High Confidence: High Root cause: `internal/web/service/server.go:2101-2104`, `importPostgresDB`, drops `keepHostSettings` for `importKindSQLiteDB`/`importKindSQLiteDump`. `migrateSQLiteIntoPostgres` (`server.go:2186-2247`) never snapshots or restores host-bound settings around `database.MigrateData`, which truncates and recopies `settings`. This happens whenever the panel runs on PostgreSQL and the upload is a SQLite file, whatever the checkbox says. The SQLite → PostgreSQL branch came in with 30b61161 "feat: import SQLite migration dumps through the PostgreSQL panel restore", and the flag came in with 6e80a468 "feat(server): keep this machine's own settings when importing a database (#6227)". Both are first in v3.10.0, and the flag was never wired into this branch. Same class: None found. `ImportDB` (SQLite panel, `server.go:1878`/`1937`) and `restorePostgresDump` (`server.go:2151`/`2171`) both honour the flag. The only other `MigrateData` caller, `x-ui migrate-db --dsn` (`main.go:703`), is a deliberate CLI full clone with no keep option. Already fixed: still present on the default branch (v3.11.0 tip) Duplicate of: None (Related: #6227 introduced the option; #5004 the original request) Evidence: `server.go:2102` `return s.migrateSQLiteIntoPostgres(file, false)`; `server.go:2231` `migrateErr := database.MigrateData(dbPath, config.GetDBDSN())`; `migrate_data.go:41` `&model.Setting{},`; `migrate_data.go:128` `truncatePostgresTables(tx, migrationModels())`; contrast `server.go:2151-2152` / `2171` in `restorePostgresDump`. Not determined: I could not run this end to end. A Postgres-backed integration test importing a SQLite file with `webPort` set differently on each side would confirm the overwrite at runtime. Suggested fix: Pass `keepHostSettings` from `importPostgresDB` into `migrateSQLiteIntoPostgres`. When it is set, call `captureHostBoundSettings()` before `database.CloseDB()`, and call `restoreHostBoundSettings()` after `database.InitDB` succeeds, the same way `restorePostgresDump` does. The existing helper already handles absent keys and the lazily minted mTLS keys (`server.go:1794-1803`), so no new logic is needed there. @MHSanaei _This reply was generated automatically by an issue analysis bot; a maintainer may follow up._ <!-- claude-issue:analyst -->
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#4610
No description provided.