Daily Database Backups WordPress: Simple Script Setup

Daily Database Backups WordPress: Simple Script Setup

Your WordPress posts, pages, comments, and settings all live in MySQL. If that database is lost or damaged, the files on disk are not enough to bring the site back. You need daily database backups wordpress can restore from, kept somewhere safe and tested now and then. This guide shows you how to build that with one small shell script, a cron job, and a second copy stored off the server. No plugin to buy, no control panel needed, just mysqldump, gzip, and a schedule you can check.

Why Your WordPress Database Needs Its Own Backup

Many backup plugins store their copies on the same server as the site. That helps if you delete a post by mistake, but it does not help if the disk fails or the whole server goes down. A database backup only does its job when it lives somewhere else too. That is why this setup has two parts: a local dump you can reach fast, and an offsite copy you can reach when the server is gone. If you want a clearer picture of what belongs where, read our guide on how to separate WordPress uploads, database, and code for easier backups before you start.

Daily Database Backups WordPress: What the Script Does

The script dumps the database with mysqldump, compresses it with gzip, saves it with the date in the filename, deletes local files older than your retention limit, and copies the new file off the server. Each step is one or two lines of shell. You can read the whole script in under a minute, and you can change any part of it later without learning a new tool. That is the point of a mysqldump backup script you own: nothing hidden, nothing that stops working when a plugin updates or a license ends.

Write the mysqldump Backup Script

Start by keeping the database password out of the script itself. Create a credentials file in your home directory that only your user can read:

cat > ~/.my.cnf << 'EOF'
[client]
user=wp_user
password=your-database-password
host=localhost
EOF
chmod 600 ~/.my.cnf

Replace wp_user and the password with the values from your wp-config.php. The chmod 600 step matters: it stops other users on the server from reading the file. Now open /usr/local/bin/wp-db-backup.sh in your editor, paste in the script below, and replace wordpress_db with your database name:

#!/bin/bash
set -e
DB_NAME="wordpress_db"
BACKUP_DIR="/var/backups/wordpress"
DATE=$(date +%Y-%m-%d)
FILE="$BACKUP_DIR/$DB_NAME-$DATE.sql.gz"

mkdir -p "$BACKUP_DIR"
mysqldump --single-transaction "$DB_NAME" | gzip > "$FILE"
echo "$(date): backup saved to $FILE" >> /var/log/wp-db-backup.log

The --single-transaction flag takes a steady snapshot of InnoDB tables without locking the site, so visitors can keep reading while the dump runs. The set -e line stops the script at the first error instead of writing an empty file and calling it a backup. Make the script executable and run it once by hand:

chmod +x /usr/local/bin/wp-db-backup.sh
sudo /usr/local/bin/wp-db-backup.sh
ls -lh /var/backups/wordpress/

You should see one file named like wordpress_db-2026-11-22.sql.gz. Check that its size is greater than zero and that the log line was written. If mysqldump reports access denied, the username or password in ~/.my.cnf is wrong, or the file belongs to a different user than the one running the script. Fix the credentials and run it again before you move on. A fuller walkthrough of the same tool is in our guide on how to back up MySQL databases automatically with mysqldump and cron.

Schedule It with a WordPress Backup Cron

A script that only runs when you remember it is not a backup plan. Cron runs it for you. Open the crontab for the user that owns the backup directory and add one line that runs the script at 2 a.m. every day:

crontab -e
0 2 * * * /usr/local/bin/wp-db-backup.sh

Two in the morning is a common pick because traffic is usually low then, but any quiet hour works. Once the wordpress backup cron runs each night, your daily database backups wordpress routine needs no attention until something changes, like a new database name or a server move.

Check the next morning that the new dated file appeared. If it did not, look in two places: the log file at /var/log/wp-db-backup.log and the cron log for the user that owns the job. The usual causes are a wrong path in the crontab line, a script that is not executable, or a credentials file owned by the wrong user. Cron runs with a bare environment, so always use full paths in the script and in the crontab line.

Set a Backup Retention Policy That Frees Disk Space

Daily dumps add up, and without cleanup the backup directory will fill the disk. A full disk can stop MySQL itself. A backup retention policy decides how long each file stays before it is deleted.

Fourteen days is a good starting point for most small sites. It gives you two weeks to notice a problem and still find a good copy. Larger sites with tight disks may keep seven days locally and lean on the offsite copy for anything older. Add one line to the end of the script, after the dump line, so cleanup runs with every backup:

find "$BACKUP_DIR" -name "*.sql.gz" -mtime +14 -delete

This deletes local files older than 14 days. Change +14 to +30 if you have room for a month of copies. Test the line by hand first with -print instead of -delete so you can see exactly which files it would remove. Never point a find-and-delete command at a directory you have not checked, and never run it against the live database directory. For the trade-offs between short and long retention, see our guide on how to archive old backups with retention policies that save disk space.

Send a Copy to Offsite Backup Storage

The local copy is for fast restores. The offsite copy is for the day the server itself is gone. Offsite backup storage can be a second VPS, a storage bucket, or any server you control in another location.

The simplest transfer is rsync over SSH to that second server. Add this line to the script after the dump, using your own user, host, and path:

rsync -az "$FILE" user@backup-server:/var/backups/wordpress/

Set up an SSH key for the transfer so cron can run it without a password prompt. If you would rather use object storage, rclone works the same way: configure one remote once, then copy each new file to it with a single command. Either way, the rule is the same. The offsite copy must sit on different hardware from the site, and the credentials that write to it must not be stored on the same server in plain text where a break-in would hand over both copies at once.

Check the offsite copy the morning after the first run. List the files on the remote side and compare the newest filename and size with the local one. If the sizes do not match, the transfer failed partway. Dated filenames make a failed night easy to spot and easy to fix.

Test the Restore Before You Need It

A backup you have never restored is a guess. Test the restore once a month, on a test database that is not your live site, so the daily database backups wordpress routine you built is more than a folder of files. Create an empty database, load the newest dump into it, and check that the tables are there:

mysql -e "CREATE DATABASE wp_restore_test;"
gunzip -c /var/backups/wordpress/wordpress_db-2026-11-22.sql.gz | mysql wp_restore_test
mysql -e "SHOW TABLES FROM wp_restore_test;" | head -20

Then open the test database and check a row you know, such as the site URL in wp_options. If the tables load and the values look right, the backup works. If the import fails halfway, find out why now, while the live site is still fine. Common causes are a dump that stopped early because the disk filled up, or a credentials file that changed. Drop the test database with mysql -e "DROP DATABASE wp_restore_test;" when you are done.

Do the same check with a copy pulled from the offsite location at least once, not just the local file. The offsite copy is the one you will reach for in a real failure, so it is the one that has to work. For a full drill that covers files as well as the database, follow our guide on how to test your VPS backup by restoring it before you actually need it. Check the log once a week and test a restore once a month, and the setup will keep running itself.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.