CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-grafana-backup

A Python-based application to backup Grafana settings using the Grafana API

Pending
Overview
Eval results
Files

configuration.mddocs/

Configuration Management

Comprehensive configuration loading and processing system that handles JSON files, environment variables, and default settings. The configuration system manages Grafana API connections, authentication methods, cloud storage credentials, and operational parameters.

Capabilities

Main Configuration Loader

The primary configuration function that loads settings from multiple sources with environment variable override support.

def main(config_path):
    """
    Load and process configuration from JSON file with environment variable overrides
    
    Args:
        config_path (str): Path to JSON configuration file
        
    Returns:
        dict: Complete configuration dictionary with all settings processed
    """

Configuration Helper Functions

def load_config(path=None):
    """
    Load JSON configuration file with error handling
    
    Module: grafana_backup.commons
    Args:
        path (str): Path to JSON configuration file
        
    Returns:
        dict: Parsed JSON configuration data
    """

Configuration Structure

Grafana Connection Settings

# Configuration keys for Grafana API connection
GRAFANA_URL: str  # Grafana server URL (e.g., "http://localhost:3000")
TOKEN: str  # API token for authentication
SEARCH_API_LIMIT: int  # Search result limit (default: 5000)
DEFAULT_USER_PASSWORD: str  # Default password for restored users
GRAFANA_VERSION: str  # Optional Grafana version override
GRAFANA_ADMIN_ACCOUNT: str  # Admin username for basic auth operations
GRAFANA_ADMIN_PASSWORD: str  # Admin password for basic auth operations
GRAFANA_BASIC_AUTH: str  # Base64 encoded basic auth credentials

General Operation Settings

# General configuration keys
DEBUG: bool  # Enable debug logging
API_HEALTH_CHECK: bool  # Perform API health checks
API_AUTH_CHECK: bool  # Verify API authentication
VERIFY_SSL: bool  # Enable SSL certificate verification
CLIENT_CERT: str  # Path to client certificate file
BACKUP_DIR: str  # Backup output directory (default: "_OUTPUT_")
BACKUP_FILE_FORMAT: str  # Timestamp format for backup files
UID_DASHBOARD_SLUG_SUFFIX: bool  # Include UID in dashboard filenames
PRETTY_PRINT: bool  # Format JSON output with indentation
EXTRA_HEADERS: dict  # Custom HTTP headers for API requests

HTTP Headers Configuration

# HTTP header dictionaries for API requests
HTTP_GET_HEADERS: dict  # Headers for GET requests
HTTP_POST_HEADERS: dict  # Headers for POST requests  
HTTP_GET_HEADERS_BASIC_AUTH: dict  # GET headers with basic auth
HTTP_POST_HEADERS_BASIC_AUTH: dict  # POST headers with basic auth

AWS S3 Configuration

# AWS S3 cloud storage settings
AWS_S3_BUCKET_NAME: str  # S3 bucket name
AWS_S3_BUCKET_KEY: str  # S3 key prefix for backups
AWS_DEFAULT_REGION: str  # AWS region
AWS_ACCESS_KEY_ID: str  # AWS access key
AWS_SECRET_ACCESS_KEY: str  # AWS secret key
AWS_ENDPOINT_URL: str  # Custom S3 endpoint URL (for S3-compatible services)

Azure Storage Configuration

# Azure Storage cloud storage settings
AZURE_STORAGE_CONTAINER_NAME: str  # Azure container name
AZURE_STORAGE_CONNECTION_STRING: str  # Azure storage connection string

Google Cloud Storage Configuration

# Google Cloud Storage settings
GCS_BUCKET_NAME: str  # GCS bucket name
GOOGLE_APPLICATION_CREDENTIALS: str  # Path to GCS service account key file

InfluxDB Monitoring Configuration

# InfluxDB metrics collection settings
INFLUXDB_MEASUREMENT: str  # InfluxDB measurement name
INFLUXDB_HOST: str  # InfluxDB server hostname
INFLUXDB_PORT: int  # InfluxDB server port
INFLUXDB_USERNAME: str  # InfluxDB username
INFLUXDB_PASSWORD: str  # InfluxDB password
INFLUXDB_DATABASE: str  # InfluxDB database name

Timestamp Configuration

# Timestamp generation
TIMESTAMP: str  # Current timestamp using BACKUP_FILE_FORMAT

Configuration File Format

Basic Configuration Example

{
  "general": {
    "debug": true,
    "verify_ssl": true,
    "api_health_check": true,
    "api_auth_check": true,
    "backup_dir": "_OUTPUT_",
    "backup_file_format": "%Y%m%d%H%M",
    "uid_dashboard_slug_suffix": false,
    "pretty_print": false
  },
  "grafana": {
    "url": "http://localhost:3000",
    "token": "{YOUR_GRAFANA_TOKEN}",
    "search_api_limit": 5000,
    "default_user_password": "00000000",
    "admin_account": "admin",
    "admin_password": "admin"
  }
}

