A Python-based application to backup Grafana settings using the Grafana API
npx @tessl/cli install tessl/pypi-grafana-backup@1.4.0A Python-based application to backup and restore Grafana settings using the Grafana API. It provides comprehensive command-line tool and Python library for backing up and restoring Grafana configurations, supporting multiple cloud storage providers, and offering extensive monitoring integration capabilities.
pip install grafana-backupgrafana-backup# For programmatic usage
import grafana_backup.cli as cli
import grafana_backup.save as save_module
import grafana_backup.restore as restore_module
import grafana_backup.delete as delete_module
import grafana_backup.tools as tools_module
import grafana_backup.grafanaSettings as settings_moduleCommon configuration loading:
from grafana_backup.grafanaSettings import main as load_config
settings = load_config('/path/to/config.json')# Backup all Grafana components
grafana-backup save
# Backup specific components
grafana-backup save --components=dashboards,datasources,folders
# Restore from archive
grafana-backup restore backup_20250101.tar.gz
# Delete components
grafana-backup delete --components=dashboards
# Tools and utilities
grafana-backup tools pause-alerts
grafana-backup tools make-users-viewersfrom grafana_backup.grafanaSettings import main as load_config
from grafana_backup.save import main as save_backup
from grafana_backup.restore import main as restore_backup
# Load configuration
settings = load_config('/path/to/config.json')
# Mock command line arguments
save_args = {
'save': True,
'--components': 'dashboards,datasources',
'--no-archive': False,
'--config': None
}
# Perform backup
save_backup(save_args, settings)The grafana-backup tool is built around a modular architecture with clear separation of concerns:
This design enables both command-line usage and programmatic integration while supporting extensible component types and storage backends.
Complete backup functionality for all supported Grafana components including dashboards, datasources, folders, users, teams, and more. Supports selective component backup and automatic archiving with timestamp versioning.
def main(args, settings):
"""
Main backup function handling complete backup workflow
Args:
args (dict): Command line arguments with --components and --no-archive
settings (dict): Configuration settings from grafanaSettings
Returns:
None: Performs backup operations and exits
"""Complete restoration of Grafana configurations from backup archives with support for selective component restoration, dependency management, and cloud storage integration.
def main(args, settings):
"""
Main restore function handling archive extraction and component restoration
Args:
args (dict): Command line arguments including archive_file and --components
settings (dict): Configuration settings from grafanaSettings
Returns:
None: Performs restore operations and exits
"""
def restore_components(args, settings, restore_functions, tmpdir):
"""
Restore individual components from extracted archive
Args:
args (dict): Command line arguments
settings (dict): Configuration settings
restore_functions (OrderedDict): Component restoration functions
tmpdir (str): Temporary directory with extracted files
Returns:
None: Processes component files and restores them
"""Selective deletion of Grafana components with support for bulk operations and safety checks to prevent accidental data loss.
def main(args, settings):
"""
Main delete function for selective component deletion
Args:
args (dict): Command line arguments with --components filter
settings (dict): Configuration settings from grafanaSettings
Returns:
None: Performs deletion operations and exits
Note:
Available components: dashboards, datasources, folders, alert-channels,
snapshots, annotations, library-elements, team-members
Teams are intentionally excluded to preserve references
"""Comprehensive configuration loading and processing with support for JSON files, environment variables, and default settings. Handles authentication, SSL verification, and API endpoint configuration.
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
"""Integration with major cloud storage providers including Amazon S3, Azure Storage, and Google Cloud Storage for automated backup archiving and retrieval.
# S3 Integration (grafana_backup.s3_upload)
def main(args, settings):
"""Upload backup archives to AWS S3"""
# S3 Download (grafana_backup.s3_download)
def main(args, settings):
"""Download backup archives from AWS S3"""
# Azure Storage Integration (grafana_backup.azure_storage_upload)
def main(args, settings):
"""Upload backup archives to Azure Storage"""
# Azure Storage Download (grafana_backup.azure_storage_download)
def main(args, settings):
"""Download backup archives from Azure Storage"""
# Google Cloud Storage Integration (grafana_backup.gcs_upload)
def main(args, settings):
"""Upload backup archives to Google Cloud Storage"""
# GCS Download (grafana_backup.gcs_download)
def main(args, settings):
"""Download backup archives from Google Cloud Storage"""Administrative utility functions for Grafana management including alert management, user role management, and permission restoration.
def main(precommand_args, settings):
"""
Administrative tools entry point with command routing
Args:
precommand_args (dict): Pre-parsed command line arguments
settings (dict): Configuration settings from grafanaSettings
Returns:
None: Routes to specific tool functions and exits
Available tools:
- pause-alerts: Pause all alert rules and save state
- unpause-alerts: Restore alert rules from saved state
- make-users-viewers: Convert all users to viewer role
- restore-users: Restore user permissions from backup
"""Comprehensive API validation and feature detection to ensure compatibility with different Grafana versions and configurations.
def main(settings):
"""
Perform comprehensive API health checks and feature detection
Args:
settings (dict): Configuration settings from grafanaSettings
Returns:
tuple: (status_code, response_data, dashboard_uid_support,
datasource_uid_support, paging_support, contact_point_support)
"""Archive creation and extraction functionality with support for compressed tar.gz format and automatic timestamping.
def main(args, settings):
"""
Create compressed tar.gz archive from backup directory
Args:
args (dict): Command line arguments
settings (dict): Configuration settings including BACKUP_DIR and TIMESTAMP
Returns:
None: Creates timestamped archive file
"""InfluxDB integration for backup operation monitoring and metrics collection, enabling operational visibility into backup processes.
def main(args, settings):
"""
Send backup operation metrics to InfluxDB
Args:
args (dict): Command line arguments
settings (dict): Configuration including InfluxDB connection details
Returns:
None: Writes metrics to InfluxDB database
"""