This will be a document about how to back up Grafana dashboards and the data sources within them.
Environments within the project where backups will be taken:
Docker
docker run --user $(id -u):$(id -g) --rm --name grafana-backup-tool \
-e GRAFANA_URL=https://dashboards.serdarcanb.com \
-e GRAFANA_TOKEN='glsa_rZdAlQCjGas83MzFBABPTw85Ca34bYt9_19cb61c2' \
-e GRAFANA_ADMIN_ACCOUNT=admin \
-e GRAFANA_ADMIN_PASSWORD='example123!' \
-e VERIFY_SSL=False \
-v /tmp/backup/:/opt/grafana-backup-tool/_OUTPUT_ \
ysde/docker-grafana-backup-tool
GRAFANA_URL: grafana url (dashboards.serdarcanb.com)GRAFANA_TOKEN: In the Grafana User tab, create a service account and set it as an admin.GRAFANA_ADMIN_ACCOUNT: admin userGRAFANA_ADMIN_PASSWORD: admin passwordVERIFY_SSL: We select False because we are not using SSL."
It also saves the backup as a tar.gz file in the /tmp/backup directory.
Restore
Before testing the restore, I create a Grafana container using Docker.
The Grafana version you backed up must be the same as the version you are restoring to, or you may encounter unexpected issues (the restore might not complete successfully).
Copy
Copy
docker run -d --name=grafana -p 3000:3000 grafana/grafana
After creating the container, you need to log in as an admin and create a service account on the User page.
Copy
Copy
docker run --user $(id -u):$(id -g) --rm --name grafana-backup-tool \
-e GRAFANA_TOKEN=glsa_oGkkyqDSTYY2gaAYPEsABlIFqgI5mo0C_2d7b72c6 \
-e GRAFANA_URL=http://192.168.60.45:3000 \
-e GRAFANA_ADMIN_ACCOUNT=admin \
-e GRAFANA_ADMIN_PASSWORD=admin \
-e VERIFY_SSL=False \
-e RESTORE="true" \
-e ARCHIVE_FILE=dashboard.tar.gz \
-v /root:/opt/grafana-backup-tool/_OUTPUT_ \
ysde/docker-grafana-backup-tool
After creating the service account, add the RESTORE
and ARCHIVE_FILE
environment variables.
After the restore, you can easily check the Dashboard, Datasource, User, and Alerts.
Kubernetes
Backup
In a Kubernetes environment, we use a Cronjob to manage this setup. You can also send backups to AWS and GCLOUD buckets in the same way. You can do this by defining it as an ENV variable.
https://github.com/ysde/grafana-backup-tool/blob/master/examples/grafana-backup-k8s-cronjob.yaml
I created a special Dockerfile to send notifications and backups to GitLab and Nexus.
Copy
Copy
FROM ysde/docker-grafana-backup-tool:1.4.2
USER root
RUN apk --no-cache add curl gitlab-release-cli git
WORKDIR /opt/grafana-backup-tool
COPY script.sh .
RUN chmod +x /opt/grafana-backup-tool/script.sh
RUN chown -R 1337:1337 /opt/grafana-backup-tool
CMD sh -c 'if [ "$RESTORE" = true ]; then if [ ! -z "$AWS_S3_BUCKET_NAME" ] || [ ! -z "$AZURE_STORAGE_CONTAINER_NAME" ] || [ ! -z "$GCS_BUCKET_NAME" ]; then grafana-backup restore $ARCHIVE_FILE; else grafana-backup restore _OUTPUT_/$ARCHIVE_FILE; fi else grafana-backup save && /opt/grafana-backup-tool/script.sh ; fi'
Copy
Copy
#!/bin/bash
current_date=$(date +"%d.%m.%Y")
mv /opt/grafana-backup-tool/_OUTPUT_/*.tar.gz /opt/grafana-backup-tool/_OUTPUT_/"$PROJECT-$current_date.tar.gz"
curl --fail -u test:test --upload-file /opt/grafana-backup-tool/_OUTPUT_/"$PROJECT-$current_date.tar.gz" "https://nexus.serdarcanb.com/repository/restapi/grafana-backup/${PROJECT}-${current_date}.tar.gz"
MESSAGE='{"text":"✅ *'$PROJECT' Grafana backup completed successfully!* \n\n📂 Nexus link: '$GRAFANA_BACKUP_LINK'\n\n🔗 Commit link: '$COMMIT_LINK'"}'
curl -X POST -H 'Content-type: application/json' --data "$MESSAGE" $SLACK_WEBHOOK_URL
It sends a notification to my Slack channel. You can schedule backups daily, weekly, or monthly using a cron job.
Restore
You can perform the restore process entirely using Docker, just like the backup.
Thank you for reading.