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 --ignore-table-data keeps a table's structure and leaves out its rows

Mariadb
~/tips/tip-database-mariadb-dump-ignore-table-data

mariadb-dump --single-transaction \

Dumps a MariaDB database but keeps only the structure of the tables you name.

Big, throwaway tables make dumps slow and large: session stores, access logs, caches. Skipping them entirely is risky, because the app then fails on a missing table after a restore. MariaDB’s dump tool can keep the table and drop only its contents.

mariadb-dump --single-transaction \
  --ignore-table-data=appdb.sessions \
  --ignore-table-data=appdb.access_log \
  appdb > appdb.sql

The docs say --ignore-table-data dumps only the structure of the named table and not its data. You repeat the option once per table, and each table has to be written with both the database and the table name, such as appdb.sessions. After a restore the tables exist and are empty, which is what most apps want for a cache.

It has a stricter sibling, --ignore-table, which leaves out the table completely, definition included. The docs say it can also be used to skip views. Use it only for things you can truly rebuild without.

The caveat is the same as for any selective dump: you are choosing to lose data. Write down which tables you excluded, and be careful with tables that look like logs but carry state, such as a queue of jobs that have not run yet. The docs also remind you that stored procedures and events are not dumped unless you add --routines and --events, which is a separate gap in many backup scripts.

Restore it the normal way, with the client reading the file: mariadb appdb < appdb.sql.

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