Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
Grafana Backup Tool
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
External wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Coopdevs
Migrated Projects
Tooling
Grafana Backup Tool
Commits
b03495e0
Unverified
Commit
b03495e0
authored
4 years ago
by
ysde
Committed by
GitHub
4 years ago
Browse files
Options
Downloads
Plain Diff
Merge pull request #104 from acjohnson/tempfile
added python2 support to restore functions
parents
b58f6f3d
cf1a5a46
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
grafana_backup/restore.py
+46
-17
46 additions, 17 deletions
grafana_backup/restore.py
with
46 additions
and
17 deletions
grafana_backup/restore.py
+
46
−
17
View file @
b03495e0
...
...
@@ -7,12 +7,11 @@ from grafana_backup.create_alert_channel import main as create_alert_channel
from
grafana_backup.create_user
import
main
as
create_user
from
grafana_backup.s3_download
import
main
as
s3_download
from
glob
import
glob
import
sys
,
tarfile
,
tempfile
import
sys
,
tarfile
,
tempfile
,
os
,
shutil
,
fnmatch
def
main
(
args
,
settings
):
arg_archive_file
=
args
.
get
(
'
<archive_file>
'
,
None
)
arg_components
=
args
.
get
(
'
--components
'
,
False
)
aws_s3_bucket_name
=
settings
.
get
(
'
AWS_S3_BUCKET_NAME
'
)
(
status
,
json_resp
,
api_version
)
=
api_checks
(
settings
)
...
...
@@ -42,29 +41,59 @@ def main(args, settings):
print
(
str
(
e
))
sys
.
exit
(
1
)
with
tempfile
.
TemporaryDirectory
()
as
tmpdir
:
restore_functions
=
{
'
folder
'
:
create_folder
,
'
datasource
'
:
create_datasource
,
'
dashboard
'
:
create_dashboard
,
'
alert_channel
'
:
create_alert_channel
,
'
organization
'
:
create_org
,
'
user
'
:
create_user
}
if
sys
.
version_info
>=
(
3
,):
with
tempfile
.
TemporaryDirectory
()
as
tmpdir
:
tar
.
extractall
(
tmpdir
)
tar
.
close
()
restore_components
(
args
,
settings
,
restore_functions
,
tmpdir
)
tmpdir
.
cleanup
()
else
:
tmpdir
=
tempfile
.
mkdtemp
()
tar
.
extractall
(
tmpdir
)
tar
.
close
()
restore_components
(
args
,
settings
,
restore_functions
,
tmpdir
)
try
:
shutil
.
rmtree
(
tmpdir
)
except
OSError
as
e
:
print
(
"
Error: %s : %s
"
%
(
tmpdir
,
e
.
strerror
))
restore_functions
=
{
'
folder
'
:
create_folder
,
'
datasource
'
:
create_datasource
,
'
dashboard
'
:
create_dashboard
,
'
alert_channel
'
:
create_alert_channel
,
'
organization
'
:
create_org
,
'
user
'
:
create_user
}
if
arg_components
:
arg_components_list
=
arg_components
.
split
(
'
,
'
)
def
restore_components
(
args
,
settings
,
restore_functions
,
tmpdir
):
arg_components
=
args
.
get
(
'
--components
'
,
False
)
if
arg_components
:
arg_components_list
=
arg_components
.
split
(
'
,
'
)
# Restore only the components that provided via an argument
# but must also exist in extracted archive
for
ext
in
arg_components_list
:
# Restore only the components that provided via an argument
# but must also exist in extracted archive
for
ext
in
arg_components_list
:
if
sys
.
version_info
>=
(
3
,):
for
file_path
in
glob
(
'
{0}/**/*.{1}
'
.
format
(
tmpdir
,
ext
[:
-
1
]),
recursive
=
True
):
print
(
'
restoring {0}: {1}
'
.
format
(
ext
,
file_path
))
restore_functions
[
ext
[:
-
1
]](
args
,
settings
,
file_path
)
else
:
# Restore every component included in extracted archive
for
ext
in
restore_functions
.
keys
():
else
:
for
root
,
dirnames
,
filenames
in
os
.
walk
(
'
{0}
'
.
format
(
tmpdir
)):
for
filename
in
fnmatch
.
filter
(
filenames
,
'
*.{0}
'
.
format
(
ext
[:
-
1
])):
file_path
=
os
.
path
.
join
(
root
,
filename
)
print
(
'
restoring {0}: {1}
'
.
format
(
ext
,
file_path
))
restore_functions
[
ext
[:
-
1
]](
args
,
settings
,
file_path
)
else
:
# Restore every component included in extracted archive
for
ext
in
restore_functions
.
keys
():
if
sys
.
version_info
>=
(
3
,):
for
file_path
in
glob
(
'
{0}/**/*.{1}
'
.
format
(
tmpdir
,
ext
),
recursive
=
True
):
print
(
'
restoring {0}: {1}
'
.
format
(
ext
,
file_path
))
restore_functions
[
ext
](
args
,
settings
,
file_path
)
else
:
for
root
,
dirnames
,
filenames
in
os
.
walk
(
'
{0}
'
.
format
(
tmpdir
)):
for
filename
in
fnmatch
.
filter
(
filenames
,
'
*.{0}
'
.
format
(
ext
)):
file_path
=
os
.
path
.
join
(
root
,
filename
)
print
(
'
restoring {0}: {1}
'
.
format
(
ext
,
file_path
))
restore_functions
[
ext
](
args
,
settings
,
file_path
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment