brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: mariadb-dump --where exports only the rows that match a condition

Mariadb
~/tips/tip-database-mariadb-dump-where-slice

mariadb-dump --single-transaction --where="created_at >= '2026-01-01'" appdb orders > orders-2026.sql

Dumps only the table rows that match a SQL condition instead of the whole table.

Sometimes I only need part of a table: the rows of one customer, one year, one site. A full dump plus a lot of deleting is the long way. mariadb-dump can filter while it dumps.

mariadb-dump --single-transaction --where="created_at >= '2026-01-01'" appdb orders > orders-2026.sql

The docs say -w / --where dumps only the rows selected by the condition, and that the quotes around it are mandatory when it contains spaces or characters your shell treats specially. That is easy to forget, and the shell then splits your condition into pieces. It is also why a date comparison like the one above needs the single quotes inside the double quotes.

I use it on one table at a time, since a single condition has to make sense for every table you name on the command line.

Other options from the same page combine well with it. --no-create-info (-t) skips the CREATE TABLE statements, so the file contains only rows, which is what you want when you load the slice into a table that already exists. --insert-ignore makes the statements INSERT IGNORE, so rows that are already there are skipped. --replace writes REPLACE INTO, so they are overwritten.

Caveat: a slice is not a backup. The condition only selects rows of the table you name, so related rows in other tables are not included unless you dump them as well. Treat it as an export for moving or inspecting data, and keep your normal full dump too.

Further reading: MariaDB documentation: mariadb-dump

More Mariadb tips ALTER TABLE t ADD COLUMN z INT INVISIBLE; CREATE SEQUENCE s START WITH 100 INCREMENT BY 10; CREATE OR REPLACE TABLE scratch ( mariadb --init-command="SET SESSION sql_mode='STRICT_ALL_TABLES'" mydb mariadb-admin -u root -p extended-status --relative --sleep=10 \ All Mariadb tips (15) All topics

Link