RefreshDB is a tool for MariaDB/MySQL for effortlessly:
- producing concise and consistent SQL dumps, regardless of the database engine type and version used
- restoring a dump into a database while ensuring the result is consistent
- applying the same cleanup/normalization rules to existing SQL dumps
- providing a very compact schema dump for context in LLMs (large language models)
For that, it offers four modes:
- Dump – Generate a normalized SQL dump (schema + data) from a live database.
- Restore – Restore
doc/database.sqlinto a (freshly dropped and re-created) database. - Repair – Take an existing
doc/database.sqland apply the same cleanup/normalization rules as with--dump. - Dump-Schema – Output a very reduced, minimal schema to
stdout(no data, no extra engine details, etc.) for LLM usage or quick reference.
You can use the PowerShell installation script to set up RefreshDB on Windows:
- Run the following command in PowerShell:
Invoke-Expression (Invoke-WebRequest -Uri https://raw.githubusercontent.com/henno/refreshdb/main/INSTALL.ps1 -UseBasicParsing).Content
This will:
- Add
$HOME\binto your PATH if it's not already included - Create the bin directory if it doesn't exist
- Download the latest refreshdb.php script to your bin directory
- Create a wrapper batch file (
db.bat) so you can run the tool with justdb [options]
php refreshdb.php [--dump | --restore | --repair | --dump-schema]If you installed using the PowerShell script (INSTALL.ps1), you can simply use:
db [--dump | --restore | --repair | --dump-schema]You can only use one mode at a time. If no mode is specified, the script will show usage information.
- If
config.phpis present, parse outDATABASE_USERNAME,DATABASE_HOSTNAME,DATABASE_DATABASE, andDATABASE_PASSWORD. - If
DATABASE_PASSWORDis empty, the-pflag will be omitted from the commands. - Allow for varied quoting (single/double) and spacing.
- Both
constanddefine()forms are possible. - If
config.phpis missing, look for WordPress-styleDB_prefixed constants. Same parsing logic as above. - If neither file is found or needed constants can't be read, output an error and exit.
Both --dump and --repair perform the same normalization/cleanup on the SQL output:
-
--dump- Encoding: Ensures that everything is UTF-8 encoded.
- Source: Reads tables from the live database.
- Streaming: Reads by line and replaces text before writing the line to disk, to avoid running out of memory.
- Output: Writes the processed result to
doc/database.sql(configurable).
-
--repair- Encoding: Can detect if the file is in UTF16LE, but writes the output always in UTF8.
- Source: Reads existing SQL from
doc/database.sql. - Streaming: Reads by line and replaces text before writing the line to disk, to avoid running out of memory.
- Output: Overwrites
doc/database.sqlwith the cleaned version (identical to what--dumpwould produce).
- Adds current DateTime and host name to the top of the file as comments.
- Adds
SET FOREIGN_KEY_CHECKS=0;andSET @@SESSION.sql_mode='NO_AUTO_VALUE_ON_ZERO';to avoid interdependency problems and ensure consistent handling of zero values in auto-increment fields. - Converts
int(11)toint, etc. (remove length parentheses) to make output consistent. - Removes quotes from numeric literals like
'1'or"0"to make output consistent. - Omits any
COLLATEclauses to get around MySQL and MariaDB collation name differences. - Replaces
utf8(whole word only) withutf8mb4to make output consistent. - Removes all
DEFINERreferences from triggers, procedures, etc. because they will cause errors when restoring to a different database that doesn't have the same user. - Consolidates all VALUE blocks of INSERT INTOs onto as few lines as possible, while not exceeding a configured maximum line length (120 characters by default). If adding another VALUE block would exceed that limit, places it on a new line. Keeps each VALUE block intact (does not split it into multiple lines).
- Removes
AUTO_INCREMENTfromCREATE TABLEstatements. - Removes all Mysqldump comments (including "MySQL dump", "Dump", "Server version", "Dump completed on", "MariaDB dump", "Host:", "Current Database:", and separator lines).
- Removes
DROP TABLE IF EXISTSfrom CREATE TABLE statements (restoring starts with a fresh database). - Removes
/*M!999999\- enable the sandbox mode */line (this is a comment that is not supported by older versions of MariaDB).
- Drops the database if it exists and creates it. This prevents leftover tables not in the dump.
- Reads from
doc/database.sql(configurable). - Uses
--binary-modeto ensure binary data is not misread. - Does not do any transformations to the SQL during restore.
- Generates only the table structures (no data) in a very compact format (e.g., no
COLLATE,ENGINE=...,AUTO_INCREMENT=..., etc.). - Outputs the result directly to
stdout, rather than writing to disk. - Primarily intended for quickly obtaining a reduced schema for reference or for use as context with large language models.
Usage:
php refreshdb.php --dump-schema(or db --dump-schema on Windows if installed with the PowerShell script.)
- Outputs total time spent at the end.
- Logs executed commands to help with debugging and troubleshooting.
- Each log is prefixed with
[0.0]where the number is the total time spent in seconds since the start of the script. - If any errors occur, they're printed and the script exits with a non-zero status code.
The script uses default paths and settings that can be modified in the source code:
- Default input/output path:
doc/database.sql - Default maximum line length for INSERT statements: 120 characters