Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
eadde611
Commit
eadde611
authored
Sep 09, 2020
by
James Fargher
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Extract repository logic from repository backups
Renamed Backup::Repository to Backup::Repositories
parent
0dae421b
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
94 additions
and
111 deletions
+94
-111
lib/backup/repositories.rb
lib/backup/repositories.rb
+85
-71
lib/tasks/gitlab/backup.rake
lib/tasks/gitlab/backup.rake
+2
-2
spec/lib/backup/repositories_spec.rb
spec/lib/backup/repositories_spec.rb
+5
-36
spec/tasks/gitlab/backup_rake_spec.rb
spec/tasks/gitlab/backup_rake_spec.rb
+2
-2
No files found.
lib/backup/repositor
y
.rb
→
lib/backup/repositor
ies
.rb
View file @
eadde611
...
...
@@ -3,7 +3,7 @@
require
'yaml'
module
Backup
class
Repositor
y
class
Repositor
ies
attr_reader
:progress
def
initialize
(
progress
)
...
...
@@ -53,44 +53,11 @@ module Backup
private
def
restore_repository
(
container
,
type
)
repository
=
type
.
repository_for
(
container
)
repository_bundle_path
=
path_to_bundle
(
repository
)
progress
.
puts
" *
#{
display_repo_path
(
repository
)
}
... "
repository
.
remove
rescue
nil
if
File
.
exist?
(
repository_bundle_path
)
repository
.
create_from_bundle
(
repository_bundle_path
)
restore_custom_hooks
(
repository
)
elsif
type
.
project?
repository
.
create_repository
end
progress
.
puts
" *
#{
display_repo_path
(
repository
)
}
... "
+
"[DONE]"
.
color
(
:green
)
rescue
=>
e
progress_warn
(
container
,
e
,
'Failed to restore repo'
)
end
def
restore_custom_hooks
(
repository
)
return
unless
Dir
.
exist?
(
repository_backup_path
(
repository
))
return
if
Dir
.
glob
(
"
#{
repository_backup_path
(
repository
)
}
/custom_hooks*"
).
none?
custom_hooks_path
=
custom_hooks_tar
(
repository
)
repository
.
gitaly_repository_client
.
restore_custom_hooks
(
custom_hooks_path
)
end
def
path_to_bundle
(
repository
)
File
.
join
(
backup_repos_path
,
repository
.
disk_path
+
'.bundle'
)
end
def
repository_backup_path
(
repository
)
File
.
join
(
backup_repos_path
,
repository
.
disk_path
)
end
def
custom_hooks_tar
(
repository
)
File
.
join
(
repository_backup_path
(
repository
),
"custom_hooks.tar"
)
BackupRestorer
.
new
(
progress
,
type
.
repository_for
(
container
),
backup_repos_path
).
restore
(
always_create:
type
.
project?
)
end
def
backup_repos_path
...
...
@@ -154,52 +121,99 @@ module Backup
end
def
backup_repository
(
container
,
type
)
repository
=
type
.
repository_for
(
container
)
BackupRestorer
.
new
(
progress
,
type
.
repository_for
(
container
),
backup_repos_path
).
backup
end
progress
.
puts
" *
#{
display_repo_path
(
repository
)
}
... "
def
restore_object_pools
PoolRepository
.
includes
(
:source_project
).
find_each
do
|
pool
|
progress
.
puts
" - Object pool
#{
pool
.
disk_path
}
..."
if
empty_repository?
(
repository
)
progress
.
puts
" *
#{
display_repo_path
(
repository
)
}
... "
+
"[SKIPPED]"
.
color
(
:cyan
)
return
pool
.
source_project
||=
pool
.
member_projects
.
first
.
root_of_fork_network
pool
.
state
=
'none'
pool
.
save
pool
.
schedule
end
end
class
BackupRestorer
attr_accessor
:progress
,
:repository
,
:backup_repos_path
def
initialize
(
progress
,
repository
,
backup_repos_path
)
@progress
=
progress
@repository
=
repository
@backup_repos_path
=
backup_repos_path
end
FileUtils
.
mkdir_p
(
repository_backup_path
(
repository
))
def
backup
progress
.
puts
" *
#{
display_repo_path
}
... "
if
repository
.
empty?
progress
.
puts
" *
#{
display_repo_path
}
... "
+
"[SKIPPED]"
.
color
(
:cyan
)
return
end
repository_bundle_path
=
path_to_bundle
(
repository
)
repository
.
bundle_to_disk
(
repository_bundle_path
)
FileUtils
.
mkdir_p
(
repository_backup_path
)
custom_hooks_path
=
custom_hooks_tar
(
repository
)
repository
.
gitaly_repository_client
.
backup_custom_hooks
(
custom_hooks_path
)
repository
.
bundle_to_disk
(
path_to_bundle
)
repository
.
gitaly_repository_client
.
backup_custom_hooks
(
custom_hooks_tar
)
progress
.
puts
" *
#{
display_repo_path
(
repository
)
}
... "
+
"[DONE]"
.
color
(
:green
)
progress
.
puts
" *
#{
display_repo_path
}
... "
+
"[DONE]"
.
color
(
:green
)
rescue
=>
e
progress_warn
(
container
,
e
,
'Failed to backup repo'
)
progress_warn
(
e
,
'Failed to backup repo'
)
end
def
progress_warn
(
project
,
cmd
,
output
)
progress
.
puts
"[WARNING] Executing
#{
cmd
}
"
.
color
(
:orange
)
progress
.
puts
"Ignoring error on
#{
display_repo_path
(
project
)
}
-
#{
output
}
"
.
color
(
:orange
)
def
restore
(
always_create:
false
)
progress
.
puts
" *
#{
display_repo_path
}
... "
repository
.
remove
rescue
nil
if
File
.
exist?
(
path_to_bundle
)
repository
.
create_from_bundle
(
path_to_bundle
)
restore_custom_hooks
elsif
always_create
repository
.
create_repository
end
def
empty_repository?
(
repository
)
repository
.
expire_emptiness_caches
repository
.
empty?
progress
.
puts
" *
#{
display_repo_path
}
... "
+
"[DONE]"
.
color
(
:green
)
rescue
=>
e
progress_warn
(
e
,
'Failed to restore repo'
)
end
def
display_repo_path
(
repository
)
private
def
display_repo_path
"
#{
repository
.
full_path
}
(
#{
repository
.
disk_path
}
)"
end
def
restore_object_pools
PoolRepository
.
includes
(
:source_project
).
find_each
do
|
pool
|
progress
.
puts
" - Object pool
#{
pool
.
disk_path
}
..."
def
repository_backup_path
@repository_backup_path
||=
File
.
join
(
backup_repos_path
,
repository
.
disk_path
)
end
pool
.
source_project
||=
pool
.
member_projects
.
first
.
root_of_fork_network
pool
.
state
=
'none'
pool
.
save
def
path_to_bundle
@path_to_bundle
||=
File
.
join
(
backup_repos_path
,
repository
.
disk_path
+
'.bundle'
)
end
pool
.
schedule
def
restore_custom_hooks
return
unless
Dir
.
exist?
(
repository_backup_path
)
return
if
Dir
.
glob
(
"
#{
repository_backup_path
}
/custom_hooks*"
).
none?
repository
.
gitaly_repository_client
.
restore_custom_hooks
(
custom_hooks_tar
)
end
def
custom_hooks_tar
File
.
join
(
repository_backup_path
,
"custom_hooks.tar"
)
end
def
progress_warn
(
cmd
,
output
)
progress
.
puts
"[WARNING] Executing
#{
cmd
}
"
.
color
(
:orange
)
progress
.
puts
"Ignoring error on
#{
display_repo_path
}
-
#{
output
}
"
.
color
(
:orange
)
end
end
...
...
lib/tasks/gitlab/backup.rake
View file @
eadde611
...
...
@@ -107,7 +107,7 @@ namespace :gitlab do
puts
"GITLAB_BACKUP_MAX_CONCURRENCY and GITLAB_BACKUP_MAX_STORAGE_CONCURRENCY must have a value of at least 1"
.
color
(
:red
)
exit
1
else
Backup
::
Repositor
y
.
new
(
progress
).
dump
(
Backup
::
Repositor
ies
.
new
(
progress
).
dump
(
max_concurrency:
max_concurrency
,
max_storage_concurrency:
max_storage_concurrency
)
...
...
@@ -117,7 +117,7 @@ namespace :gitlab do
task
restore: :gitlab_environment
do
puts_time
"Restoring repositories ..."
.
color
(
:blue
)
Backup
::
Repositor
y
.
new
(
progress
).
restore
Backup
::
Repositor
ies
.
new
(
progress
).
restore
puts_time
"done"
.
color
(
:green
)
end
end
...
...
spec/lib/backup/repositor
y
_spec.rb
→
spec/lib/backup/repositor
ies
_spec.rb
View file @
eadde611
...
...
@@ -2,7 +2,7 @@
require
'spec_helper'
RSpec
.
describe
Backup
::
Repositor
y
do
RSpec
.
describe
Backup
::
Repositor
ies
do
let
(
:progress
)
{
StringIO
.
new
}
subject
{
described_class
.
new
(
progress
)
}
...
...
@@ -190,46 +190,15 @@ RSpec.describe Backup::Repository do
end
it
'cleans existing repositories'
do
wiki_repository_spy
=
spy
(
:wiki
)
expect
(
Repository
).
to
receive
(
:new
).
twice
.
and_wrap_original
do
|
method
,
*
original_args
|
repository
=
method
.
call
(
*
original_args
)
allow_next_instance_of
(
ProjectWiki
)
do
|
project_wiki
|
allow
(
project_wiki
).
to
receive
(
:repository
).
and_return
(
wiki_repository_spy
)
end
expect
(
repository
).
to
receive
(
:remove
)
expect_next_instance_of
(
Repository
)
do
|
repo
|
expect
(
repo
).
to
receive
(
:remove
)
repository
end
subject
.
restore
expect
(
wiki_repository_spy
).
to
have_received
(
:remove
)
end
end
describe
'#empty_repository?'
do
context
'for a wiki'
do
let
(
:wiki
)
{
create
(
:project_wiki
)
}
let
(
:repository
)
{
wiki
.
repository
}
it
'invalidates the emptiness cache'
do
expect
(
repository
).
to
receive
(
:expire_emptiness_caches
).
once
subject
.
send
(
:empty_repository?
,
repository
)
end
context
'wiki repo has content'
do
let!
(
:wiki_page
)
{
create
(
:wiki_page
,
wiki:
wiki
)
}
it
'returns false, regardless of bad cache value'
do
expect
(
subject
.
send
(
:empty_repository?
,
repository
)).
to
be_falsy
end
end
context
'wiki repo does not have content'
do
it
'returns true, regardless of bad cache value'
do
expect
(
subject
.
send
(
:empty_repository?
,
repository
)).
to
be_truthy
end
end
end
end
end
spec/tasks/gitlab/backup_rake_spec.rb
View file @
eadde611
...
...
@@ -370,7 +370,7 @@ RSpec.describe 'gitlab:app namespace rake task', :delete do
end
it
'has defaults'
do
expect_next_instance_of
(
::
Backup
::
Repositor
y
)
do
|
instance
|
expect_next_instance_of
(
::
Backup
::
Repositor
ies
)
do
|
instance
|
expect
(
instance
).
to
receive
(
:dump
)
.
with
(
max_concurrency:
1
,
max_storage_concurrency:
1
)
.
and_call_original
...
...
@@ -383,7 +383,7 @@ RSpec.describe 'gitlab:app namespace rake task', :delete do
stub_env
(
'GITLAB_BACKUP_MAX_CONCURRENCY'
,
5
)
stub_env
(
'GITLAB_BACKUP_MAX_STORAGE_CONCURRENCY'
,
2
)
expect_next_instance_of
(
::
Backup
::
Repositor
y
)
do
|
instance
|
expect_next_instance_of
(
::
Backup
::
Repositor
ies
)
do
|
instance
|
expect
(
instance
).
to
receive
(
:dump
)
.
with
(
max_concurrency:
5
,
max_storage_concurrency:
2
)
.
and_call_original
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment