46 lines
1.4 KiB
Bash
46 lines
1.4 KiB
Bash
#!/bin/bash
|
|
|
|
# Variables
|
|
GRAFANA_URL="https://oglog-graf.mytld:3000"
|
|
EXPORT_DIR="../etc/grafana/resources"
|
|
TOKEN_FILE="./grafana_token.txt"
|
|
# Comprobar que el token existe
|
|
if [ ! -f "$TOKEN_FILE" ]; then
|
|
echo "Error: No se encontró el archivo $TOKEN_FILE con el token de Grafana."
|
|
echo "Ejecuta primero setup_grafana_token.sh"
|
|
exit 1
|
|
fi
|
|
|
|
API_TOKEN=$(cat "$TOKEN_FILE")
|
|
|
|
# Crear directorios
|
|
mkdir -p "$EXPORT_DIR/dashboards"
|
|
mkdir -p "$EXPORT_DIR/datasources"
|
|
mkdir -p "$EXPORT_DIR/alerts"
|
|
|
|
# Exportar todos los dashboards
|
|
echo "Exportando dashboards..."
|
|
curl -s -H "Authorization: Bearer $API_TOKEN" "$GRAFANA_URL/api/search?query=&type=dash-db" | jq -c '.[]' |
|
|
while read -r dashboard; do
|
|
uid=$(echo "$dashboard" | jq -r '.uid')
|
|
title=$(echo "$dashboard" | jq -r '.title' | tr ' /' '_' | tr -d '"')
|
|
|
|
curl -s -H "Authorization: Bearer $API_TOKEN" "$GRAFANA_URL/api/dashboards/uid/$uid" \
|
|
| jq '.' > "$EXPORT_DIR/dashboards/${title}.json"
|
|
|
|
echo "Exportado: $title"
|
|
done
|
|
|
|
# Exportar datasources
|
|
echo "Exportando datasources..."
|
|
curl -s -H "Authorization: Bearer $API_TOKEN" "$GRAFANA_URL/api/datasources" \
|
|
| jq '.' > "$EXPORT_DIR/datasources/datasources.json"
|
|
|
|
# Exportar alertas (si usas alert rules v2)
|
|
echo "Exportando alert rules..."
|
|
curl -s -H "Authorization: Bearer $API_TOKEN" "$GRAFANA_URL/api/v1/provisioning/alert-rules" \
|
|
| jq '.' > "$EXPORT_DIR/alerts/alert-rules.json"
|
|
|
|
echo "Exportación completada en: $EXPORT_DIR"
|
|
|