Docs / Surfaces / Databases / MySQL and MariaDB

MySQL and MariaDB

You can back up MySQL 8.0 and 8.4 and MariaDB 10.6 and 11.4 with one command. SafeGrd runs mysqldump on your host:

# MySQL 8.0 and 8.4, MariaDB 10.6 and 11.4 (each run by the test suite)
safegrd backup --database-url "mysql://backup:$DB_PASSWORD@db.internal:3306/shop" --retention-days 14

SafeGrd runs mysqldump (or mariadb-dump) with --single-transaction. It reads InnoDB tables from one consistent snapshot without locking them. The backup includes stored routines, triggers and events, and writes binary columns as hex. The dump streams into the encrypted backup as it is produced. SafeGrd counts rows from the dump stream itself, so the counts match the rows in the backup even while your app writes to the database.

The account to back up as

Give SafeGrd an account that reads and changes nothing. On MySQL 8.0.20 and newer:

CREATE USER 'safegrd_backup'@'%' IDENTIFIED BY '...';
GRANT SELECT, SHOW VIEW, TRIGGER, LOCK TABLES, EVENT ON shop.* TO 'safegrd_backup'@'%';
GRANT SHOW_ROUTINE ON *.* TO 'safegrd_backup'@'%';

TRIGGER and EVENT let mysqldump read the database's triggers and scheduled events. SHOW_ROUTINE lets it read every stored procedure and function, including those other accounts created. If one of these grants is missing, the backup still runs and tells you what it left out.

MariaDB and MySQL before 8.0.20 have no SHOW_ROUTINE. Grant the first line's privileges there, and the backup holds the routines the account can read. safegrd doctor warns you when a surface's account can write, and prints these grants.

The sandbox for Fire Drills

A sandbox drill loads the dump into a database used for nothing else, counts its tables and rows, then drops everything it loaded: tables, views, routines and events. Name that database with drill.sandbox_url on the host, or with --sandbox-target. It has to be empty before each drill. Give the account every right on it and none elsewhere:

CREATE DATABASE shop_sandbox;
CREATE USER 'safegrd_sandbox'@'%' IDENTIFIED BY '...';
GRANT ALL ON shop_sandbox.* TO 'safegrd_sandbox'@'%';

As the dump loads, SafeGrd rewrites every DEFINER in it to the sandbox account. A view, trigger or routine owned by another account then restores without SUPER.

On a self-hosted MySQL with the binary log on (the default since 8.0), an account without SUPER can create a trigger or function only if the server sets log_bin_trust_function_creators = 1. Managed servers set it. Without it, the drill fails at the first trigger and tells you why.

Incremental backups

Each backup uploads only what changed since the last run. It is a run of the surface's repository, like a PostgreSQL backup. SafeGrd cuts the dump at each table into a header, one file per table (its structure, rows and triggers), and a tail with the routines, events and view definitions.

A table nobody wrote to since the last run gives the same bytes, so it uploads nothing. A table that changed uploads the 64 KiB chunks around the rows that moved. Every run still reads the whole database and is a restore point. The snapshot detail in the console shows what each table uploaded.

An incremental backup reads its tables over four connections at once (parallel on the surface), all at the same moment of the database. To open them at one moment, the backup holds writes while it opens the connections and reads the schema. That usually takes under a second, and the backup prints how long. On MySQL 8.0.23 and newer it needs one more grant:

GRANT FLUSH_TABLES ON *.* TO 'safegrd_backup'@'%';

On MariaDB and older MySQL, grant RELOAD ON *.* instead. If the grant is missing, or statements already running hold up the lock for 5 seconds, the backup reads one table at a time and tells you. parallel: 1 always reads one table at a time, and so does a full backup (--format tar).

Bars to scale. The database is 270 MB. A run after 400 rows were updated across a 300,000-row table uploaded 20 MB. A run after 2,000 rows were appended to one table uploaded 0.5 MB. A run with no writes uploaded 0.1 MB. The database 270 MB 400 rows updated across a 300,000-row table 20 MB 2,000 rows appended to one table 0.5 MB A run with no writes 0.1 MB
What one run uploaded, measured on a 270 MB database. Bars are to scale, so the two smallest are drawn at the minimum width.

On a 270 MB database, a run with no writes uploaded 0.1 MB, and 2,000 rows appended to one table uploaded 0.5 MB. 400 rows updated across a table of 300,000 uploaded 20 MB, the chunks around each row. A row inserted into or deleted from the middle of a table costs about the same as an update. mysqldump packs rows into INSERT statements by size, so every statement after that row shifts.

For one archive per backup instead, use --format tar, or format: tar on a surface. The archive is the dump as mysqldump wrote it, and once you decrypt it, mysql loads it without SafeGrd. When you add a surface in the console, Backup type sets this.

The change log (--change-log) works for PostgreSQL only. A MySQL run reads every table each time.

Restore

A run restores whole into an empty database: safegrd restore --snapshot <id> --target mysql://…. SafeGrd checks every table's restored row count against the snapshot manifest. MySQL does not run DDL statements inside a transaction, so a failed restore can leave part of the schema behind. Drop and recreate the database before you try again.

You can also restore one table into a database that has other tables: safegrd restore --snapshot <id> --table orders --target mysql://…. This loads the dump's section for that table, with its rows and triggers. If the target has no such table, SafeGrd creates it as the backup defined it. If the table exists, it has to be empty and have the same columns. In the console, the Tables tab has Restore table for the same thing. A full backup (tar) is one dump, so it only restores whole.

Client tools, password and TLS

The host needs the MySQL client (mysql-client, or brew install mysql-client on macOS). SafeGrd prefers the dump tool from the same project as the server. MySQL's client has also been tested against MariaDB 11.4. To name the tools yourself, set SAFEGRD_MYSQLDUMP and SAFEGRD_MYSQL.

SafeGrd passes the password to the client in an owner-only options file, which it deletes when the run ends. The password is never on the client's command line.

Choose TLS in the URL:

If your provider has its own CA (Aiven, DigitalOcean, RDS), name it on a host with ?ssl-ca=/path/ca.pem, or with ca_file on the surface in the config. For a database SafeGrd backs up for you, paste the CA certificate when you add it, or later with CA certificate on the surface. Each backup then checks the server's certificate and host name against it. A URL that also says tls=skip-verify or ssl-mode=PREFERRED is refused, because it would check nothing.

You can use ssl-mode in place of tls, as mysql takes it and as Aiven's URIs carry it. A URL that sets both to different values is refused.

Check the backups another tool makes

If Coolify, a cron mysqldump or another tool already backs up the database, SafeGrd can check those backups where that tool stores them instead of taking its own. Set it up as for a PostgreSQL surface, with type: mysql:

surfaces:
- id: shop-db
type: mysql
schedule: daily
watch:
url: s3://my-backups/data/coolify/backups/databases/team-0/shop-db
endpoint: https://ACCOUNT.r2.cloudflarestorage.com
access_key: env:R2_KEY_ID
secret_key: env:R2_SECRET

SafeGrd reads the plain SQL that mysqldump and mariadb-dump write, gzipped or not. That includes Coolify's mysql-dump-*.dmp and mariadb-dump-*.dmp. The check counts each table's rows from its INSERT statements. A dump without its -- Dump completed line counts as cut short. A dump of several databases (--all-databases, Coolify's "all databases" backup) is checked, but drilled in memory only, because loading it would write beside the sandbox.

A Fire Drill loads the dump into the sandbox through the mysql or mariadb client, with each DEFINER set to the sandbox account. It then compares every table's rows. The client stops at the first error, and MySQL commits each statement as it runs. So a failed load leaves part of the dump in the sandbox, and the drill empties it afterwards. drill.sandbox_url must name a MySQL or MariaDB database. A sandbox of another engine is refused before anything connects.

Under one Coolify tree, a type: mysql surface reads only the MySQL and MariaDB files. The PostgreSQL and MongoDB backups beside them need surfaces of their own type.

If you have no host, add the surface in the console under Back up on SafeGrd and tick Check the backups another tool makes, as the PostgreSQL page describes. SafeGrd holds the bucket key, and checks and drills the backups on the machines it starts.

Restore to a point in time

A backup restores the database as it was when the run started. Add a wal: block to the surface and the resident daemon also follows the server's binary log. You can then restore the database to any second in a window, 7 days by default:

surfaces:
  - id: shop
    type: mysql
    database_url: mysql://safegrd_backup:...@db.internal:3306/shop
    wal: {}

The server has to keep a binary log in row format. MySQL 8.0 and newer do by default. On MariaDB, set this in my.cnf and restart:

[mysqld]
log_bin = mysql-bin
server_id = 1
binlog_format = ROW

Give the backup account above these grants so it can read the binary log. On MySQL:

GRANT REPLICATION SLAVE, REPLICATION CLIENT, FLUSH_TABLES ON *.* TO 'safegrd_backup'@'%';

On MariaDB:

GRANT REPLICATION SLAVE, BINLOG MONITOR ON *.* TO 'safegrd_backup'@'%';

REPLICATION SLAVE reads the log, and REPLICATION CLIENT (MariaDB: BINLOG MONITOR) reads where it ends. FLUSH_TABLES (RELOAD before MySQL 8.0.23) lets a base backup hold writes for the moment its snapshot opens, so the log position it records is exact. MariaDB reads that position from the snapshot and needs no lock. The binary log belongs to the whole server, so with these grants the account can read the changes to every database on it.

The daemon reads the log as a replica does. Every 5 seconds (interval_seconds) it pushes the transactions committed since the last push to your bucket, compressed and encrypted on the host and unlocked. Once a day it joins them into one locked bundle and deletes the unlocked copies. On hosted storage, this comes with Growth (7 days) and Scale (30 days).

The archive holds the whole server's log, encrypted, with every database's changes. A restore replays only this surface's database.

A base backup is a mysqldump of the database that records the log position of its snapshot. The daemon takes one when it starts following the log, and again every 7 days (base_every_days). To take one now, run safegrd wal base --surface shop.

Keep the log reachable

How the daemon checks it

Once a day the daemon records a restore point: the log position and every table's row count, read from the same snapshot. Every 7 days it restores the archive into the surface's drill.sandbox_url, to the newest restore point and to the end, and checks the counts. To run this check now, use safegrd wal drill --surface shop. Without a sandbox the check does not run, and the console tells you why.

Restore to a moment

Restore into an empty database:

safegrd wal restore --surface shop --to "2026-10-17 14:52:00" --target-url "mysql://restorer:...@db.internal:3306/shop_restored"

In the console, the surface's Point in time tab writes this command for a moment you pick.

The command loads the newest base backup from before that moment (UTC). It then replays every transaction of this database committed by then, using the server's own mysqlbinlog and mysql. MySQL stamps each commit to the microsecond. MariaDB stamps it to the second, so a transaction committed in the same second as the moment you give is not replayed.

The account you restore with needs ALL on the target database and, on MySQL:

GRANT SESSION_VARIABLES_ADMIN, BINLOG_ADMIN ON *.* TO 'restorer'@'%';

On MariaDB:

GRANT BINLOG REPLAY ON *.* TO 'restorer'@'%';

The replay keeps a statement if it ran with this database as its default (USE shop), as MySQL replication's own database filter does. A statement that changed shop while another database was the default is not replayed. Row changes are kept by the table they change, whatever the default database was.

PlanetScale does not give access to the binary log ("PlanetScale does not currently support binlog replication to external databases"), so point-in-time recovery is not available there. Regular backups of a PlanetScale database work as on any MySQL server.

Amazon RDS for MySQL keeps no binary log for an outside reader until you set a retention, up to 168 hours: CALL mysql.rds_set_configuration('binlog retention hours', 24);

Amazon RDS and Aurora (MySQL)

RDS does not grant SUPER, and SafeGrd does not need it.