A Python-based application to backup Grafana settings using the Grafana API
—
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.
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
"""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 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 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 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 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 cloud storage settings
AZURE_STORAGE_CONTAINER_NAME: str # Azure container name
AZURE_STORAGE_CONNECTION_STRING: str # Azure storage connection string# Google Cloud Storage settings
GCS_BUCKET_NAME: str # GCS bucket name
GOOGLE_APPLICATION_CREDENTIALS: str # Path to GCS service account key file# 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 generation
TIMESTAMP: str # Current timestamp using BACKUP_FILE_FORMAT{
"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"
}
}{
"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": {
"measurement": "grafana_backup",
"host": "localhost",
"port": 8086,
"username": "monitoring",
"password": "monitoring_password",
"database": "grafana_metrics"
}
}All configuration values can be overridden using environment variables:
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="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=trueexport 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"export GRAFANA_HEADERS="X-Custom-Header:value,X-Another-Header:value2"The configuration system searches for configuration files in this order:
--config=/path/to/config.json~/.grafana-backup.jsongrafana_backup/conf/grafanaSettings.jsonfrom grafana_backup.grafanaSettings import main as load_config
# Load from default locations (searches in order)
settings = load_config(None)# Load from specific file path
settings = load_config('/path/to/custom-config.json')# 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']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'){
"grafana": {
"url": "http://localhost:3000",
"token": "eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk"
}
}{
"grafana": {
"url": "http://localhost:3000",
"token": "eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk",
"admin_account": "admin",
"admin_password": "admin123"
}
}export GRAFANA_TOKEN="eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk"
export GRAFANA_BASIC_AUTH="YWRtaW46YWRtaW4xMjM=" # Base64 of "admin:admin123"The configuration system includes automatic validation:
Install with Tessl CLI
npx tessl i tessl/pypi-grafana-backup