<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Andrii Serhiienko</title>
    <description>The latest articles on Forem by Andrii Serhiienko (@fycth).</description>
    <link>https://forem.com/fycth</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2985%2F29c73c99-a29e-446a-999a-4bfd318d792e.jpg</url>
      <title>Forem: Andrii Serhiienko</title>
      <link>https://forem.com/fycth</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/fycth"/>
    <language>en</language>
    <item>
      <title>Replicating Grafana via Provisioning</title>
      <dc:creator>Andrii Serhiienko</dc:creator>
      <pubDate>Fri, 16 May 2025 21:30:00 +0000</pubDate>
      <link>https://forem.com/fycth/replicating-grafana-via-provisioning-2pb1</link>
      <guid>https://forem.com/fycth/replicating-grafana-via-provisioning-2pb1</guid>
      <description>&lt;p&gt;This guide describes a simple way of exporting configuration from a running Grafana instance, baking provisioning scripts from the configuration, and setting up new Grafana instances based on the exported configuration via the provisioning feature.&lt;/p&gt;

&lt;p&gt;Only basic configuration is exported: dashboards and data sources. If you need to replicate also alerts, users, or other stuff, you should adjust the script in the guide.&lt;/p&gt;

&lt;h2&gt;
  
  
  Export configuration from Grafana
&lt;/h2&gt;

&lt;p&gt;This section describes how you can export configuration from an existing Grafana instance&lt;/p&gt;

&lt;h3&gt;
  
  
  Generate an API token
&lt;/h3&gt;

&lt;p&gt;If you do not have an API token, go to UI and generate it: &lt;strong&gt;Grafana dashboard -&amp;gt; Administration -&amp;gt; Users and Access -&amp;gt; Create Service Account -&amp;gt; Add token&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Shell script to export configurations
&lt;/h3&gt;

&lt;p&gt;The following shell script uses the API token to fetch configurations from Grafana, then it converts the data to a set of YAML and JSON files that can be used for provisioning a new Grafana instance further.&lt;/p&gt;

&lt;p&gt;The following parameters should be set in the script:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;GRAFANA_URL&amp;gt;&lt;/code&gt; - the URL of the existing Grafana instance, accessible to the script. Example: &lt;code&gt;http://127.0.0.1:3000&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;GRAFANA_API_KEY&amp;gt;&lt;/code&gt; - the service account API key you generated in the previous step. Example: &lt;code&gt;glsa_xzWpKux0iIJXdhM123dQO0aHaoVS_fa5a&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;EXPORT_DIR&amp;gt;&lt;/code&gt; - the directory where the results will be saved. Example: &lt;code&gt;/home/admin/grafana-exported-data&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash
set -e

# CONFIG
GRAFANA_URL="&amp;lt;GRAFANA_URL&amp;gt;"
API_KEY="&amp;lt;GRAFANA_API_KEY&amp;gt;"
EXPORT_DIR="&amp;lt;EXPORT_DIR&amp;gt;"
DASHBOARDS_DIR="$EXPORT_DIR/dashboards"
DATASOURCE_YAML="$EXPORT_DIR/datasource.yaml"
DASHBOARD_LOADER_YAML="$EXPORT_DIR/dashboards.yaml"

# Prepare directories
mkdir -p "$DASHBOARDS_DIR"
rm -f "$DATASOURCE_YAML" "$DASHBOARD_LOADER_YAML"

echo "[*] Exporting dashboards..."

# Get all dashboards
DASHBOARDS=$(curl -s -H "Authorization: Bearer $API_KEY" "$GRAFANA_URL/api/search?type=dash-db")

echo "$DASHBOARDS" | jq -c '.[]' | while read -r row; do
    DASH_UID=$(echo "$row" | jq -r '.uid')
    TITLE=$(echo "$row" | jq -r '.title' | tr ' ' '_' | tr -dc '[:alnum:]_')
    echo "  -&amp;gt; $TITLE (uid: $DASH_UID)"

    curl -s -H "Authorization: Bearer $API_KEY" "$GRAFANA_URL/api/dashboards/uid/$DASH_UID" \
        | jq '.dashboard' \
        &amp;gt; "$DASHBOARDS_DIR/$TITLE.json"
done

echo "[*] Exporting data sources..."

# Get data sources
DATASOURCES=$(curl -s -H "Authorization: Bearer $API_KEY" "$GRAFANA_URL/api/datasources")

