A Python-based application to backup Grafana settings using the Grafana API
—
Complete restoration functionality for Grafana configurations from backup archives. The restore system recreates Grafana objects from JSON backup files with proper dependency ordering, conflict resolution, and support for multiple storage sources.
The primary restore entry point that handles archive extraction, component restoration, and dependency management.
def main(args, settings):
"""
Main restore function that orchestrates the complete restore workflow
Args:
args (dict): Command line arguments including:
- '<archive_file>': Path to backup archive or cloud storage identifier
- '--components': Comma-separated list of components to restore
settings (dict): Configuration settings with cloud storage credentials
"""Internal function that handles the actual restoration of components from extracted archive files.
def restore_components(args, settings, restore_functions, tmpdir):
"""
Restore components from extracted archive directory
Args:
args (dict): Command line arguments
settings (dict): Configuration settings
restore_functions (OrderedDict): Mapping of file extensions to restore functions
tmpdir (str): Temporary directory containing extracted backup files
"""Components are restored in dependency order to ensure proper object relationships:
def main(args, settings, file_path):
"""
Restore folder from JSON backup file
Module: grafana_backup.create_folder
Input: *.folder files from backup
Features: Hierarchy preservation, permission handling
"""def main(args, settings, file_path):
"""
Restore datasource configuration from JSON backup file
Module: grafana_backup.create_datasource
Input: *.datasource files from backup
Features: Credential restoration, connection validation
"""def main(args, settings, file_path):
"""
Restore library element from JSON backup file
Module: grafana_backup.create_library_element
Input: *.library_element files from backup
Features: Element type preservation, model validation
"""def main(args, settings, file_path):
"""
Restore dashboard from JSON backup file
Module: grafana_backup.create_dashboard
Input: *.dashboard files from backup
Features: Folder assignment, UID handling, overwrite protection
"""def main(args, settings, file_path):
"""
Restore alert notification channel from JSON backup file
Module: grafana_backup.create_alert_channel
Input: *.alert_channel files from backup
Features: Channel type validation, credential handling
"""def main(args, settings, file_path):
"""
Restore organization from JSON backup file
Module: grafana_backup.create_org
Input: *.organization files from backup
Requires: Basic authentication (admin credentials)
"""def main(args, settings, file_path):
"""
Restore user account from JSON backup file
Module: grafana_backup.create_user
Input: *.user files from backup
Requires: Basic authentication (admin credentials)
Note: Uses default password from configuration
"""def main(args, settings, file_path):
"""
Restore dashboard snapshot from JSON backup file
Module: grafana_backup.create_snapshot
Input: *.snapshot files from backup
Features: Complete snapshot data restoration
"""def main(args, settings, file_path):
"""
Restore annotation from JSON backup file
Module: grafana_backup.create_annotation
Input: *.annotation files from backup
Features: Time range and tag preservation
"""def main(args, settings, file_path):
"""
Restore team from JSON backup file
Module: grafana_backup.create_team
Input: *.team files from backup
Features: Team metadata and settings restoration
"""def main(args, settings, file_path):
"""
Restore team membership from JSON backup file
Module: grafana_backup.create_team_member
Input: *.team_member files from backup
Features: Role assignment preservation
"""def main(args, settings, file_path):
"""
Update folder permissions from JSON backup file
Module: grafana_backup.update_folder_permissions
Input: *.folder_permission files from backup
Features: Access control restoration
"""def main(args, settings, file_path):
"""
Restore unified alerting rule from JSON backup file
Module: grafana_backup.create_alert_rule
Input: *.alert_rule files from backup
Features: Rule group preservation, expression handling
"""def main(args, settings, file_path):
"""
Restore contact point from JSON backup file
Module: grafana_backup.create_contact_point
Input: *.contact_point files from backup
Requires: Unified alerting support
"""# Restore from local tar.gz file
args = {
'restore': True,
'<archive_file>': '/path/to/backup_20250101.tar.gz',
'--components': None,
'--config': None
}# Restore from S3 (filename specified in S3 settings)
settings['AWS_S3_BUCKET_NAME'] = 'my-backup-bucket'
args = {
'restore': True,
'<archive_file>': 'backup_20250101.tar.gz', # S3 object key
'--components': None,
'--config': None
}# Restore from Azure Storage
settings['AZURE_STORAGE_CONTAINER_NAME'] = 'grafana-backups'
args = {
'restore': True,
'<archive_file>': 'backup_20250101.tar.gz', # Blob name
'--components': None,
'--config': None
}# Restore from GCS
settings['GCS_BUCKET_NAME'] = 'grafana-backup-bucket'
args = {
'restore': True,
'<archive_file>': 'backup_20250101.tar.gz', # GCS object name
'--components': None,
'--config': None
}from grafana_backup.restore import main as restore_backup
from grafana_backup.grafanaSettings import main as load_config
# Load configuration
settings = load_config('/path/to/grafanaSettings.json')
# Restore all components from archive
args = {
'restore': True,
'<archive_file>': 'backup_20250101.tar.gz',
'--components': None, # None means all components
'--config': None
}
restore_backup(args, settings)# Restore only dashboards and datasources
args = {
'restore': True,
'<archive_file>': 'backup_20250101.tar.gz',
'--components': 'dashboards,datasources',
'--config': None
}
restore_backup(args, settings)# Configure cloud storage
settings['AWS_S3_BUCKET_NAME'] = 'my-grafana-backups'
settings['AWS_DEFAULT_REGION'] = 'us-east-1'
# Restore from S3
args = {
'restore': True,
'<archive_file>': 'backup_20250101.tar.gz',
'--components': None,
'--config': None
}
restore_backup(args, settings)Notification policy restoration is disabled by default due to API issues that can lock the notification policy interface. The restore function exists but is commented out in the restore mapping.
Dashboard version history cannot be restored - it's backup-only data that provides historical reference but cannot be imported back into Grafana.
User passwords are not stored in backups for security reasons. The default_user_password configuration setting is used for all restored users.
Teams don't have consistent unique identifiers across Grafana instances, so team deletion/recreation can break folder permissions and team member references.
The restore system automatically handles dependencies:
Comprehensive error handling throughout the restore process:
The restore system expects tar.gz archives with the following structure:
backup_archive.tar.gz
├── folders/
│ └── {timestamp}/
│ └── *.folder
├── datasources/
│ └── {timestamp}/
│ └── *.datasource
├── dashboards/
│ └── {timestamp}/
│ └── *.dashboard
└── ...File extensions must match the component type for proper routing to restore functions.
Install with Tessl CLI
npx tessl i tessl/pypi-grafana-backup