Commit c9bf2bb288cffba2d4a30f1937ca88b9b92e64ed
1 parent
56f9a674
Exists in
master
and in
4 other branches
Rename gitlab:app:backup_* to gitlab:backup:*
Rename gitlab:app:db_* to gitlab:backup:db:* Rename gitlab:app:repo_* to gitlab:backup:repo:* Rename *_dump to *_create
Showing
2 changed files
with
73 additions
and
69 deletions
Show diff stats
doc/raketasks/backup_restore.md
| ... | ... | @@ -4,7 +4,7 @@ Creates a backup archive of the database and all repositories. This archive will |
| 4 | 4 | The filename will be `[TIMESTAMP]_gitlab_backup.tar`. This timestamp can be used to restore an specific backup. |
| 5 | 5 | |
| 6 | 6 | ``` |
| 7 | -bundle exec rake gitlab:app:backup_create | |
| 7 | +bundle exec rake gitlab:backup:create | |
| 8 | 8 | ``` |
| 9 | 9 | |
| 10 | 10 | Example output: |
| ... | ... | @@ -40,7 +40,7 @@ Deleting old backups... [SKIPPING] |
| 40 | 40 | ### Restore a previously created backup |
| 41 | 41 | |
| 42 | 42 | ``` |
| 43 | -bundle exec rake gitlab:app:backup_restore | |
| 43 | +bundle exec rake gitlab:backup:restore | |
| 44 | 44 | ``` |
| 45 | 45 | |
| 46 | 46 | Options: | ... | ... |
lib/tasks/gitlab/backup.rake
| 1 | 1 | require 'active_record/fixtures' |
| 2 | 2 | |
| 3 | 3 | namespace :gitlab do |
| 4 | - namespace :app do | |
| 4 | + namespace :backup do | |
| 5 | 5 | # Create backup of GitLab system |
| 6 | 6 | desc "GITLAB | Create a backup of the GitLab system" |
| 7 | - task :backup_create => :environment do | |
| 8 | - Rake::Task["gitlab:app:db_dump"].invoke | |
| 9 | - Rake::Task["gitlab:app:repo_dump"].invoke | |
| 7 | + task :create => :environment do | |
| 8 | + Rake::Task["gitlab:backup:db:create"].invoke | |
| 9 | + Rake::Task["gitlab:backup:repo:create"].invoke | |
| 10 | 10 | |
| 11 | 11 | Dir.chdir(Gitlab.config.backup.path) |
| 12 | 12 | |
| ... | ... | @@ -54,7 +54,7 @@ namespace :gitlab do |
| 54 | 54 | |
| 55 | 55 | # Restore backup of GitLab system |
| 56 | 56 | desc "GITLAB | Restore a previously created backup" |
| 57 | - task :backup_restore => :environment do | |
| 57 | + task :restore => :environment do | |
| 58 | 58 | Dir.chdir(Gitlab.config.backup.path) |
| 59 | 59 | |
| 60 | 60 | # check for existing backups in the backup dir |
| ... | ... | @@ -62,7 +62,7 @@ namespace :gitlab do |
| 62 | 62 | puts "no backups found" if file_list.count == 0 |
| 63 | 63 | if file_list.count > 1 && ENV["BACKUP"].nil? |
| 64 | 64 | puts "Found more than one backup, please specify which one you want to restore:" |
| 65 | - puts "rake gitlab:app:backup_restore BACKUP=timestamp_of_backup" | |
| 65 | + puts "rake gitlab:backup:restore BACKUP=timestamp_of_backup" | |
| 66 | 66 | exit 1; |
| 67 | 67 | end |
| 68 | 68 | |
| ... | ... | @@ -93,8 +93,8 @@ namespace :gitlab do |
| 93 | 93 | exit 1 |
| 94 | 94 | end |
| 95 | 95 | |
| 96 | - Rake::Task["gitlab:app:db_restore"].invoke | |
| 97 | - Rake::Task["gitlab:app:repo_restore"].invoke | |
| 96 | + Rake::Task["gitlab:backup:db:restore"].invoke | |
| 97 | + Rake::Task["gitlab:backup:repo:restore"].invoke | |
| 98 | 98 | |
| 99 | 99 | # cleanup: remove tmp files |
| 100 | 100 | print "Deleting tmp directories..." |
| ... | ... | @@ -110,82 +110,86 @@ namespace :gitlab do |
| 110 | 110 | |
| 111 | 111 | ################################# REPOSITORIES ################################# |
| 112 | 112 | |
| 113 | - task :repo_dump => :environment do | |
| 114 | - backup_path_repo = File.join(Gitlab.config.backup.path, "repositories") | |
| 115 | - FileUtils.mkdir_p(backup_path_repo) until Dir.exists?(backup_path_repo) | |
| 116 | - puts "Dumping repositories:" | |
| 117 | - project = Project.all.map { |n| [n.path, n.path_to_repo] } | |
| 118 | - project << ["gitolite-admin.git", File.join(File.dirname(project.first.second), "gitolite-admin.git")] | |
| 119 | - project.each do |project| | |
| 120 | - print "- Dumping repository #{project.first}... " | |
| 121 | - if Kernel.system("cd #{project.second} > /dev/null 2>&1 && git bundle create #{backup_path_repo}/#{project.first}.bundle --all > /dev/null 2>&1") | |
| 122 | - puts "[DONE]".green | |
| 123 | - else | |
| 124 | - puts "[FAILED]".red | |
| 113 | + namespace :repo do | |
| 114 | + task :create => :environment do | |
| 115 | + backup_path_repo = File.join(Gitlab.config.backup.path, "repositories") | |
| 116 | + FileUtils.mkdir_p(backup_path_repo) until Dir.exists?(backup_path_repo) | |
| 117 | + puts "Dumping repositories:" | |
| 118 | + project = Project.all.map { |n| [n.path, n.path_to_repo] } | |
| 119 | + project << ["gitolite-admin.git", File.join(File.dirname(project.first.second), "gitolite-admin.git")] | |
| 120 | + project.each do |project| | |
| 121 | + print "- Dumping repository #{project.first}... " | |
| 122 | + if Kernel.system("cd #{project.second} > /dev/null 2>&1 && git bundle create #{backup_path_repo}/#{project.first}.bundle --all > /dev/null 2>&1") | |
| 123 | + puts "[DONE]".green | |
| 124 | + else | |
| 125 | + puts "[FAILED]".red | |
| 126 | + end | |
| 125 | 127 | end |
| 126 | 128 | end |
| 127 | - end | |
| 128 | 129 | |
| 129 | - task :repo_restore => :environment do | |
| 130 | - backup_path_repo = File.join(Gitlab.config.backup.path, "repositories") | |
| 131 | - puts "Restoring repositories:" | |
| 132 | - project = Project.all.map { |n| [n.path, n.path_to_repo] } | |
| 133 | - project << ["gitolite-admin.git", File.join(File.dirname(project.first.second), "gitolite-admin.git")] | |
| 134 | - project.each do |project| | |
| 135 | - print "- Restoring repository #{project.first}... " | |
| 136 | - FileUtils.rm_rf(project.second) if File.dirname(project.second) # delete old stuff | |
| 137 | - 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") | |
| 138 | - permission_commands = [ | |
| 139 | - "sudo chmod -R g+rwX #{Gitlab.config.gitolite.repos_path}", | |
| 140 | - "sudo chown -R #{Gitlab.config.gitolite.ssh_user}:#{Gitlab.config.gitolite.ssh_user} #{Gitlab.config.gitolite.repos_path}" | |
| 141 | - ] | |
| 142 | - permission_commands.each { |command| Kernel.system(command) } | |
| 143 | - puts "[DONE]".green | |
| 144 | - else | |
| 145 | - puts "[FAILED]".red | |
| 130 | + task :restore => :environment do | |
| 131 | + backup_path_repo = File.join(Gitlab.config.backup.path, "repositories") | |
| 132 | + puts "Restoring repositories:" | |
| 133 | + project = Project.all.map { |n| [n.path, n.path_to_repo] } | |
| 134 | + project << ["gitolite-admin.git", File.join(File.dirname(project.first.second), "gitolite-admin.git")] | |
| 135 | + project.each do |project| | |
| 136 | + print "- Restoring repository #{project.first}... " | |
| 137 | + FileUtils.rm_rf(project.second) if File.dirname(project.second) # delete old stuff | |
| 138 | + 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") | |
| 139 | + permission_commands = [ | |
| 140 | + "sudo chmod -R g+rwX #{Gitlab.config.git_base_path}", | |
| 141 | + "sudo chown -R #{Gitlab.config.ssh_user}:#{Gitlab.config.ssh_user} #{Gitlab.config.git_base_path}" | |
| 142 | + ] | |
| 143 | + permission_commands.each { |command| Kernel.system(command) } | |
| 144 | + puts "[DONE]".green | |
| 145 | + else | |
| 146 | + puts "[FAILED]".red | |
| 147 | + end | |
| 146 | 148 | end |
| 147 | 149 | end |
| 148 | 150 | end |
| 149 | 151 | |
| 150 | 152 | ###################################### DB ###################################### |
| 151 | 153 | |
| 152 | - task :db_dump => :environment do | |
| 153 | - backup_path_db = File.join(Gitlab.config.backup.path, "db") | |
| 154 | - FileUtils.mkdir_p(backup_path_db) unless Dir.exists?(backup_path_db) | |
| 155 | - | |
| 156 | - puts "Dumping database tables:" | |
| 157 | - ActiveRecord::Base.connection.tables.each do |tbl| | |
| 158 | - print "- Dumping table #{tbl}... " | |
| 159 | - count = 1 | |
| 160 | - File.open(File.join(backup_path_db, tbl + ".yml"), "w+") do |file| | |
| 161 | - ActiveRecord::Base.connection.select_all("SELECT * FROM `#{tbl}`").each do |line| | |
| 162 | - line.delete_if{|k,v| v.blank?} | |
| 163 | - output = {tbl + '_' + count.to_s => line} | |
| 164 | - file << output.to_yaml.gsub(/^---\n/,'') + "\n" | |
| 165 | - count += 1 | |
| 154 | + namespace :db do | |
| 155 | + task :create => :environment do | |
| 156 | + backup_path_db = File.join(Gitlab.config.backup.path, "db") | |
| 157 | + FileUtils.mkdir_p(backup_path_db) unless Dir.exists?(backup_path_db) | |
| 158 | + | |
| 159 | + puts "Dumping database tables:" | |
| 160 | + ActiveRecord::Base.connection.tables.each do |tbl| | |
| 161 | + print "- Dumping table #{tbl}... " | |
| 162 | + count = 1 | |
| 163 | + File.open(File.join(backup_path_db, tbl + ".yml"), "w+") do |file| | |
| 164 | + ActiveRecord::Base.connection.select_all("SELECT * FROM `#{tbl}`").each do |line| | |
| 165 | + line.delete_if{|k,v| v.blank?} | |
| 166 | + output = {tbl + '_' + count.to_s => line} | |
| 167 | + file << output.to_yaml.gsub(/^---\n/,'') + "\n" | |
| 168 | + count += 1 | |
| 169 | + end | |
| 170 | + puts "[DONE]".green | |
| 166 | 171 | end |
| 167 | - puts "[DONE]".green | |
| 168 | 172 | end |
| 169 | 173 | end |
| 170 | - end | |
| 171 | 174 | |
| 172 | - task :db_restore=> :environment do | |
| 173 | - backup_path_db = File.join(Gitlab.config.backup.path, "db") | |
| 175 | + task :restore=> :environment do | |
| 176 | + backup_path_db = File.join(Gitlab.config.backup.path, "db") | |
| 174 | 177 | |
| 175 | - puts "Restoring database tables:" | |
| 176 | - Rake::Task["db:reset"].invoke | |
| 178 | + puts "Restoring database tables:" | |
| 179 | + Rake::Task["db:reset"].invoke | |
| 177 | 180 | |
| 178 | - Dir.glob(File.join(backup_path_db, "*.yml") ).each do |dir| | |
| 179 | - fixture_file = File.basename(dir, ".*" ) | |
| 180 | - print "- Loading fixture #{fixture_file}..." | |
| 181 | - if File.size(dir) > 0 | |
| 182 | - ActiveRecord::Fixtures.create_fixtures(backup_path_db, fixture_file) | |
| 183 | - puts "[DONE]".green | |
| 184 | - else | |
| 185 | - puts "[SKIPPING]".yellow | |
| 181 | + Dir.glob(File.join(backup_path_db, "*.yml") ).each do |dir| | |
| 182 | + fixture_file = File.basename(dir, ".*" ) | |
| 183 | + print "- Loading fixture #{fixture_file}..." | |
| 184 | + if File.size(dir) > 0 | |
| 185 | + ActiveRecord::Fixtures.create_fixtures(backup_path_db, fixture_file) | |
| 186 | + puts "[DONE]".green | |
| 187 | + else | |
| 188 | + puts "[SKIPPING]".yellow | |
| 189 | + end | |
| 186 | 190 | end |
| 187 | 191 | end |
| 188 | 192 | end |
| 189 | 193 | |
| 190 | - end # namespace end: app | |
| 194 | + end # namespace end: backup | |
| 191 | 195 | end # namespace end: gitlab | ... | ... |