/grafana-migration-tool

Tool to export Grafana dashboards keeping folder hierarchy and import on the other instance of Grafana

Primary LanguagePythonMIT LicenseMIT

Grafana Migration Tool

Export and Import dashboards in Grafana are extremaly easy.

Unless you have to import 200 dashboards to the new Grafana instance. And the database must be switched from sqlite to MySQL. And some datasources are different. And source 200 dashboards are kept in ellegant directory structure which you want to migrate as well. And at the end it turns out that the migration must be started from the beginning.

To solve such migration problem the below migrating script is created. It is designed for helping with migration of Grafana dashboards from one instance to another (e.g. switching from builtin SQLite to MySQL etc).

Practically is possible to just migrate SQL scripts but it will not work for bigger instances, when the destination Grafana has already some dashboards or finally if datasources are defined differently. With this script is possible also to easily migrate/import only one Grafana directory, so can be served as Grafana Dashboard backup tool.

What does the script do ?

For standard migration we can run it in 3 phases:

  1. Export dashboards keeping directory information.
python grafana-migration.py --export

It saves Grafana folders definition into OUTPUT_DIRECTORY/grafana-folders.json and creates a local directory tree where it laters dumps each of the Dashboard into JSON file keeping name-convention we can see in Grafana UI.

  1. Import Grafana directory structure:
python grafana-migration.py --import_folders

It searches for OUTPUT_DIRECTORY/grafana-folders.json and tries to import the file into destination Grafana. Only directory structure is created in this step.

  1. Import Grafana dashboards per subdirectory
python grafana-migration.py --import_dashboards_from K8S
python grafana-migration.py --import_dashboards_from Cassandra

It connects to remote Grafana to get uid numbers of previosly imported folders and after that reads every file inside folder OUTPUT_DIRECTORY/K8S/*json modifies the Id to match existing folder and then imports. This must be repeated for each of the folders where we have exported dashboard json files.

  1. Import all Grafana dashboards
python grafana-migration.py --import_dashboards_all

It will import all existing dashboards in subdirectories inside the OUTPUT_DIRECTORY.

  1. Delete folders hierarchy from Grafana
python grafana-migration.py --delete_folders

This must be used carefully - as it removes any existing folder with its dashboards. Only dashboards from General folder would survive this operation. Anyway it helps cleanup during testing of this migration.

Preparation

Before executing the grafana-migration.py, you can set two environment variables to override default values :

  • CONFIG_FILE

    File path for the config script config.py.

  • EXPORT_TARGET_DIR

    Path where the exported data are written into.

If the environment variables are not set, CONFIG_FILE will be config.py and EXPORT_TARGET_DIR will be named to exported_dashboards at current directory as default values.

Create API keys for the source and destination Grafana instances. Then inside of the script config.py there are 5 VARIABLES:

GF_URL_SRC - source Grafana url where we import dashboards from

GF_KEY_SRC - API Key for this source Grafana

GF_URL_DST - destination Grafana url where we import dashboards and folders into. Also delete operation uses only this endpoint.

GF_KEY_DST - API Key for this destination Grafana

If deletion option is required then the last variable must be set: SURE_STRING

SURE_STRING = 'Yes I want delete all the dashboards' Otherwise scripts do nothing.