Back Up and Restore a WordPress MySQL Database with mysqldump and Cron

Back up and restore a WordPress MySQL database with mysqldump and cron

A backup is only as good as the restore. This article is about the database part: a daily compressed dump with a retention policy, a copy outside the server, a dedicated backup user, and the restore procedure, tested before it is needed. For a combined backup of files and database, see the bash backup script.

Problem: A plugin update corrupted wp_options, and the only backup is a three-week-old hosting snapshot: everything published since then is lost.

Solution: A script that dumps the database with mysqldump --single-transaction, compresses it, keeps 30 days, copies each dump to remote storage with rclone, and runs from cron every night.

A user for backups

Do not back up as root. A dedicated MySQL user with read-only rights is enough:

CREATE USER 'backup'@'localhost' IDENTIFIED BY 'a-long-random-password';
GRANT SELECT, LOCK TABLES, SHOW VIEW, TRIGGER, EVENT ON helloadmin.* TO 'backup'@'localhost';

Put its credentials into ~/.my.cnf of the system user that runs the backup (chmod 600), so they never appear in the script, the process list or the shell history:

[mysqldump]
user=backup
password=a-long-random-password

The script

/usr/local/bin/wp-db-backup.sh:

#!/usr/bin/env bash
set -euo pipefail

DB_NAME="helloadmin"
BACKUP_DIR="/var/backups/wordpress"
REMOTE_DEST="offsite:wp-backups/"   # an rclone remote (S3, SFTP, ...)
RETAIN_DAYS=30
FILE="$BACKUP_DIR/${DB_NAME}-$(date +%Y%m%d-%H%M%S).sql.gz"

mkdir -p "$BACKUP_DIR"

mysqldump --single-transaction --quick --no-tablespaces --triggers --events "$DB_NAME" \
    | gzip -9 > "$FILE"

echo "Backup saved: $FILE ($(du -h "$FILE" | cut -f1))"

if command -v rclone > /dev/null; then
    rclone copy "$FILE" "$REMOTE_DEST"
fi

find "$BACKUP_DIR" -name "*.sql.gz" -mtime +"$RETAIN_DAYS" -delete
  • --single-transaction takes a consistent snapshot of InnoDB tables without locking the site;
  • --quick streams the rows instead of loading whole tables into memory;
  • set -o pipefail makes the script fail if mysqldump fails, instead of saving an empty or cut archive;
  • mysqldump includes DROP TABLE IF EXISTS before every table by default, so the dump restores cleanly over existing tables.

Run it every night at 2:00 (crontab -e):

0 2 * * * /usr/local/bin/wp-db-backup.sh >> /var/log/wp-db-backup.log 2>&1

Restore

# Straight from the archive
gunzip < /var/backups/wordpress/helloadmin-20240119-020000.sql.gz | mysql helloadmin

# Or with WP-CLI, which takes the credentials from wp-config.php
gunzip -k /var/backups/wordpress/helloadmin-20240119-020000.sql.gz
wp db import /var/backups/wordpress/helloadmin-20240119-020000.sql

# Afterwards
wp cache flush
wp rewrite flush

Restoring needs a user with write rights (not the backup user). If you restore to another domain, follow with wp search-replace for the URLs. After a large import, ANALYZE TABLE refreshes the index statistics.

NOTE: Test the restore regularly on a separate database or server: import the latest dump and open the site. A backup that was never restored may be empty, cut off or missing tables, and you find out at the worst possible moment.