You restore a website from a database backup by importing a SQL dump into a database server, then pointing the site's configuration file at that database and verifying that content, users, and settings load correctly. The key condition is knowing which piece actually broke: a database restore fixes lost or corrupted content, users, orders, and settings, but it won't repair deleted theme files, a broken plugin folder, or a botched server configuration. If your site's pages are blank because a PHP file was overwritten, restoring the database alone changes nothing.
This guide walks through the practical sequence for a typical MySQL or MariaDB-backed site such as WordPress, Drupal, or a custom PHP application: capture the current state, prepare a clean target database, import the dump, reconnect the application, and verify what came back. It also covers the decisions that trip people up—which backup to pick, how to handle a database that's too large for a standard import, and when to stop and hand the job to a host or DBA.
First Decide Whether the Database Is Really the Problem
Before touching any restore, you want evidence that the database—not files, DNS, or certificates—is the failure point. Getting this wrong is the most common way a recovery effort makes things worse, because you overwrite a working database with an older one.
Look for database-specific symptoms: the site connects but pages show database connection errors; content is missing while the theme and layout still render; logins fail for all users including admins; or the admin dashboard loads but posts and pages are empty. Check the application's error log and the web server log for messages like "Error establishing a database connection," "Table doesn't exist," or "Access denied for user." A 500 error with no database mention often points to a PHP fatal error instead.
File-side symptoms look different. If CSS and images are gone, the theme directory was likely damaged. If the whole site is unreachable but the database responds, suspect DNS, the web server, or an expired certificate. In those cases a database restore is the wrong tool, and you should restore files or fix configuration first.
A quick sanity check: connect to the database with a client and run a count against a core table. In WordPress that might be SELECT COUNT(*) FROM wp_posts;. If the table returns rows and the count looks plausible for your site, the data may be intact and the problem lives elsewhere. If the table is missing or empty, a restore is justified.
Capture the Current State Before You Change Anything
Restoring is destructive to whatever is currently in the target database, so preserve the present state first. Even a broken database can contain content published after your last backup, and you may need to merge it back later.
Take a fresh dump of the live database, even if it's damaged, and store it somewhere separate from your backups. On a server with command-line access, mysqldump -u username -p dbname > current_state.sql captures it. If the database won't dump cleanly because of corruption, at least export the tables that still respond. Also copy the application's configuration file—wp-config.php for WordPress, settings.php for Drupal, or .env for many frameworks—so you know the exact credentials and table prefix the site expects.
Note the current database name, user, host, and table prefix. Restoring into a database with a different prefix than the configuration expects is a silent failure: the site connects but sees no tables. Recording these details now saves confusion later.
If the site is live and taking traffic, consider putting it into maintenance mode or pointing DNS to a staging copy. Users writing to the database during a restore can cause duplicate keys or partial data. For a small personal site this matters less; for anything with orders or user registrations, freeze writes first.
Choose the Right Backup and Confirm It's Usable
Not all backups are equal, and the newest file isn't automatically the best one. You're choosing between recency and integrity, and you want the most recent backup you can verify actually restores.
Identify what you have. A database backup is usually a .sql file, sometimes compressed as .sql.gz, .zip, or .tar.gz. Some hosts provide backups only through a control panel restore button, which handles the import for you. Others deliver a full-site archive that bundles files and a database dump together. Know which you're holding before you start.
Then verify the dump before importing. A backup that's truncated, zero bytes, or missing its final lines will import partially and leave you with a broken database. Open the file and check that it ends with a statement terminator and contains the expected CREATE TABLE and INSERT statements. For a compressed file, test that it decompresses without error. A dump that starts with CREATE TABLE lines but has no INSERT data restores an empty schema—technically successful, practically useless.
Check the table prefix inside the dump too. If the file creates wp_ tables but your config expects mysite_, you'll need to either import and rename, or edit the config. Search the dump for its first CREATE TABLE line to confirm.
If you keep several generations of backups, prefer the newest one that passes these checks. An older intact backup beats a newer corrupt one every time.
Prepare a Clean Target Database
Importing a full dump into a database that already holds tables usually fails on the first CREATE TABLE with a "table already exists" error, or worse, mixes old and new rows. Start clean.
Create a new, empty database rather than reusing the old one. This gives you a rollback point: if the restore goes wrong, the original database is still there. On the command line, CREATE DATABASE site_restore CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; creates it. Match the character set your application uses—most modern sites want utf8mb4, which handles emoji and full Unicode; an older site might expect utf8 or latin1. A mismatch shows up later as garbled characters or failed inserts.
Create or identify a database user with full privileges on the new database. You can reuse the existing application user if it has rights, or grant a dedicated user: GRANT ALL PRIVILEGES ON site_restore.* TO 'siteuser'@'localhost'; followed by FLUSH PRIVILEGES;. Keep the credentials handy for the configuration step.
If you're working on a managed host without command-line access, use phpMyAdmin or the host's database tool to create the empty database and note its name. The import step will use the same interface.
Import the Backup Into the Database
With a clean target ready, import the dump. The method depends on file size and the tools you have.
For command-line access, the standard approach is:
1: Confirm the target database is empty with SHOW TABLES;. 2: Run the import, for example mysql -u username -p site_restore < backup.sql for a plain dump, or gunzip < backup.sql.gz | mysql -u username -p site_restore for a gzipped one. 3: Watch for errors in the output; a clean import prints nothing. 4: Verify by listing tables and checking row counts against your expectations.
What to expect: the import runs silently on success. Errors appear as lines beginning with ERROR. A single failed statement doesn't necessarily stop the import, so a run that "finishes" can still be incomplete—read the output.
For large files that exceed upload limits in phpMyAdmin or a host panel, you have options. Many hosts accept imports through the command line even when the web interface caps uploads. If you only have the web interface, some panels allow chunked uploads or importing from a file already on the server. A dump that's many gigabytes may need to be split or imported table by table; tools like split or dedicated import scripts help, but this is where a host support ticket often saves hours.
Success check: after import, run SHOW TABLES; and confirm the expected tables exist with your site's prefix. Then query a core table for a row count and compare it to what you remember or what the backup's metadata suggests. If your site had roughly 400 posts and wp_posts returns 400, the content data is likely intact.
Failure modes to recognize: "Access denied" means wrong credentials or missing grants. "Unknown database" means the target name is wrong. "Table already exists" means you didn't start clean. A timeout mid-import leaves a partial database—drop the tables, recreate the empty database, and retry, ideally with a larger timeout or a command-line import.
Reconnect the Application to the Restored Database
The data is back, but the site won't use it until the configuration points at the right place. Edit the application's config file with the new database name, user, password, and host. For WordPress this is wp-config.php; the relevant constants are DB_NAME, DB_USER, DB_PASSWORD, and DB_HOST. For Drupal it's settings.php; for many frameworks, environment variables in .env.
Pay attention to the table prefix. If the dump used wp_ and the config says wp_, you're fine. If they differ, either change the config's prefix variable or rename the tables after import. Renaming is riskier on a live site but straightforward on a fresh restore.
One subtlety: many applications store the site URL in the database itself, not just in config. If you restored into a different domain or a staging environment, the site may redirect to the old address. WordPress stores this in the wp_options table under siteurl and home. You can update those values directly with SQL, or use a search-replace tool designed for serialized data—plain text search-replace can corrupt serialized PHP values. If you're restoring to the same domain, this usually isn't an issue.
After editing config, clear any application cache. Cached pages or object caches can serve stale content and make a successful restore look broken. Restart the PHP service or clear the cache directory as your setup requires.
Verify the Restore Actually Worked
A restore isn't done when the import finishes; it's done when the site behaves correctly. Work through checks in order of importance.
First, load the homepage and confirm it renders with content. Then log in as an administrator. Successful login proves the users table and password hashes came through. Browse to the posts or content list and confirm the expected number of entries. Open a recent post and check that its body text, images references, and metadata appear.
Next, test the interactive paths that depend on the database: submitting a comment, a contact form that writes to the database, a search query, or a checkout flow if you run a store. These exercise write operations and reveal permission problems that read-only checks miss.
Check for character encoding issues—garbled apostrophes or question marks where accented characters should be indicate a charset mismatch between the dump and the target database. Check the error log again for new database warnings.
A practical success criterion: the site serves pages, an admin can log in, content counts match your expectation, and a test write succeeds. If all four hold, the database restore is functionally complete.
Handle Partial Restores and Merging Newer Content
Sometimes the backup predates content you need. You restored last week's database and lost three days of posts or orders. Rather than choosing between an old backup and a broken live database, you can merge.
The safe approach: keep the freshly restored database as the working copy, and extract only the newer rows from the current-state dump you captured earlier. Identify the tables with recent activity—posts, comments, orders—and pull rows created after the backup's timestamp. Import those into the restored database, watching for primary key collisions: a new post may reuse an ID that the older backup already assigned to something else.
This is fiddly and easy to get wrong, especially with related tables (a post and its metadata live in separate tables linked by ID). For a handful of posts, re-creating them by hand may be faster and safer. For substantial recent data, a staging environment where you can test the merge before touching production is worth the setup. If the merge involves financial records or user data you can't afford to corrupt, this is a reasonable point to bring in a developer or your host's support.
Common Failure Modes and How to Recover
Most restore failures fall into a few recognizable patterns, and each has a reversible first check.
The site shows a database connection error after restore. Check the config credentials against the database user's actual grants, and confirm the host value—localhost versus 127.0.0.1 versus a remote hostname matters. Try connecting with the same credentials from the command line to isolate whether the problem is the app or the database.
Pages load but content is empty. This usually means the table prefix in config doesn't match the imported tables, or you imported into the wrong database. Run SHOW TABLES; and compare prefixes. It can also mean the dump contained schema but no data—recheck the backup file for INSERT statements.
The import stopped partway with an error. Note the failing statement. "Duplicate entry" suggests you didn't start from an empty database. "Specified key was too long" is a version mismatch between the MySQL server that made the dump and the one importing it—older servers had shorter index limits. Dropping the partial tables and recreating the empty database is the clean retry; adjusting server settings is the longer fix.
Characters look wrong. The dump's charset and the database's charset disagree. Recreating the database with the matching charset and re-importing is the reliable fix; patching individual rows rarely catches everything.
The site redirects to the old domain. Update the stored site URL in the options table, using a serialization-aware tool if the value is embedded in serialized data.
If repeated attempts fail, or the database contains data you can't reconstruct and the errors point to corruption or version incompatibility, stop. Further improvisation risks overwriting the only good copy. Your host's support team or a database administrator can import with server-level tools and diagnose version mismatches that aren't fixable from the application side.
Prevention: Make the Next Restore Boring
The restore you're doing now is easier if the next one is routine. A few habits make that happen.
Automate database backups on a schedule and store them off the server—a copy on the same disk dies with the disk. Keep several generations so a corrupt recent backup doesn't leave you with nothing. Test a restore into a staging database periodically; an untested backup is a guess, not a plan.
Record the database name, user, prefix, and charset alongside your backups. Future-you, mid-incident, will not want to reverse-engineer them. And keep the command-line import one-liner written down somewhere accessible, because the moment you need it is the moment you least want to be looking up syntax.
FAQ
Can I restore a database without command-line access? Yes. Host control panels and phpMyAdmin can create databases and import dumps through a web interface. The limitation is file size—very large dumps may exceed upload limits, in which case host support or a command-line session is usually the practical route.
Will restoring the database bring back deleted images and theme files? No. Images, themes, and plugins live in the filesystem, not the database. A database restore returns content, users, and settings; files need a separate file backup or restore. A full-site backup bundles both, which is why they're preferable when available.
How do I know which backup to choose if I have several? Pick the newest one you can verify is complete and imports cleanly. Check that the file contains both table-creation and data-insert statements, and confirm the table prefix matches your configuration. An older intact backup is better than a newer damaged one.
What if the restored site works but some recent posts are missing? Your backup predates them. You can re-create the missing content manually, or merge rows from a dump of the pre-restore database, taking care with ID collisions in related tables. For anything involving orders or user records, test the merge on a staging copy first.