# Convert to provisioning YAML
echo "apiVersion: 1" &amp;gt; "$DATASOURCE_YAML"
echo "datasources:" &amp;gt;&amp;gt; "$DATASOURCE_YAML"

echo "$DATASOURCES" | jq -c '.[]' | while read -r ds; do
    NAME=$(echo "$ds" | jq -r '.name')
    TYPE=$(echo "$ds" | jq -r '.type')
    URL=$(echo "$ds" | jq -r '.url')
    ACCESS=$(echo "$ds" | jq -r '.access')
    IS_DEFAULT=$(echo "$ds" | jq -r '.isDefault')

    echo "  - name: \"$NAME\"" &amp;gt;&amp;gt; "$DATASOURCE_YAML"
    echo "    type: \"$TYPE\"" &amp;gt;&amp;gt; "$DATASOURCE_YAML"
    echo "    access: \"$ACCESS\"" &amp;gt;&amp;gt; "$DATASOURCE_YAML"
    echo "    url: \"$URL\"" &amp;gt;&amp;gt; "$DATASOURCE_YAML"
    echo "    isDefault: $IS_DEFAULT" &amp;gt;&amp;gt; "$DATASOURCE_YAML"
done

echo "[*] Writing dashboards loader YAML..."

cat &amp;lt;&amp;lt;EOF &amp;gt; "$DASHBOARD_LOADER_YAML"
apiVersion: 1

providers:
  - name: 'default'
    orgId: 1
    folder: ''
    type: file
    options:
      path: /etc/grafana/provisioning/dashboards
      watch_interval: 10s
      allowUiUpdates: true
EOF

echo "[✔] Export complete. Files saved to: $EXPORT_DIR"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make sure the script has 'x' attirbute set: &lt;code&gt;chmod +x grafana-export.sh&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now you run the script. If successful, you should see something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[*] Exporting dashboards...
  -&amp;gt; GuiMiddleware (uid: dewer)
  -&amp;gt; HW_Monitoring (uid: tbgfdvsda)
  -&amp;gt; Loki_stack_monitoring_Promtail_Loki (uid: loki_limpopo)
  -&amp;gt; Sales_Monitor (uid: ilokutyuj)
  -&amp;gt; System_overview (uid: ertgfyj)
[*] Exporting data sources...
[*] Writing dashboards loader YAML...
[✔] Export complete. Files saved to: /home/admin/grafana-exported-data
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Spin up a new Grafana instance with provisioning
&lt;/h2&gt;

&lt;p&gt;This section describes how to spin up a new Grafana instance using the data exported in the previous section for provisioning the new instance.&lt;/p&gt;

&lt;h3&gt;
  
  
  Create a new folder structure
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir -p ~/grafana/provisioning/dashboards
mkdir -p ~/grafana/provisioning/datasources
mkdir -p ~/grafana/data
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Copy exported data to the new folders
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cp /home/admin/grafana-exported-data/dashboards/* ~/grafana/provisioning/dashboards/
cp /home/admin/grafana-exported-data/dashboards.yaml ~/grafana/provisioning/dashboards/dashboards.yaml
cp /home/admin/grafana-exported-data/datasource.yaml ~/grafana/provisioning/datasources/datasource.yaml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Start the docker container
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;podman run -d \
  --name grafana-alt \
  -p 4000:3000 \
  -v ~/grafana/data:/var/lib/grafana:z \
  -v ~/grafana/provisioning/dashboards:/etc/grafana/provisioning/dashboards:z \
  -v ~/grafana/provisioning/datasources:/etc/grafana/provisioning/datasources:z \
  -v ~/grafana/provisioning/dashboards/dashboards.yaml:/etc/grafana/provisioning/dashboards/dashboards.yaml:z \
  -e GF_SECURITY_ADMIN_USER=admin \
  -e GF_SECURITY_ADMIN_PASSWORD=admin \
  grafana/grafana-oss:latest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Navigate to Grafana UI and check
&lt;/h3&gt;

&lt;p&gt;Go to the new Grafana UI (port 4000 in our example). The default password/username combination is admin/admin, it will ask you to set up a new password right after login.&lt;/p&gt;

&lt;p&gt;Now go check the dashboards and data sources, they should be all in place, the same as in the original Grafana instance.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This approach can be used for replicating Grafana based on a pre-defined configuration. For automation of the deployment process, you should adapt the YAML provisioning scripts to a special software like Puppet, Ansible, or another. &lt;/p&gt;

</description>
      <category>grafana</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
