Create an On-Demand MySQL Backup from a PHP Application

Last updated: August 25, 2026.

An application can offer an “Export database” button, but the web request should authorize the user and queue a backup job. A background worker can then run mysqldump, store the result outside the web root, and provide an expiring download.

Backup command

mysqldump --defaults-extra-file=/secure/mysql-backup.cnf \
  --single-transaction --routines --triggers --events \
  --default-character-set=utf8mb4 app_database > app_database.sql

The option file should be readable only by the worker account:

[client]
host=127.0.0.1
user=backup_user
password=replace-with-secret

Queue the job from PHP

<?php
declare(strict_types=1);

requireAuthenticatedAdministrator();
verifyCsrfToken((string) ($_POST['csrf_token'] ?? ''));

$jobId = $backupJobs->enqueue([
    'database' => 'app_database',
    'requested_by' => currentUserId(),
    'requested_at' => new DateTimeImmutable(),
]);

header('Location: /backups/status.php?id=' . urlencode($jobId));
exit;

Operational requirements

  • Give the backup account only the permissions required for the dump.
  • Never put a database password in a process argument or download URL.
  • Encrypt sensitive backups and set automatic retention.
  • Limit who can request and download a backup.
  • Test restoration regularly and monitor worker failures.

Review the MySQL mysqldump reference for version-specific options.

Need export and administration features without building them manually? PHPRunner can generate authenticated database administration pages.

admin

admin

Leave a Reply

Your email address will not be published.