Every post, page, comment, user account, and setting lives in the MySQL database. Themes, plugins, and uploads live in files. Lose the files and you can reinstall them; lose the database and your site becomes an empty shell. This guide shows you how to back up WordPress database automatically every night, so a bad update or a failed disk never costs you more than a day of content.
Nightly fits most sites because content changes daily. A weekly backup sounds fine until you count what a week holds: new posts, comments, form entries, and orders. With a wordpress nightly backup, the worst case is losing one day of changes instead of a week.
Why the database deserves its own nightly backup
Many site owners back up their files and assume the database comes along. It does not: a full file backup of /var/www/html holds zero rows of your posts table, so restoring files alone after a server failure leaves WordPress showing an empty install wizard.
The database also changes in ways files do not: a plugin update writes new rows, a comment arrives at 2 a.m., a customer edits an address. None of that touches the filesystem, so a file-only backup taken at midnight already misses it. Sites that take orders every hour should back up more often, but nightly is a solid floor.
Three ways to automate a nightly backup
| Method | Setup effort | Cost | Control | Main weakness |
|---|---|---|---|---|
| Backup plugin | Minutes, no SSH needed | Free tier; paid tiers add cloud storage | Low | Runs inside WordPress; fails when WordPress breaks |
| mysqldump + cron | About 30 minutes | Free | Full | Needs SSH and a little scripting |
| Host backup | None | Often bundled; check official pricing for your plan | Low | Restore options set by the host |
A plugin is the fastest path on shared hosting, where you have no shell access. A host backup is a safety net, not a primary plan: you cannot choose what it restores or how far back it goes. The rest of this guide covers mysqldump and cron, the method that gives you the most control on a VPS.
How to back up WordPress database automatically with mysqldump and cron
Read the credentials from wp-config.php instead of typing them from memory: run wp config get DB_NAME, wp config get DB_USER, and wp config get DB_PASSWORD from your WordPress root if WP-CLI is installed, or copy the four DB_* values from the file. For the full walkthrough, see our guide on backing up MySQL databases automatically with mysqldump and cron.
Save this as /usr/local/bin/wp-db-backup.sh and replace the placeholders with your own values:
#!/bin/bash
set -euo pipefail
DB_NAME="your_db_name"
DB_USER="your_db_user"
DB_PASS="your_db_password"
BACKUP_DIR="/var/backups/wordpress-db"
DATE=$(date +%Y-%m-%d_%H%M)
mkdir -p "$BACKUP_DIR"
mysqldump --single-transaction --quick
-u "$DB_USER" -p"$DB_PASS" "$DB_NAME"
| gzip > "$BACKUP_DIR/$DB_NAME-$DATE.sql.gz"
chmod 600 "$BACKUP_DIR/$DB_NAME-$DATE.sql.gz"
Two details matter. --single-transaction dumps InnoDB tables without locking them, so visitors can keep commenting while the backup runs. chmod 600 keeps the dump readable only by its owner: the file holds password hashes and user email addresses.
Make the script executable with chmod +x /usr/local/bin/wp-db-backup.sh, then run it once by hand and check the output file is larger than zero bytes. A zero-byte file usually means the password or database name is wrong: mysqldump printed the error to the terminal instead of into the file.
Then add the cron job with crontab -e:
0 3 * * * /usr/local/bin/wp-db-backup.sh >> /var/log/wp-db-backup.log 2>&1
Three in the morning works because traffic is low and the dump finishes before the rush. If your audience sits in another timezone, pick their quietest hour. The log redirect matters: without it, a failed run leaves no trace, because cron failures are silent by design. This mysqldump cron backup now runs on its own, and the log tells you whether it worked.
Where to store the backup so it survives a dead server
A backup on the same disk as the database is not a backup: it covers a bad plugin update or an accidental delete, and nothing else. If the disk dies, the backup dies with it. Every wordpress database backup needs at least one copy somewhere else.
The simplest offsite step is a second server: one line in the script copies the new file with rsync or scp to another VPS, the same pattern our guide to backing up your VPS to an external drive automatically every night shows for whole-server backups.
Cloud storage works just as well. If you already use Google Drive, backing up WordPress to Google Drive automatically from your VPS walks through the rclone setup that pushes the nightly dump off the server. For object storage, backing up a VPS to S3-compatible storage with Restic covers encrypted, deduplicated copies that cost little to keep for months. If WordPress runs in containers, read our guide on backing up Docker volumes automatically: the database lives in a volume, not on the host.
Whatever destination you pick, keep the copy automated. A manual upload you do when you remember is a schedule with gaps, and the gap is always the week something breaks.
Pruning old backups before the disk fills up
Nightly dumps pile up fast: a 200 MB database becomes 6 GB in a month, and a full disk stops MySQL from writing at all. Add retention so old dumps delete themselves. Keep seven daily backups and four weekly ones. The daily rule is one line:
find "$BACKUP_DIR" -name "*.sql.gz" -mtime +7 -delete
For the weekly copies, keep the Sunday dump in a separate folder and prune that folder at four weeks. Run the same find command against the offsite copy too, or the remote disk fills up the same way. A backup job that fills the disk is worse than no backup job: it takes the live site down with it.
Testing a restore before you actually need one
An untested backup is a guess: the file can exist and the cron log can say it ran, yet the restore can fail because the dump cut off halfway or a table was corrupt when it was read. Test the restore once a month, on a copy, never on the live site.
Create an empty database on a staging server, then import the newest dump:
gunzip < wordpress-db-2026-12-03_0300.sql.gz
| mysql -u root -p staging_db_name
Then check three things: the import finishes with no errors, the admin loads on the staging copy with a matching post count, and a recent post and comment show their real content. If all three pass, the backup is real. If any fails, fix the backup job the same day, while the live database is still healthy. Use the newest dump each time: restoring a six-month-old file proves nothing about last night’s backup.
When a plugin is the better choice
The script wins on control, but it is not right for every site. An automated wordpress backup plugin is the better call on shared hosting with no SSH access, when a client needs a restore button instead of a terminal, or when you manage a dozen small sites and a dashboard beats a dozen cron jobs.
Plugins such as UpdraftPlus and WPvivid schedule nightly dumps, push them to cloud storage, and restore with a few clicks. Their weakness is the one the table showed: they run inside WordPress, so a bad update that breaks the admin area stops the plugin too. On a VPS, use the script as the primary backup and a plugin as a second layer for its one-click restore. On shared hosting, the plugin is the primary backup.
What to do when the nightly backup fails
Backups fail quietly: cron sends no notification when a job errors, and a missing dump looks exactly like a night with no changes. Build the check into the job. First, log everything, which the cron line already does. Second, add a size check after the dump: if the new file is smaller than a few kilobytes, or if gzip -t reports the archive is corrupt, write an error to the log and exit with a non-zero status. Third, check the log once a week. A quick glance at the last few lines catches a job that stopped after a reboot or a password change.
Common causes are easy to fix: a changed database password breaks the dump until you update the script, a full disk breaks it until you prune old files, and a cron job under the wrong user stops after the account changes. After any server change, run the script by hand once and confirm a fresh file appears.
Set the script up tonight, confirm the first dump and its offsite copy, and schedule a monthly restore test.