Best way to persist or back up the Sample Database?

Hello,

I’ve created a fairly large dashboard using the Sample Database.
I know this database is volatile, and I’d like to back it up so I don’t have to rebuild everything.

What would be the best solution?

The Sample Database is easier for me because I can upload CSV files directly from the browser.
With PostgreSQL, I would need to place the CSV files in the folder linked to my Postgres database (which is hosted on a VM), and that requires more steps.

PS: For French speakers, we can also exchange in French if that works for you :slightly_smiling_face:
Thanks!

The Sample Database is an H2 database file, located on a filesystem wherever Metabase is running (JAR or container). Here's how you can back it up: Backing up Metabase | Metabase Documentation

On every upgrade, Metabase will replace the Sample Database with its built-in copy, and your changes will be lost.

In order to backup the H2 databases, you must shut down Metabase first. There is no online backup facility. (Metabase would have to support running the H2 backup commands, as it holds the database open while it is running. External H2 admin tools won’t open the database file because it’s locked by Metabase itself.)

Once shut down, you can copy the .db.mv.db file to a safe place, just like any other file. You can also use the Script or Backup tool to generate a portable (text script) or non-portable (binary) backup copy. See the H2 tools docs for details. To invoke these tools, run Java like so to access the tools in the Metabase jar:

To create a text backup called script.zip (note the JDBC URL omits the .mv.db extension):

java --add-opens java.base/java.nio=ALL-UNNAMED -cp metabase.jar org.h2.tools.Script -url jdbc:h2:/path/to/metabase/plugins/sample-database.db -script script.zip -options compression zip 

To create a binary backup called backup.zip:

cd /directory/with/plugins/sample-database.db.mv.db
java --add-opens java.base/java.nio=ALL-UNNAMED -cp metabase.jar org.h2.tools.Backup -file backup.zip

Just so it’s said, Metabase chose to use a bizarre point version of H2 with a file format that is not supported by the H2-shipped tools. You must use the ones that are in the Metabase jar if you want to access a Metabase-generated H2 database file.

That is not strictly correct with respect to PostgreSQL. While you can use the legacy database-server-local file access versions of COPY (and really shouldn’t), just create and configure a database connection to receive the CSVs and you can use the CSV upload feature to upload CSVs to PostgreSQL.