Skip to content

Database restore overwrites wrong database when SQL dump contains USE statement #3436

Description

@CebRazvan

To Reproduce

Steps to Reproduce

  1. Have two databases: production_db and dev_db
  2. Create a backup of production_db (the dump will contain USE production_db;)
  3. Go to Dokploy → Backups → Restore Backup
  4. Select destination and set Database Name to dev_db
  5. Click Restore

Current vs. Expected behavior

Description

When restoring a database backup through Dokploy's restore feature, if the SQL dump file contains a USE database_name statement, the restore operation overwrites that database instead of the target database specified in the restore dialog. This caused a production data loss in our environment.

Expected Behavior

The backup should be restored into dev_db only. The USE production_db; statement in the dump should either be:

  • Replaced with USE dev_db;
  • Removed entirely
  • Or the restore should use --one-database flag

Actual Behavior

The restore command executes:
mariadb -u 'user' -p'password' dev_db < backup.sql
But inside backup.sql:

USE `production_db`;
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` ...
INSERT INTO `users` ...

The USE production_db; statement switches context to the production database, causing all subsequent DROP TABLE and INSERT statements to execute against production instead of dev_db.

Result: Production database is overwritten with old backup data.

Suggested Fix

Option 1: Filter/replace USE statements during restore:

gunzip < backup.sql.gz | sed "s/USE \`.*\`/USE \`${TARGET_DB}\`/g" | mariadb -u user -p target_db

Option 2: Use --one-database flag:
mariadb --one-database target_db < backup.sql
Option 3: Add a warning in the UI when the backup's source database differs from the target database.

Provide environment information

Operating System:
  OS: Ubuntu 24.04.3 LTS
  Arch: x86_64
Dokploy version: 0.26.2
VPS Provider: Hostinger (4 VCPU, 16Gb Ram)
applications/services to deploy:
Database, laravel, redis, elastic-search, soketi.

Database: MariaDB 11.x
Backup source: S3 (Hetzner Object Storage)

Which area(s) are affected? (Select all that apply)

Databases

Are you deploying the applications where Dokploy is installed or on a remote server?

Same server where Dokploy is installed

Additional context

No response

Will you send a PR to fix it?

Yes

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions