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:
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:
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:
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:
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).
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.
- Views, triggers and stored routines keep their original definer accounts in the dump.
Recreating one with an arbitrary definer needs
SET_USER_ID, which managed platforms such as RDS do not grant. So during a restore, SafeGrd rewrites each definer to the restoring account and reports how many definitions it changed. - Rows up to the source server's
max_allowed_packetare backed up whole. Setmax_allowed_packeton the restore target at least as high as on the source. - A trigger created through a multi-statement API can end in a semicolon that
mysqldumpexports as invalid SQL. SafeGrd finds these triggers during the backup, and a sandbox drill flags them. An in-memory drill does not parse SQL, so it cannot.
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:
?tls=trueencrypts and checks the server's certificate against the system's certificate authorities.- MySQL's own certificate is self-signed and fails that check. For a server that still uses
it, add
?tls=skip-verifyto encrypt without checking, or?ssl-ca=/path/ca.pemto check it against your own CA. ?tls=preferreduses TLS when the server offers it.
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.
REQUIREDistls=skip-verify.VERIFY_CAandVERIFY_IDENTITYaretls=true.PREFERREDistls=preferred.DISABLEDistls=false.
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:
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:
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:
Give the backup account above these grants so it can read the binary log. On MySQL:
On MariaDB:
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
- Only the resident daemon follows the log.
safegrd daemon run --oncefrom cron cannot, and tells you so. - When the daemon starts again, it carries on where the archive ends. If the server purged that part of the log in the meantime, the daemon tells you, starts from the server's current position and takes a new base. The window starts again from there.
- Keep the binary log for at least a day (
binlog_expire_logs_seconds, orexpire_logs_dayson MariaDB). Then a daemon that is down for a while leaves no gap. binlog_transaction_compression = ONis refused, because the daemon cannot see which database a compressed transaction changes.safegrd doctorchecks that the log is on and in row format, the grants, how long the server keeps the log, and that this host has themysqlbinloga restore needs.
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:
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.
--to-point <name>restores to a restore point.--to latestrestores everything archived.- If the log has a gap before the moment you asked for, the restore is refused and names the gap.
safegrd wal status --surface shopshows the window.
The account you restore with needs ALL on the target database and, on MySQL:
On MariaDB:
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.
- SafeGrd runs
mysqldumpwith--single-transactionand--set-gtid-purged=OFF, which work with a standard user's privileges. - During a restore, SafeGrd rewrites object definers (for example
%oradmin@%) to the restoring account, so RDS does not reject them. - Back up from an Aurora or RDS read replica to keep table scans off your primary.