Running a self-hosted Metabase instance gives you full control of your data. But at the same time you are fully responsible for your backup strategy in case something goes wrong. In this post I will share a little backup script how to automatically backup your Metabase instance.
Metabase backup strategy
While backup strategy sounds quite advanced it’s pretty simple for a self-hosted Metabase. In case your instance crashes and you need to do a disaster recovery you just need the following parts:
- The Metabase JAR file
- The Metabase config file
- The Metabase service script
- The Metabase application database
Both parts should be backed up automatically on a regular basis. How often really depends how much changes you are doing in your Metabase. Do your users create new questions (queries) and dashboards every day? Then your backup script should also run daily. If your dashboards are all set up and aren’t touched for weeks then your backup could run less frequently too.
Metabase backup script
Here is my generic Metabase backup script. It can run on remote server (with public key login) and gets a database dump, the Metabase files and compresses it into a metabase-date-tar.gz archive.
#!/bin/bash
SERVER="example.com"
DATABASE="metabase"
USER="metabase"
PASS="xyz"
TMP_DIR="/home/backup/temp"
ARCHIVE_DIR="/home/backup/archives"
NOW=$(date '+%Y-%m-%d-%H%M')
# set pass in env var
export MYSQL_PWD=$PASS
# dump application database
mysqldump -h$SERVER -u$USER --ssl-mode=REQUIRED --quick $DATABASE > $TMP_DIR/metabase.sql
# sync metebase files
rsync --delete -aze ssh $USER@$SERVER:/opt/metabase $TMP_DIR
# create archive
tar -C $TMP_DIR/ -czf $ARCHIVE_DIR/metabase-$NOW.tar.gz .
# delete files from temp dir
rm -rf $TMP_DIR/*
Code language: Shell Session (shell)
To simplify this example server and credentials for database and SSH login are the same here which is rarely the case. But you can simply change it to your needs. The last step would be to schedule the script as a cron job so it runs automatically according to your preferred schedule.
After thoughts
As with any automated backup you need to regularly check if your system works as expected. Also keep in mind that you only backup Metabase files and it’s application database. The databases you have connected to your Metabase for visualization purposes are NOT included in this backup.
