MySQL Binary Log and Point-in-Time Recovery for WordPress

Point-in-time recovery for WordPress with the MySQL binary log

A nightly backup protects against losing everything, but not against losing a day. When a faulty plugin update or a wrong DELETE destroys data at 15:42, restoring last night’s dump also throws away every order and comment since midnight. MySQL’s binary log records every change; with a full backup plus the binary logs since then, the database can be restored to the second before the mistake.

Problem: At 15:42 a bulk action in a shop plugin deleted 3,000 products’ meta. The last backup is from 02:00, and 140 orders came in since then.

Solution: Enable the binary log with enough retention, make the nightly mysqldump consistent and record its binlog position, and in an emergency restore the dump into a new database, replay the binary logs from the recorded position up to just before the bad statement with mysqlbinlog --stop-datetime or --stop-position, and switch WordPress to it.

Enable the binary log

# /etc/mysql/mysql.conf.d/zz-binlog.cnf
[mysqld]
server_id                  = 1
log_bin                    = /var/log/mysql/mysql-bin
binlog_format              = ROW
binlog_expire_logs_seconds = 604800      # keep 7 days
max_binlog_size            = 256M

MySQL 8.0 enables the binary log by default; MariaDB and older versions need log_bin. ROW format logs the changed rows, which replays exactly. The retention must be longer than the interval between full backups, otherwise the logs needed for a recovery are already deleted.

A backup that knows its binlog position

mysqldump --single-transaction --source-data=2 --routines --triggers \
    --databases wordpress | gzip > /backup/wordpress-$(date +%F).sql.gz

--single-transaction takes a consistent snapshot of InnoDB tables without locking the site. --source-data=2 (on MySQL before 8.0.26 and on MariaDB: --master-data=2) writes the binlog file and position of that snapshot into the dump as a comment:

zcat /backup/wordpress-2025-09-20.sql.gz | grep -m1 'CHANGE REPLICATION SOURCE\|CHANGE MASTER'
# -- CHANGE REPLICATION SOURCE TO SOURCE_LOG_FILE='mysql-bin.000412', SOURCE_LOG_POS=81234;

The nightly job itself, with cron and rotation: back up WordPress with mysqldump and cron.

Find the bad statement

mysqlbinlog --base64-output=DECODE-ROWS -vv \
    --start-datetime="2025-09-20 15:40:00" --stop-datetime="2025-09-20 15:45:00" \
    /var/log/mysql/mysql-bin.000415 | grep -n -B2 -A8 'DELETE FROM `wordpress`.`wp_postmeta`' | sed -n 1,40p
# # at 5532190
# #250920 15:42:07 server id 1  end_log_pos 5532301 ... Delete_rows ...

The # at line before the event is its start position. Replaying up to that position (exclusive) restores everything before the deletion.

Restore into a new database

mysql -e "CREATE DATABASE wordpress_restore"
zcat /backup/wordpress-2025-09-20.sql.gz | sed 's/^USE `wordpress`;/USE `wordpress_restore`;/; /^CREATE DATABASE/d' | mysql

mysqlbinlog --start-position=81234 /var/log/mysql/mysql-bin.000412 /var/log/mysql/mysql-bin.000413 \
            /var/log/mysql/mysql-bin.000414 --database=wordpress --rewrite-db='wordpress->wordpress_restore' > /tmp/replay-1.sql
mysqlbinlog --stop-position=5532190 /var/log/mysql/mysql-bin.000415 \
            --database=wordpress --rewrite-db='wordpress->wordpress_restore' > /tmp/replay-2.sql
mysql < /tmp/replay-1.sql && mysql < /tmp/replay-2.sql

The start position applies to the first file, the stop position to the last; both are therefore separate calls. --rewrite-db redirects the events into the restore database, so the live database stays untouched until the result is checked. Replay from the binary logs of the same server and do not run the generated SQL with binary logging on for the restore (add SET sql_log_bin=0; at the top) if replicas are attached.

Then compare (for example the number of rows in wp_postmeta and the newest orders), set DB_NAME in wp-config.php to wordpress_restore, and flush the object cache.

Events after the mistake

Everything after 15:42 is not in the restore. If orders came in after the deletion, replay the logs after the bad event as well (--start-position at the event after it), or export those orders from the old database and import them. That is the hard part of every point-in-time recovery, and the reason to put the site into maintenance mode as soon as the mistake is noticed.

NOTE: Binary logs contain all data changes, including personal data and password hashes: protect /var/log/mysql like the backups. Practice the procedure once on a staging server; under pressure, the positions and file names are where mistakes happen.