Commit 02e859a23470d799a77963503687789d4a410a0c
1 parent
af5faaf0
Exists in
master
and in
4 other branches
move backup logic to lib. Fixed removing outdated backups
Showing
3 changed files
with
114 additions
and
95 deletions
Show diff stats
.gitignore
| @@ -0,0 +1,106 @@ | @@ -0,0 +1,106 @@ | ||
| 1 | +module Backup | ||
| 2 | + class Manager | ||
| 3 | + def pack | ||
| 4 | + # saving additional informations | ||
| 5 | + s = {} | ||
| 6 | + s[:db_version] = "#{ActiveRecord::Migrator.current_version}" | ||
| 7 | + s[:backup_created_at] = Time.now | ||
| 8 | + s[:gitlab_version] = %x{git rev-parse HEAD}.gsub(/\n/,"") | ||
| 9 | + s[:tar_version] = %x{tar --version | head -1}.gsub(/\n/,"") | ||
| 10 | + | ||
| 11 | + Dir.chdir(Gitlab.config.backup.path) | ||
| 12 | + | ||
| 13 | + File.open("#{Gitlab.config.backup.path}/backup_information.yml", "w+") do |file| | ||
| 14 | + file << s.to_yaml.gsub(/^---\n/,'') | ||
| 15 | + end | ||
| 16 | + | ||
| 17 | + # create archive | ||
| 18 | + print "Creating backup archive: #{s[:backup_created_at].to_i}_gitlab_backup.tar ... " | ||
| 19 | + if Kernel.system("tar -cf #{s[:backup_created_at].to_i}_gitlab_backup.tar repositories/ db/ uploads/ backup_information.yml") | ||
| 20 | + puts "done".green | ||
| 21 | + else | ||
| 22 | + puts "failed".red | ||
| 23 | + end | ||
| 24 | + end | ||
| 25 | + | ||
| 26 | + def cleanup | ||
| 27 | + print "Deleting tmp directories ... " | ||
| 28 | + if Kernel.system("rm -rf repositories/ db/ uploads/ backup_information.yml") | ||
| 29 | + puts "done".green | ||
| 30 | + else | ||
| 31 | + puts "failed".red | ||
| 32 | + end | ||
| 33 | + end | ||
| 34 | + | ||
| 35 | + def remove_old | ||
| 36 | + # delete backups | ||
| 37 | + print "Deleting old backups ... " | ||
| 38 | + keep_time = Gitlab.config.backup.keep_time.to_i | ||
| 39 | + path = Gitlab.config.backup.path | ||
| 40 | + | ||
| 41 | + if keep_time > 0 | ||
| 42 | + removed = 0 | ||
| 43 | + file_list = Dir.glob(Rails.root.join(path, "*_gitlab_backup.tar")) | ||
| 44 | + file_list.map! { |f| $1.to_i if f =~ /(\d+)_gitlab_backup.tar/ } | ||
| 45 | + file_list.sort.each do |timestamp| | ||
| 46 | + if Time.at(timestamp) < (Time.now - keep_time) | ||
| 47 | + if system("rm #{timestamp}_gitlab_backup.tar") | ||
| 48 | + removed += 1 | ||
| 49 | + end | ||
| 50 | + end | ||
| 51 | + end | ||
| 52 | + puts "done. (#{removed} removed)".green | ||
| 53 | + else | ||
| 54 | + puts "skipping".yellow | ||
| 55 | + end | ||
| 56 | + end | ||
| 57 | + | ||
| 58 | + def unpack | ||
| 59 | + Dir.chdir(Gitlab.config.backup.path) | ||
| 60 | + | ||
| 61 | + # check for existing backups in the backup dir | ||
| 62 | + file_list = Dir.glob("*_gitlab_backup.tar").each.map { |f| f.split(/_/).first.to_i } | ||
| 63 | + puts "no backups found" if file_list.count == 0 | ||
| 64 | + if file_list.count > 1 && ENV["BACKUP"].nil? | ||
| 65 | + puts "Found more than one backup, please specify which one you want to restore:" | ||
| 66 | + puts "rake gitlab:backup:restore BACKUP=timestamp_of_backup" | ||
| 67 | + exit 1 | ||
| 68 | + end | ||
| 69 | + | ||
| 70 | + tar_file = ENV["BACKUP"].nil? ? File.join("#{file_list.first}_gitlab_backup.tar") : File.join(ENV["BACKUP"] + "_gitlab_backup.tar") | ||
| 71 | + | ||
| 72 | + unless File.exists?(tar_file) | ||
| 73 | + puts "The specified backup doesn't exist!" | ||
| 74 | + exit 1 | ||
| 75 | + end | ||
| 76 | + | ||
| 77 | + print "Unpacking backup ... " | ||
| 78 | + unless Kernel.system("tar -xf #{tar_file}") | ||
| 79 | + puts "failed".red | ||
| 80 | + exit 1 | ||
| 81 | + else | ||
| 82 | + puts "done".green | ||
| 83 | + end | ||
| 84 | + | ||
| 85 | + settings = YAML.load_file("backup_information.yml") | ||
| 86 | + ENV["VERSION"] = "#{settings[:db_version]}" if settings[:db_version].to_i > 0 | ||
| 87 | + | ||
| 88 | + # backups directory is not always sub of Rails root and able to execute the git rev-parse below | ||
| 89 | + begin | ||
| 90 | + Dir.chdir(Rails.root) | ||
| 91 | + | ||
| 92 | + # restoring mismatching backups can lead to unexpected problems | ||
| 93 | + if settings[:gitlab_version] != %x{git rev-parse HEAD}.gsub(/\n/, "") | ||
| 94 | + puts "GitLab version mismatch:".red | ||
| 95 | + puts " Your current HEAD differs from the HEAD in the backup!".red | ||
| 96 | + puts " Please switch to the following revision and try again:".red | ||
| 97 | + puts " revision: #{settings[:gitlab_version]}".red | ||
| 98 | + exit 1 | ||
| 99 | + end | ||
| 100 | + ensure | ||
| 101 | + # chdir back to original intended dir | ||
| 102 | + Dir.chdir(Gitlab.config.backup.path) | ||
| 103 | + end | ||
| 104 | + end | ||
| 105 | + end | ||
| 106 | +end |
lib/tasks/gitlab/backup.rake
| @@ -11,49 +11,10 @@ namespace :gitlab do | @@ -11,49 +11,10 @@ namespace :gitlab do | ||
| 11 | Rake::Task["gitlab:backup:repo:create"].invoke | 11 | Rake::Task["gitlab:backup:repo:create"].invoke |
| 12 | Rake::Task["gitlab:backup:uploads:create"].invoke | 12 | Rake::Task["gitlab:backup:uploads:create"].invoke |
| 13 | 13 | ||
| 14 | - | ||
| 15 | - # saving additional informations | ||
| 16 | - s = {} | ||
| 17 | - s[:db_version] = "#{ActiveRecord::Migrator.current_version}" | ||
| 18 | - s[:backup_created_at] = Time.now | ||
| 19 | - s[:gitlab_version] = %x{git rev-parse HEAD}.gsub(/\n/,"") | ||
| 20 | - s[:tar_version] = %x{tar --version | head -1}.gsub(/\n/,"") | ||
| 21 | - | ||
| 22 | - Dir.chdir(Gitlab.config.backup.path) | ||
| 23 | - | ||
| 24 | - File.open("#{Gitlab.config.backup.path}/backup_information.yml", "w+") do |file| | ||
| 25 | - file << s.to_yaml.gsub(/^---\n/,'') | ||
| 26 | - end | ||
| 27 | - | ||
| 28 | - # create archive | ||
| 29 | - print "Creating backup archive: #{s[:backup_created_at].to_i}_gitlab_backup.tar ... " | ||
| 30 | - if Kernel.system("tar -cf #{s[:backup_created_at].to_i}_gitlab_backup.tar repositories/ db/ uploads/ backup_information.yml") | ||
| 31 | - puts "done".green | ||
| 32 | - else | ||
| 33 | - puts "failed".red | ||
| 34 | - end | ||
| 35 | - | ||
| 36 | - # cleanup: remove tmp files | ||
| 37 | - print "Deleting tmp directories ... " | ||
| 38 | - if Kernel.system("rm -rf repositories/ db/ uploads/ backup_information.yml") | ||
| 39 | - puts "done".green | ||
| 40 | - else | ||
| 41 | - puts "failed".red | ||
| 42 | - end | ||
| 43 | - | ||
| 44 | - # delete backups | ||
| 45 | - print "Deleting old backups ... " | ||
| 46 | - if Gitlab.config.backup.keep_time > 0 | ||
| 47 | - file_list = Dir.glob("*_gitlab_backup.tar").map { |f| f.split(/_/).first.to_i } | ||
| 48 | - file_list.sort.each do |timestamp| | ||
| 49 | - if Time.at(timestamp) < (Time.now - Gitlab.config.backup.keep_time) | ||
| 50 | - %x{rm #{timestamp}_gitlab_backup.tar} | ||
| 51 | - end | ||
| 52 | - end | ||
| 53 | - puts "done".green | ||
| 54 | - else | ||
| 55 | - puts "skipping".yellow | ||
| 56 | - end | 14 | + backup = Backup::Manager.new |
| 15 | + backup.pack | ||
| 16 | + backup.cleanup | ||
| 17 | + backup.remove_old | ||
| 57 | end | 18 | end |
| 58 | 19 | ||
| 59 | # Restore backup of GitLab system | 20 | # Restore backup of GitLab system |
| @@ -61,64 +22,15 @@ namespace :gitlab do | @@ -61,64 +22,15 @@ namespace :gitlab do | ||
| 61 | task restore: :environment do | 22 | task restore: :environment do |
| 62 | warn_user_is_not_gitlab | 23 | warn_user_is_not_gitlab |
| 63 | 24 | ||
| 64 | - Dir.chdir(Gitlab.config.backup.path) | ||
| 65 | - | ||
| 66 | - # check for existing backups in the backup dir | ||
| 67 | - file_list = Dir.glob("*_gitlab_backup.tar").each.map { |f| f.split(/_/).first.to_i } | ||
| 68 | - puts "no backups found" if file_list.count == 0 | ||
| 69 | - if file_list.count > 1 && ENV["BACKUP"].nil? | ||
| 70 | - puts "Found more than one backup, please specify which one you want to restore:" | ||
| 71 | - puts "rake gitlab:backup:restore BACKUP=timestamp_of_backup" | ||
| 72 | - exit 1 | ||
| 73 | - end | ||
| 74 | - | ||
| 75 | - tar_file = ENV["BACKUP"].nil? ? File.join("#{file_list.first}_gitlab_backup.tar") : File.join(ENV["BACKUP"] + "_gitlab_backup.tar") | ||
| 76 | - | ||
| 77 | - unless File.exists?(tar_file) | ||
| 78 | - puts "The specified backup doesn't exist!" | ||
| 79 | - exit 1 | ||
| 80 | - end | ||
| 81 | - | ||
| 82 | - print "Unpacking backup ... " | ||
| 83 | - unless Kernel.system("tar -xf #{tar_file}") | ||
| 84 | - puts "failed".red | ||
| 85 | - exit 1 | ||
| 86 | - else | ||
| 87 | - puts "done".green | ||
| 88 | - end | ||
| 89 | - | ||
| 90 | - settings = YAML.load_file("backup_information.yml") | ||
| 91 | - ENV["VERSION"] = "#{settings[:db_version]}" if settings[:db_version].to_i > 0 | ||
| 92 | - | ||
| 93 | - # backups directory is not always sub of Rails root and able to execute the git rev-parse below | ||
| 94 | - begin | ||
| 95 | - Dir.chdir(Rails.root) | ||
| 96 | - | ||
| 97 | - # restoring mismatching backups can lead to unexpected problems | ||
| 98 | - if settings[:gitlab_version] != %x{git rev-parse HEAD}.gsub(/\n/, "") | ||
| 99 | - puts "GitLab version mismatch:".red | ||
| 100 | - puts " Your current HEAD differs from the HEAD in the backup!".red | ||
| 101 | - puts " Please switch to the following revision and try again:".red | ||
| 102 | - puts " revision: #{settings[:gitlab_version]}".red | ||
| 103 | - exit 1 | ||
| 104 | - end | ||
| 105 | - ensure | ||
| 106 | - # chdir back to original intended dir | ||
| 107 | - Dir.chdir(Gitlab.config.backup.path) | ||
| 108 | - end | 25 | + backup = Backup::Manager.new |
| 26 | + backup.unpack | ||
| 109 | 27 | ||
| 110 | Rake::Task["gitlab:backup:db:restore"].invoke | 28 | Rake::Task["gitlab:backup:db:restore"].invoke |
| 111 | Rake::Task["gitlab:backup:repo:restore"].invoke | 29 | Rake::Task["gitlab:backup:repo:restore"].invoke |
| 112 | Rake::Task["gitlab:backup:uploads:restore"].invoke | 30 | Rake::Task["gitlab:backup:uploads:restore"].invoke |
| 113 | Rake::Task["gitlab:shell:setup"].invoke | 31 | Rake::Task["gitlab:shell:setup"].invoke |
| 114 | 32 | ||
| 115 | - # cleanup: remove tmp files | ||
| 116 | - print "Deleting tmp directories ... " | ||
| 117 | - if Kernel.system("rm -rf repositories/ db/ uploads/ backup_information.yml") | ||
| 118 | - puts "done".green | ||
| 119 | - else | ||
| 120 | - puts "failed".red | ||
| 121 | - end | 33 | + backup.cleanup |
| 122 | end | 34 | end |
| 123 | 35 | ||
| 124 | namespace :repo do | 36 | namespace :repo do |