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: the one-database option of mariadb restores a single database from a full dump

Database
~/tips/tip-database-mariadb-one-database-restore

mariadb -e 'CREATE DATABASE IF NOT EXISTS blog'

Pulls one database out of a full server dump without touching the others.

You have one big --all-databases dump and only the blog database is broken. Cutting the file apart with sed is no fun, and the client can do the filtering for you.

mariadb -e 'CREATE DATABASE IF NOT EXISTS blog'
mariadb --one-database blog < all.sql

The short form is -o. The client starts in blog and skips every statement that runs while another database is selected. Because a full dump has a USE line before each database, only the blog part gets executed. A row changed in another database before the restore stays changed afterwards.

The filter is simple, and the docs say so: it only follows USE statements. A statement that names another database directly, like UPDATE shop.orders ..., still runs while blog is the current database. Normal dumps do not write statements like that, but views, triggers or hand-edited SQL can.

So I’d try this on a scratch server or a copy first. It is a restore, and it will overwrite whatever is in blog right now, because the dump drops and recreates each table. The same option exists in the MySQL mysql client.

Further reading: mariadb command-line client options

More Database tips mariadb-dump --no-create-info --insert-ignore shop orders > orders-rows.sql SELECT table_schema, table_name, CHECKSUM TABLE shop.orders; services: services: All Database tips (53) All topics

Link