Package: grafana-backup
Grafana Backup ToolA Python-based application to backup Grafana settings using theGrafana API.The aim of this tool is to:Easily backup and restore Grafana.Have versioned backups(date and time in file name)for restoring and saving to cloud storage providers. Currently supportAmazon S3Azure StorageGCP Cloud Storage(Use service-account's credential file,see here)Supported componentsFolderFolder PermissionsLibrary Elements (doesn't work with Grafana 8.0.0 but 8.4.3)Dashboard (contains Alert)DatasourceAlert ChannelAlert Rules (Supported in version 9.4.0 of grafana and up.)TeamsTeam Members (Needs Basic Authentication (username and password, seegrafana doc)You need to setAdmin's account and passwordingrafanaSettings.json, or set the base64 encodedadmin account and passwordin ENVGRAFANA_BASIC_AUTH. E.gexport GRAFANA_BASIC_AUTH=YWRtaW46YWRtaW4=Or Sets this ENV of the Grafana serverGF_USERS_ALLOW_ORG_CREATE=true. seegrafana docOrganization (Needs Basic Authentication (username and password, seegrafana doc)You need to setAdmin's account and passwordingrafanaSettings.json, or set the base64 encodedadmin account and passwordin ENVGRAFANA_BASIC_AUTH. E.gexport GRAFANA_BASIC_AUTH=YWRtaW46YWRtaW4=Or Sets this ENV of the Grafana serverGF_USERS_ALLOW_ORG_CREATE=true. seegrafana docUser (Needs Basic Authentication (username and password, seegrafana doc)You need to setAdmin's account and passwordingrafanaSettings.json, or set the base64 encodedadmin account and passwordin ENVGRAFANA_BASIC_AUTH. E.gexport GRAFANA_BASIC_AUTH=YWRtaW46YWRtaW4=Grafana's api doesn't provide user's password when backing up, so thedefault_user_passwordingrafanaSettings.json, or in ENVDEFAULT_USER_PASSWORD, E.gexport DEFAULT_USER_PASSWORD=supersecretwill be used when restoring.SnapshotsDashboard Versions (only backup, no restore)AnnotationsNOTEThe only supportedorgIdright now is1, the default organization will be backed up only!RequirementsBashPython 2.7 or Python 3.xAccess to a Grafana API server.ATokenof anAdminrole (seeConfigurationsection below for more info)ConfigurationThere are three ways to setup the configuration:Useenvironment variablesto define the variables for connecting to a Grafana server.Usehard-coded settingsinconf/grafanaSettings.json(this is the default settings file if not specified otherwise).Use~/.grafana-backup.jsonto define variables in json format.Example ConfigCopygrafanaSettings.example.jsonand modify it for you to use, removeazure,aws,gcp,influxdbblocks (but keep the ones you used).Check out theexamplesfolder for more configuration details.NOTEIf you useenvironment variables, you need to add the following to your.bashrcor execute once before using the tool (please change variables according to your setup):(GRAFANA_HEADERSis optional, use it if necessary. please see#45)### Do not use a trailing slash on GRAFANA_URLexportGRAFANA_URL=http://some.host.org:3000exportGRAFANA_TOKEN=eyJrIjoidUhaU2ZQQndrWFN3RRVkUnVfrT56a1JoaG9KWFFObEgiLCJuIjoiYWRtaW4iLCJpZCI6MX0=# GRAFANA_HEADERS is optionalexportGRAFANA_HEADERS=Host:some.host.orgTo create and obtain aTokenfor your Grafana server, please refer to theofficial documentation.NOTEthat you need to generate aTokenwith anAdminrole for the backup to succeed, otherwise you will have potential permission issues.InstallationVirtual environment (optional but recommended)Create a virtualenv, you could using something likepyenvif you'd prefervirtualenv -p $(which python3) venv
source venv/bin/activateInstallation using pypipip install grafana-backupInstallation using this repoFirst clone this repogit clone https://github.com/ysde/grafana-backup-tool.git
cd grafana-backup-toolInstallation works best usingpippip install .How to UseFirst perform theConfigurationandInstallationsections as described above.Use thegrafana-backup savecommand to backup all your folders, dashboards, datasources and alert channels to the_OUTPUT_subdirectory of the current directory.Example:$grafana-backupsave
$tree_OUTPUT_
_OUTPUT_/
└──202006272027.tar.gzUse thegrafana-backup restore <archive_file>command with a path to a previous backup to restore everything.NOTEthismayresult in data loss, by overwriting data on the server.Example:$grafana-backuprestore_OUTPUT_/202006272027.tar.gzDockerReplace variables below to use the Docker version of this tool{YOUR_GRAFANA_TOKEN}: Your Grafana siteToken.{YOUR_GRAFANA_URL}: Your Grafana siteURL.{YOUR_BACKUP_FOLDER_ON_THE_HOST}: Thebackup folderon the Grafana host machine.KubernetesCheck out the CronJob inexamplesfor a simple example of how grafana-backup-tool
can be ran within a Kubernetes environmentBackupIf you decide to use a volume (-v) then you'll need to create the volume first with 1337 uid/gid ownership first, example:mkdir /tmp/backup
sudo chown 1337:1337 /tmp/backupdocker run --user $(id -u):$(id -g) --rm --name grafana-backup-tool \
           -e GRAFANA_TOKEN={YOUR_GRAFANA_TOKEN} \
           -e GRAFANA_URL={YOUR_GRAFANA_URL} \
           -e GRAFANA_ADMIN_ACCOUNT={YOUR_GRAFANA_ADMIN_ACCOUNT} \
           -e GRAFANA_ADMIN_PASSWORD={YOUR_GRAFANA_ADMIN_PASSWORD} \
           -e VERIFY_SSL={True/False} \
           -v {YOUR_BACKUP_FOLDER_ON_THE_HOST}:/opt/grafana-backup-tool/_OUTPUT_  \
           ysde/docker-grafana-backup-toolExample:docker run --user $(id -u):$(id -g) --rm --name grafana-backup-tool \
           -e GRAFANA_TOKEN="eyJrIjoiNGZqTDEyeXNaY0RsMXNhbkNTSnlKN2M3bE1VeHdqVTEiLCJuIjoiZ3JhZmFuYS1iYWNrdXAiLCJpZCI6MX0=" \
           -e GRAFANA_URL=http://192.168.0.79:3000 \
           -e GRAFANA_ADMIN_ACCOUNT=admin \
           -e GRAFANA_ADMIN_PASSWORD=adminpassword \
           -e VERIFY_SSL=False \
           -v /tmp/backup/:/opt/grafana-backup-tool/_OUTPUT_ \
           ysde/docker-grafana-backup-toolS3 Example:Set S3 configurations in-eorgrafanaSettings.json(example)-e AWS_S3_BUCKET_NAME="my-backups-bucket" \
           -e AWS_S3_BUCKET_KEY="grafana-backup-folder" \
           -e AWS_DEFAULT_REGION="us-east-1" \
           -e AWS_ACCESS_KEY_ID="secret" \
           -e AWS_SECRET_ACCESS_KEY="secret" \Azure Example:Set Azure configurations in-eorgrafanaSettings.json(example)-e AZURE_STORAGE_CONTAINER_NAME="azure-storage-container-name" \
		   -e AZURE_STORAGE_CONNECTION_STRING="azure-storage-connection-string"GCS Example:Set GCS configurations in-eorgrafanaSettings.json(example)-e GCS_BUCKET_NAME="bucket-name" \
		   -e GCLOUD_PROJECT="gcp-project-name" \
		   -e GOOGLE_APPLICATION_CREDENTIALS="credential-file-path"Restoredocker run --user $(id -u):$(id -g) --rm --name grafana-backup-tool \
           -e GRAFANA_TOKEN={YOUR_GRAFANA_TOKEN} \
           -e GRAFANA_URL={YOUR_GRAFANA_URL} \
           -e GRAFANA_ADMIN_ACCOUNT={YOUR_GRAFANA_ADMIN_ACCOUNT} \
           -e GRAFANA_ADMIN_PASSWORD={YOUR_GRAFANA_ADMIN_PASSWORD} \
           -e VERIFY_SSL={True/False} \
           -e RESTORE="true" \
           -e ARCHIVE_FILE={THE_ARCHIVED_FILE_NAME} \
           -v {YOUR_BACKUP_FOLDER_ON_THE_HOST}:/opt/grafana-backup-tool/_OUTPUT_  \
           ysde/docker-grafana-backup-toolExample:docker run --user $(id -u):$(id -g) --rm --name grafana-backup-tool \
           -e GRAFANA_TOKEN="eyJrIjoiNGZqTDEyeXNaY0RsMXNhbkNTSnlKN2M3bE1VeHdqVTEiLCJuIjoiZ3JhZmFuYS1iYWNrdXAiLCJpZCI6MX0=" \
           -e GRAFANA_URL=http://192.168.0.79:3000 \
           -e GRAFANA_ADMIN_ACCOUNT=admin \
           -e GRAFANA_ADMIN_PASSWORD=adminpassword \
           -e VERIFY_SSL=False \
           -e RESTORE="true" \
           -e ARCHIVE_FILE="202006280247.tar.gz" \
           -v /tmp/backup/:/opt/grafana-backup-tool/_OUTPUT_ \
           ysde/docker-grafana-backup-toolBuildingYou can build the docker image simply by executingmakein the root of this repo. The image will get tagged asysde:grafana-backupMonitoringInfluxDB support has been added and Prometheus push gateway support will be added in the future.In order to monitor successful backups with InfluxDB simply configuregrafana-backupInfluxDB settings using thisexampleconfiguration.
Or if you prefer to use environment variables you can instead setINFLUXDB_HOST,INFLUXDB_PORT,INFLUXDB_MEASUREMENT,INFLUXDB_USERNAMEandINFLUXDB_PASSWORD.Once configuredgrafana-backupwill automatically enter a1in your defined timeseries measurement upon each successful backup.