Database Backup Explained
A database backup is a saved copy of a database's data and structure that can be restored if the live data is lost, corrupted, or accidentally changed. For a typical website this means regularly exporting or snapshotting the database, such as MySQL or PostgreSQL, and storing that copy somewhere separate from the live server.
Why database backups are different from file backups
A website's files, such as PHP code and images, rarely change from moment to moment, but a database is being written to constantly: new orders, comments, user accounts and session data. A backup taken while the database is actively writing can end up inconsistent unless it is taken carefully, so most database backup tools either briefly lock tables, use a consistent snapshot feature, or read a transaction log to capture a single coherent point in time.
Common backup methods
| Method | How it works | Good for |
|---|---|---|
| Logical dump | Exports data as SQL statements or a portable file (for example mysqldump or pg_dump) | Small to medium databases, easy to move between servers or versions |
| Physical/file-level backup | Copies the database's actual data files while the engine is stopped or using a snapshot tool | Large databases where speed of backup and restore matters |
| Binary log / point-in-time | Combines a full backup with a log of every change since, so the database can be restored to an exact moment | Systems that cannot afford to lose transactions since the last full backup |
| Hosting-panel backup | A built-in tool in cPanel, Plesk or a managed hosting dashboard that bundles database and file backups together | Shared hosting users without command-line access |
A simple logical backup and restore
# Back up a MySQL/MariaDB database
mysqldump -u dbuser -p database_name > backup.sql
# Restore it later
mysql -u dbuser -p database_name < backup.sql
# Back up a PostgreSQL database
pg_dump -U dbuser database_name > backup.sql
Best practices
- Follow the 3-2-1 pattern: at least three copies, on two different kinds of storage, with one copy kept off the live server.
- Automate the schedule with cron or your host's backup tool rather than relying on remembering to run it manually.
- Test restores periodically. A backup that has never been restored is not confirmed to work.
- Match backup frequency to how much data you can afford to lose (your recovery point objective): daily is common for small sites, hourly or continuous for active stores.
- Keep several generations of backups, not just the most recent one, in case corruption is discovered days later.
When you actually need a backup
Backups are not only for disasters like hardware failure. The more common need is recovering from a bad plugin update, a failed migration, an accidental table drop, or a compromised site that needs to roll back to a known-clean state. A database-only backup should normally be paired with a file backup of the website's code and uploads, since restoring one without the other can leave a site broken or out of sync.
Frequently asked questions
How often should I back up my database?
For most small sites, a daily backup is enough. Active stores, forums or any site where losing a few hours of data would be costly should back up more frequently, often hourly or through continuous point-in-time recovery.
What is the difference between a database backup and a website backup?
A database backup only saves the data stored in tables, such as posts, orders and user accounts, while a full website backup also includes files like code, themes, images and uploads. Both are usually needed to fully restore a site.
How do I back up a MySQL database?
The most common command-line method is <code>mysqldump</code>, which exports the database as a portable SQL file. Many hosting control panels also offer a one-click database backup and download option.
Where should database backups be stored?
Backups should be kept somewhere separate from the live server, such as cloud or object storage, a different data center, or downloaded locally, so a server failure or hack does not destroy the backup along with the original data.
Can I automate database backups?
Yes. A scheduled task such as a cron job can run a dump command on a regular schedule, and many hosting panels and managed database services include built-in automatic backups with a configurable retention period.
How do I know if a backup actually works?
The only reliable way is to restore it, ideally to a separate test database or server, and confirm the data and structure are intact. Periodic test restores catch problems like corrupted files or incomplete dumps before an emergency.
Related topics
- What Is a Database? Databases
- What Is MySQL? Databases
- What Is PostgreSQL? Databases
- What Is cPanel? Control Panels
- What Is Shared Hosting? Hosting Basics