Skip to content

Instantly share code, notes, and snippets.

@svet-b
Last active September 9, 2022 08:32
Show Gist options
  • Save svet-b/d385a6f7c1230f11cc4f1588a53abe87 to your computer and use it in GitHub Desktop.
Save svet-b/d385a6f7c1230f11cc4f1588a53abe87 to your computer and use it in GitHub Desktop.
Search-and-replace on Grafana dashboards by manipulating database

It's possible to do search-and-replace on Grafana dashboard definitions by directly manipulating the SQLite database. The key steps after logging into the server are as follows:

cd /var/lib/grafana
# Make backup copy; preserve old backup copies
sudo cp --backup=numbered grafana.db grafana.db.bak

# Stop Grafana
sudo systemctl stop grafana-server

# Open database
sudo sqlite3 grafana.db

# Check how many definitions contain the string to be replaced.
# Note that using isntr() is superior to LIKE, since it's case sensitive and
# deals with multi-line strings correctly
SELECT COUNT(*) FROM dashboard WHERE instr(data, 'replaceme') > 0;

# Do search and replace on dashboard definition
UPDATE dashboard SET data = replace( data, 'replaceme', 'replaced' ) WHERE instr(data, 'replaceme') > 0;

# Can do count check again to ensure replacement has been successful
SELECT COUNT(*) FROM dashboard WHERE data LIKE '%replaceme%';

# Quit SQLite
.quit

# Start Grafana again
sudo systemctl start grafana-server

Some useful SQLite commands:

  • .tables shows all tables
  • .schema tablename shows schema of specific table
  • .headers ON turns on table headers for output of SELECT statements
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment