Commit 83924495993753e6c503ce539c6533b54af6f86a

Authored by Dmitriy Zaporozhets
1 parent 9df17fa4

Fix backup/restore of repos

Showing 1 changed file with 34 additions and 20 deletions   Show diff stats
lib/tasks/gitlab/backup.rake
@@ -119,35 +119,49 @@ namespace :gitlab do @@ -119,35 +119,49 @@ namespace :gitlab do
119 backup_path_repo = File.join(Gitlab.config.backup.path, "repositories") 119 backup_path_repo = File.join(Gitlab.config.backup.path, "repositories")
120 FileUtils.mkdir_p(backup_path_repo) until Dir.exists?(backup_path_repo) 120 FileUtils.mkdir_p(backup_path_repo) until Dir.exists?(backup_path_repo)
121 puts "Dumping repositories ..." 121 puts "Dumping repositories ..."
122 - project = Project.all.map { |n| [n.path, n.path_to_repo] }  
123 - project << ["gitolite-admin.git", File.join(Gitlab.config.git_base_path, "gitolite-admin.git")]  
124 - project.each do |project|  
125 - print "#{project.first.yellow} ... "  
126 - if Kernel.system("cd #{project.second} > /dev/null 2>&1 && git bundle create #{backup_path_repo}/#{project.first}.bundle --all > /dev/null 2>&1")  
127 - puts "done".green 122 +
  123 + Project.find_each(:batch_size => 1000) do |project|
  124 + print "#{project.path_with_namespace} ... "
  125 +
  126 + if project.empty_repo?
  127 + puts "[SKIPPED]".cyan
  128 + next
  129 + end
  130 +
  131 + # Create namespace dir if missing
  132 + FileUtils.mkdir_p(File.join(backup_path_repo, project.namespace.path)) if project.namespace
  133 +
  134 + # Build a destination path for backup
  135 + path_to_bundle = File.join(backup_path_repo, project.path_with_namespace + ".bundle")
  136 +
  137 + if Kernel.system("cd #{project.path_to_repo} > /dev/null 2>&1 && git bundle create #{path_to_bundle} --all > /dev/null 2>&1")
  138 + puts "[DONE]".green
128 else 139 else
129 - puts "failed".red 140 + puts "[FAILED]".red
130 end 141 end
131 end 142 end
132 end 143 end
133 144
134 task :restore => :environment do 145 task :restore => :environment do
135 backup_path_repo = File.join(Gitlab.config.backup.path, "repositories") 146 backup_path_repo = File.join(Gitlab.config.backup.path, "repositories")
  147 + repos_path = Gitlab.config.gitolite.repos_path
  148 +
136 puts "Restoring repositories ... " 149 puts "Restoring repositories ... "
137 - project = Project.all.map { |n| [n.path, n.path_to_repo] }  
138 - project << ["gitolite-admin.git", File.join(Gitlab.config.git_base_path, "gitolite-admin.git")]  
139 - project.each do |project|  
140 - print "#{project.first.yellow} ... "  
141 - FileUtils.rm_rf(project.second) if File.dirname(project.second) # delete old stuff  
142 - if Kernel.system("cd #{File.dirname(project.second)} > /dev/null 2>&1 && git clone --bare #{backup_path_repo}/#{project.first}.bundle #{project.first}.git > /dev/null 2>&1")  
143 - permission_commands = [  
144 - "sudo chmod -R g+rwX #{Gitlab.config.git_base_path}",  
145 - "sudo chown -R #{Gitlab.config.ssh_user}:#{Gitlab.config.ssh_user} #{Gitlab.config.git_base_path}"  
146 - ]  
147 - permission_commands.each { |command| Kernel.system(command) }  
148 - puts "done".green 150 +
  151 + Project.find_each(:batch_size => 1000) do |project|
  152 + print "#{project.path_with_namespace} ... "
  153 +
  154 + if project.namespace
  155 + project.namespace.ensure_dir_exist
  156 + end
  157 +
  158 + # Build a backup path
  159 + path_to_bundle = File.join(backup_path_repo, project.path_with_namespace + ".bundle")
  160 +
  161 + if Kernel.system("git clone --bare #{path_to_bundle} #{project.path_to_repo} > /dev/null 2>&1")
  162 + puts "[DONE]".green
149 else 163 else
150 - puts "failed".red 164 + puts "[FAILED]".red
151 end 165 end
152 end 166 end
153 end 167 end