Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backup, restore, and delete teams, team-members, and library-elements #181

Merged
merged 10 commits into from
Feb 21, 2023

Conversation

nileger
Copy link
Contributor

@nileger nileger commented Mar 8, 2022

Problem

It was not possible to backup and restore any library-elements (library-panels) and teams. However, we heavily rely on library-panels because they are referred on multiple dashboards. Without library-elements being backed up, all our dashboard are broken after a restore.

Solution

This PR solves the above described problem by extending the grafana-backup-tool to also back up library-elements and teams.

Testing

Basic tests were already successful. I'm currently doing more advanced tests.
Any feedback and and testing is highly appreciated.

Related Issues

#180
#164

@acjohnson
Copy link
Collaborator

+1 this looks good to me, any feedback @ysde or others?

@nileger
Copy link
Contributor Author

nileger commented Mar 8, 2022

@acjohnson I just found out that when restoring, references between teams and users as well as dashboards and library panels are lost. Thus, I have to have to take a look at it tomorrow.

@nileger
Copy link
Contributor Author

nileger commented Mar 9, 2022

@acjohnson
Backing up library-elements, teams, and team members works now.
Unfortunately, restoring library elements didn't work in v8.0.0 for me. This is because Grafana creates the library-elements with the wrong uid. However, this is fixed in v8.4.3.

@nileger nileger changed the title Backup, restore, and delete teams and library-elements Backup, restore, and delete teams, team-members, and library-elements Mar 10, 2022
@ysde
Copy link
Owner

ysde commented Mar 14, 2022

Hi @acjohnson
Sorry for the late response

Hi @NilsLeger Thanks for the PR.

Is it compatible for older version grafana ?
I mean if older version of grafana doesn't support library-element api, would this PR break the backup or save process ?

Thank you

@nileger
Copy link
Contributor Author

nileger commented Mar 14, 2022

Hi @ysde,
thank you for your input.

I'm not sure whether backups or restores for older Grafana versions would stop working with this update.
Irrespective of that, we should implement functionality to skip specific types of resources for specific version. Then, we are 100% sure that this PR is no breaking change.

I thought of something like the following procedure.

  1. The health endpoint returns the Grafana version.
  2. We have a map that maps resource types to Grafana versions (e.g., library-elements: 8.4.0)
  3. Based on 1. and 2., we decide whether to backup and restore a certain resource type.

What do you think?

@ysde
Copy link
Owner

ysde commented Mar 15, 2022

Hi @ysde, thank you for your input.

I'm not sure whether backups or restores for older Grafana versions would stop working with this update. Irrespective of that, we should implement functionality to skip specific types of resources for specific version. Then, we are 100% sure that this PR is no breaking change.

I thought of something like the following procedure.

  1. The health endpoint returns the Grafana version.
  2. We have a map that maps resource types to Grafana versions (e.g., library-elements: 8.4.0)
  3. Based on 1. and 2., we decide whether to backup and restore a certain resource type.

What do you think?

Yeah sure, good idea.
Could you help implement this ? Many thanks

@rubencosta
Copy link

@NilsLeger @ysde This approach would fail if there's no health endpoint which is the case for AWS managed grafana. See #184.
If I might suggest, could the version be set as an env var possibly making things simpler on the implementation side and not relying on a health endpoint for getting the version?
If we really want to get the version from the health endpoint then we would need a fallback if the health endpoint is not available as is the case with AWS managed grafana.

@nileger
Copy link
Contributor Author

nileger commented Jun 1, 2022

@rubencosta Great idea! Thanks for the input!

@azalenski-branch
Copy link

Is there something else that needs to be done in this PR? @ysde @rubencosta

If not is it possible to merge this PR soon?

@tedpearson
Copy link

tedpearson commented Dec 2, 2022

Tried this out (merged into master) on grafana v9.3.1 for some library elements. Backing up seems to succeed, but restoring is failing with this error:

restoring library_element: /tmp/tmpvn9o2lwu/tmp/grafana-backup/library-elements/202212021805/2ShwS6zgz.library_element
[DEBUG] resp status: 200
[DEBUG] resp body: [{'id': 1, 'uid': 'MqUvRWWgz', 'title': 'Unifi'}]
query folder:, status:200
Traceback (most recent call last):
  File "/home/ted/.local/bin/grafana-backup", line 8, in <module>
    sys.exit(main())
  File "/home/ted/.local/lib/python3.9/site-packages/grafana_backup/cli.py", line 55, in main
    restore(args, settings)
  File "/home/ted/.local/lib/python3.9/site-packages/grafana_backup/restore.py", line 96, in main
    restore_components(args, settings, restore_functions, tmpdir)
  File "/home/ted/.local/lib/python3.9/site-packages/grafana_backup/restore.py", line 134, in restore_components
    restore_functions[ext](args, settings, file_path)
  File "/home/ted/.local/lib/python3.9/site-packages/grafana_backup/create_library_element.py", line 19, in main
    folder_id = get_folder(folder_uid, grafana_url, http_post_headers, verify_ssl, client_cert, debug)[1]['id']
TypeError: list indices must be integers or slices, not str

Here's the element it was trying to upload:
element.txt

@tedpearson
Copy link

tedpearson commented Dec 3, 2022

Looks like File "create_library_element.py", line 19 needs to read

folder_id = get_folder(folder_uid, grafana_url, http_post_headers, verify_ssl, client_cert, debug)[1][0]['id']

at least on my system.

Making just this change made it work 100% for me.

@ysde ysde merged commit b3a58b3 into ysde:master Feb 21, 2023
@maidanskyi
Copy link

maidanskyi commented Mar 22, 2023

This issue is still not fixed.

I have two AWS Managed Grafana: A and B.
I did a backup for both of them and I'm trying to restore a data from A to Grafana B
and I watch this error:

Traceback (most recent call last):
  File "/usr/bin/grafana-backup", line 33, in <module>
    sys.exit(load_entry_point('grafana-backup==1.2.5', 'console_scripts', 'grafana-backup')())
  File "/usr/lib/python3.10/site-packages/grafana_backup/cli.py", line 55, in main
    restore(args, settings)
  File "/usr/lib/python3.10/site-packages/grafana_backup/restore.py", line 96, in main
    restore_components(args, settings, restore_functions, tmpdir)
  File "/usr/lib/python3.10/site-packages/grafana_backup/restore.py", line 134, in restore_components
    restore_functions[ext](args, settings, file_path)
  File "/usr/lib/python3.10/site-packages/grafana_backup/create_library_element.py", line 19, in main
    folder_id = get_folder(folder_uid, grafana_url, http_post_headers, verify_ssl, client_cert, debug)[1]['id']

when I restore the original backup (B to B ) everything is ok

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants