Skip to content

Restoring only databases from a cPanel backup: the surgical path

When the application files are fine but the database is corrupted, restoring the whole account overwrites work since the last backup. The surgical path restores only the database.

Sometimes the recovery is “only the database is wrong” — a developer dropped the wrong table, a runaway script overwrote data, or a corrupted import polluted production. A full account restore from the previous backup would also overwrite the application files, uploaded media, and email — all of which are fine. The surgical path is to extract only the database from the backup and restore it alone.

What’s happening

A cPanel account backup is a .tar.gz with a defined internal structure. Inside, the mysql/ directory contains the database dumps as .sql files. The standard restore (/scripts/restorepkg) puts everything back. By extracting only the SQL files, you can replay them against the live MySQL while leaving every other part of the account untouched. The catch: the application’s connection credentials must match, and the database may need to be dropped first if you want a clean replace rather than an additive merge.

How to verify

Examine the backup before restoring:

# list contents of the backup
tar -tzf /backup/<date>/accounts/<user>.tar.gz | grep -E "mysql|\.sql"
# extract just the database dumps to a working directory
mkdir -p /root/restore-work
tar -xzf /backup/<date>/accounts/<user>.tar.gz \
    --wildcards "*/mysql/*" -C /root/restore-work
ls /root/restore-work/*/mysql/
# preview a dump to confirm it has the data you want
head -50 /root/restore-work/*/mysql/<user>_dbname.sql
grep "INSERT INTO" /root/restore-work/*/mysql/<user>_dbname.sql | wc -l

The fix

  1. Identify the exact database — mysql -e "SHOW DATABASES;" to see what is live. Match against the backup’s .sql file names. cPanel prefixes user databases with <user>_ so the file <user>_wp1.sql restores into <user>_wp1.
  2. Backup the live database first — mysqldump <user>_wp1 > /root/pre-restore-<user>_wp1.sql. If the restore goes wrong, you can put back the bad-but-recent state.
  3. Drop and recreate (clean restore) — mysql -e "DROP DATABASE <user>_wp1; CREATE DATABASE <user>_wp1;". The application’s MySQL user retains grants to the database with the same name.
  4. Replay the dump — mysql <user>_wp1 < /root/restore-work/*/mysql/<user>_wp1.sql. Watch for errors; a syntax mismatch (older MariaDB syntax, newer MySQL refusing) shows up here.
  5. Validate — log into the application, browse a few pages, check tables that should have data, confirm row counts roughly match expectations.

If the application also references data that lives outside this one database (a _meta table in another DB), restore those too. Stack Harbor handles database-only recoveries under cPanel/WHM management — start a conversation when a surgical restore is the right call.