Cloud Storage Configuration Example

{
  "aws": {
    "s3_bucket_name": "my-grafana-backups",
    "s3_bucket_key": "grafana-backup",
    "default_region": "us-east-1",
    "access_key_id": "AKIAIOSFODNN7EXAMPLE",
    "secret_access_key": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
  },
  "azure": {
    "container_name": "grafana-backups",
    "connection_string": "DefaultEndpointsProtocol=https;AccountName=..."
  },
  "gcp": { 
    "gcs_bucket_name": "my-grafana-backups",
    "google_application_credentials": "/path/to/service-account.json"
  }
}

InfluxDB Monitoring Configuration Example

{
  "influxdb": {
    "measurement": "grafana_backup",
    "host": "localhost",
    "port": 8086,
    "username": "monitoring",
    "password": "monitoring_password",
    "database": "grafana_metrics"
  }
}

Environment Variable Overrides

All configuration values can be overridden using environment variables:

Grafana Settings

export GRAFANA_URL="https://grafana.example.com"
export GRAFANA_TOKEN="eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk"
export SEARCH_API_LIMIT=1000
export DEFAULT_USER_PASSWORD="newuserpass"
export GRAFANA_ADMIN_ACCOUNT="admin"
export GRAFANA_ADMIN_PASSWORD="admin123"
export GRAFANA_BASIC_AUTH="YWRtaW46YWRtaW4xMjM="

General Settings

export DEBUG=true
export VERIFY_SSL=false
export BACKUP_DIR="/custom/backup/path"
export BACKUP_FILE_FORMAT="%Y-%m-%d_%H-%M-%S"
export PRETTY_PRINT=true

AWS Settings

export AWS_S3_BUCKET_NAME="production-grafana-backups"
export AWS_DEFAULT_REGION="us-west-2"
export AWS_ACCESS_KEY_ID="AKIAIOSFODNN7EXAMPLE"
export AWS_SECRET_ACCESS_KEY="wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"

Custom Headers

export GRAFANA_HEADERS="X-Custom-Header:value,X-Another-Header:value2"

Configuration File Locations

The configuration system searches for configuration files in this order:

  1. Command line specified: --config=/path/to/config.json
  2. User home directory: ~/.grafana-backup.json
  3. Package default: grafana_backup/conf/grafanaSettings.json

Usage Examples

Load Configuration from Default Location

from grafana_backup.grafanaSettings import main as load_config

# Load from default locations (searches in order)
settings = load_config(None)

Load Configuration from Specific File

# Load from specific file path
settings = load_config('/path/to/custom-config.json')

Access Configuration Values

# Access configuration values
grafana_url = settings['GRAFANA_URL']
backup_dir = settings['BACKUP_DIR']
aws_bucket = settings.get('AWS_S3_BUCKET_NAME', '')
debug_mode = settings['DEBUG']

Configuration with Environment Variables

import os

# Set environment variables
os.environ['GRAFANA_URL'] = 'https://grafana.production.com'
os.environ['DEBUG'] = 'false'

# Load configuration (environment variables take precedence)
settings = load_config('/path/to/config.json')

Authentication Configuration

API Token Authentication (Recommended)

{
  "grafana": {
    "url": "http://localhost:3000",
    "token": "eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk"
  }
}

Basic Authentication (Required for User/Org Operations)

{
  "grafana": {
    "url": "http://localhost:3000",
    "token": "eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk",
    "admin_account": "admin",
    "admin_password": "admin123"
  }
}

Environment Variable Authentication

export GRAFANA_TOKEN="eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk"
export GRAFANA_BASIC_AUTH="YWRtaW46YWRtaW4xMjM="  # Base64 of "admin:admin123"

Configuration Validation

The configuration system includes automatic validation:

  • Required fields: URL and token validation
  • Type conversion: String environment variables to appropriate types
  • SSL certificate: Client certificate file existence check
  • Cloud credentials: Basic credential format validation
  • Directory creation: Backup directory creation if it doesn't exist

Security Considerations

  • Credentials: Never commit configuration files with real credentials
  • File permissions: Restrict access to configuration files (600 recommended)
  • Environment variables: Use environment variables in production
  • SSL verification: Always enable SSL verification in production
  • Token rotation: Regularly rotate API tokens and credentials

Install with Tessl CLI

npx tessl i tessl/pypi-grafana-backup

docs

admin-tools.md

api-health.md

archive-management.md

backup-operations.md

cloud-storage.md

configuration.md

delete-operations.md

index.md

monitoring.md

restore-operations.md

tile.json