ysde / grafana-backup-tool

A Python-based application to backup Grafana settings by using the Grafana API
MIT License
877 stars 277 forks source link

restore script can't recreate membership of deleted team #237

Open valafon opened 1 year ago

valafon commented 1 year ago

If we delete the team and recreate it by script, team members don't restore. As I see, membership is restored through the ID of a team. But when the team is recreated by script, it has a new ID. So the script has 500 errors in the logs

[DEBUG] resp body: {'id': 1, 'email': 'admin@localhost', 'name': '', 'login': 'admin', 'theme': '', 'orgId': 1, 'isGrafanaAdmin': True, 'isDisabled': False, 'isExternal': False, 'authLabels': None, 'updatedAt': '2023-08-03T04:44:51Z', 'createdAt': '2023-07-28T09:49:47Z', 'avatarUrl': ''}
[DEBUG] resp status: 500
[DEBUG] resp body: {'message': 'Failed to add Member to Team', 'traceID': ''}
create team member: , status: 500, msg: {'message': 'Failed to add Member to Team', 'traceID': ''}
restoring team_member: /tmp/tmpey6gbt25/_OUTPUT_/team_members/202308030620/7_9.team_member

I think the main root cause is that Grafana doesn't recreate a team with an ID specified -

curl -X POST -H "Content-Type: application/json" -H "Authorization: Basic *****" -d '{"id": "9","name": "test-issue"}' http://grafana_url.com/api/teams
{"message":"Team created","teamId":13}

So when the script tried to recreate a team with the same ID, actually Grafana creates it with another, so restoring membership also doesn't work.

Steps to reproduce: create a team, and add some members to it. Back up Grafana with grafana backup tool, then delete the team and try to restore Grafana. You will see the team is recreated, but there are no members and 500 errors in logs related to adding members to the team. Permissions to folders for teams also don't restore when team is recreated