Exclusive Metin2 game shield on FreeBSD: Automatic siege mode, VIP list, and whitelist from your dashboard. See how it works
Create a server

Guide

How to Verify That Your MySQL Backup Can Be Restored

Many backups fail silently: a dump cut short due to lack of space, a table that doesn't fit, or an expired password. You only discover this when you try to restore the data, and if that's the day of the disaster, it's already too late. In this guide, you’ll restore your MySQL or MariaDB backup to a test environment, verify that it’s complete, and schedule the drill.

Before you begin: Don't overwrite your actual database

If your copy was created using --databases or --all-databases, the file includes CREATE DATABASE and USE with the name of the original database. Restoring it on the same server would overwrite the production database. Check this first:

gunzip -c /var/backups/mysql/tienda-2026-01-15.sql.gz | grep -nE '^(CREATE DATABASE|USE )' | head

If the command displays any lines containing CREATE DATABASE or USE, run the test on another VPS or using a copy created as described in Step 1. Even so, it’s best to always restore to a separate server: the dump runs with administrator privileges and, in addition to tables, can create stored procedures, triggers, and events. In the examples, the database is named tienda; use the name of your own database.

1. Keep a separate, restorable copy

If your usual backup contains CREATE DATABASE or USE, create a new backup for testing without --databases. --single-transaction takes a consistent snapshot of the InnoDB tables without blocking the website:

(umask 077; sudo mysqldump --single-transaction --routines --triggers --events tienda | gzip > ~/tienda-prueba.sql.gz)gunzip -c ~/tienda-prueba.sql.gz | tail -n 1

The last line should be -- Dump completed with the date. If it does not appear, the dump was truncated, and that copy is unusable.

2. Restore the copy to a separate test database

Check to see if you have enough free space for a second copy of the database with df -h, create an empty database with a different name, and restore the data to it, timing how long it takes:

sudo mysql -e "CREATE DATABASE tienda_restore" && time (gunzip -c ~/tienda-prueba.sql.gz | sudo mysql tienda_restore)

If CREATE DATABASE fails because the database already exists, do not continue: check its contents before deleting it, or use a different name.

The time it takes is how long your website would be down if you actually had to restore it. If it's close to what you can afford, make a note of it and come up with a plan.

3. Compare it to the original

Count the rows in each table in both databases. The approximate counts from information_schema aren't useful for this in InnoDB; this loop performs a real COUNT(*):

for t in $(sudo mysql -N -e "SHOW FULL TABLES WHERE Table_type='BASE TABLE'" tienda | cut -f1); do  a=$(sudo mysql -N -e "SELECT COUNT(*) FROM \`$t\`" tienda)  b=$(sudo mysql -N -e "SELECT COUNT(*) FROM \`$t\`" tienda_restore)  [ "$a" = "$b" ] && echo "OK   $t $a" || echo "DIF  $t $a / $b"done
  • OK in all of them: the copy is complete.
  • DIF in tables that change frequently, such as sessions or orders: this is normal if the website has continued to operate since the backup.
  • Significant differences in DIF or a missing table: the copy is incomplete. Check for errors in the copying process before relying on it.

If you can, open the app against the restored database as well, and check a couple of recent records.

4. Delete the test database

sudo mysql -e "DROP DATABASE tienda_restore"rm ~/tienda-prueba.sql.gz

Double-check the name before pressing Enter: DROP DATABASE does not ask for confirmation.

5. Schedule a monthly drill

This script restores the most recent backup to a test database, counts its tables, and then deletes it. It stops without restoring anything if the file contains CREATE DATABASE or USE, or if the test database already exists. Adjust DB and the path to your backups:

sudo tee /usr/local/bin/simulacro-mysql.sh > /dev/null <<'EOF'#!/bin/bashset -euo pipefailDB=tiendaPRUEBA="${DB}_restore"COPIA=$(ls -1t /var/backups/mysql/${DB}-*.sql.gz | head -n 1)USOS=$(gunzip -c "$COPIA" | grep -cE '^(CREATE DATABASE|USE )' || true)if [ "$USOS" != "0" ]; then  echo "$(date +%F) $COPIA nombra su base: no se restaura aquí"; exit 1fiif [ -n "$(mysql -N -e "SHOW DATABASES LIKE '$PRUEBA'")" ]; then  echo "$(date +%F) la base $PRUEBA ya existe (¿simulacro anterior fallido?): revísala y bórrala a mano"; exit 1fimysql -e "CREATE DATABASE \`$PRUEBA\`"gunzip -c "$COPIA" | mysql "$PRUEBA"TABLAS=$(mysql -N -e "SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA='$PRUEBA'")mysql -e "DROP DATABASE \`$PRUEBA\`"echo "$(date +%F) $COPIA restaurada: $TABLAS tablas"EOFsudo chmod 700 /usr/local/bin/simulacro-mysql.sh

Schedule it for the 1st of each month in /etc/cron.d/simulacro-mysql and check the log:

0 4 1 * * root /usr/local/bin/simulacro-mysql.sh >> /var/log/simulacro-mysql.log 2>&1

Once a quarter, perform the comparison from Step 3 manually: the script verifies that the backup is restored, but not that it is complete.

ext step

It's safest to run the test on a server other than the production server. Deploy a test Cloud VPS to restore your backups without affecting the actual server.

Related guides