Commit bb7dde0d94de939958063f06465c65e239748cfd

Authored by Dmitriy Zaporozhets
2 parents d3862c0e 4dae41d5

Merge pull request #1065 from moregeek/rake_task_for_backup_whole_gitlab

add: rake task to backup/restore gitlab db and repos
config/gitlab.yml.example
... ... @@ -21,6 +21,8 @@ email:
21 21 # Like default project limit for user etc
22 22 app:
23 23 default_projects_limit: 10
  24 + # backup_path: "/vol/backups" # default: Rails.root + backups/
  25 + # backup_keep_time: 604800 # default: 0 (forever) (in seconds)
24 26  
25 27  
26 28 #
... ...
config/initializers/1_settings.rb
... ... @@ -101,5 +101,15 @@ class Settings < Settingslogic
101 101 def default_projects_limit
102 102 app['default_projects_limit'] || 10
103 103 end
  104 +
  105 + def backup_path
  106 + t = app['backup_path'] || "backups/"
  107 + t = /^\//.match(t) ? t : File.join(Rails.root + t)
  108 + t
  109 + end
  110 +
  111 + def backup_keep_time
  112 + app['backup_keep_time'] || 0
  113 + end
104 114 end
105 115 end
... ...
lib/tasks/gitlab/backup.rake 0 → 100644
... ... @@ -0,0 +1,190 @@
  1 +require 'active_record/fixtures'
  2 +
  3 +namespace :gitlab do
  4 + namespace :app do
  5 +
  6 + # Create backup of gitlab system
  7 + desc "GITLAB | Create a backup of the gitlab system"
  8 + task :backup_create => :environment do
  9 +
  10 + Rake::Task["gitlab:app:db_dump"].invoke
  11 + Rake::Task["gitlab:app:repo_dump"].invoke
  12 +
  13 + Dir.chdir(Gitlab.config.backup_path)
  14 +
  15 + # saving additional informations
  16 + s = Hash.new
  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 + File.open("#{Gitlab.config.backup_path}/backup_information.yml", "w+") do |file|
  23 + file << s.to_yaml.gsub(/^---\n/,'')
  24 + end
  25 +
  26 + # create archive
  27 + print "Creating backup archive: #{Time.now.to_i}_gitlab_backup.tar "
  28 + if Kernel.system("tar -cf #{Time.now.to_i}_gitlab_backup.tar repositories/ db/ backup_information.yml")
  29 + puts "[DONE]".green
  30 + else
  31 + puts "[FAILED]".red
  32 + end
  33 +
  34 + # cleanup: remove tmp files
  35 + print "Deletion of tmp directories..."
  36 + if Kernel.system("rm -rf repositories/ db/ backup_information.yml")
  37 + puts "[DONE]".green
  38 + else
  39 + puts "[FAILED]".red
  40 + end
  41 +
  42 + # delete backups
  43 + print "Deleting old backups... "
  44 + if Gitlab.config.backup_keep_time > 0
  45 + file_list = Dir.glob("*_gitlab_backup.tar").map { |f| f.split(/_/).first.to_i }
  46 + file_list.sort.each do |timestamp|
  47 + if Time.at(timestamp) < (Time.now - Gitlab.config.backup_keep_time)
  48 + %x{rm #{timestamp}_gitlab_backup.tar}
  49 + end
  50 + end
  51 + puts "[DONE]".green
  52 + else
  53 + puts "[SKIPPING]".yellow
  54 + end
  55 +
  56 + end
  57 +
  58 +
  59 + # Restore backup of gitlab system
  60 + desc "GITLAB | Restore a previously created backup"
  61 + task :backup_restore => :environment do
  62 +
  63 + Dir.chdir(Gitlab.config.backup_path)
  64 +
  65 + # check for existing backups in the backup dir
  66 + file_list = Dir.glob("*_gitlab_backup.tar").each.map { |f| f.split(/_/).first.to_i }
  67 + puts "no backup found" if file_list.count == 0
  68 + if file_list.count > 1 && ENV["BACKUP"].nil?
  69 + puts "Found more than one backup, please specify which one you want to restore:"
  70 + puts "rake gitlab:app:backup_restore BACKUP=timestamp_of_backup"
  71 + exit 1;
  72 + end
  73 +
  74 + tar_file = ENV["BACKUP"].nil? ? File.join(file_list.first.to_s + "_gitlab_backup.tar") : File.join(ENV["BACKUP"] + "_gitlab_backup.tar")
  75 +
  76 + unless File.exists?(tar_file)
  77 + puts "The specified backup doesn't exist!"
  78 + exit 1;
  79 + end
  80 +
  81 + print "Unpacking backup... "
  82 + unless Kernel.system("tar -xf #{tar_file}")
  83 + puts "[FAILED]".red
  84 + exit 1
  85 + else
  86 + puts "[DONE]".green
  87 + end
  88 +
  89 + settings = YAML.load_file("backup_information.yml")
  90 + ENV["VERSION"] = "#{settings["db_version"]}" if settings["db_version"].to_i > 0
  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 +
  101 + Rake::Task["gitlab:app:db_restore"].invoke
  102 + Rake::Task["gitlab:app:repo_restore"].invoke
  103 +
  104 + # cleanup: remove tmp files
  105 + print "Deletion of tmp directories..."
  106 + if Kernel.system("rm -rf repositories/ db/ backup_information.yml")
  107 + puts "[DONE]".green
  108 + else
  109 + puts "[FAILED]".red
  110 + end
  111 +
  112 + end
  113 +
  114 +
  115 + ################################################################################
  116 + ################################# invoked tasks ################################
  117 +
  118 + ################################# REPOSITORIES #################################
  119 +
  120 + task :repo_dump => :environment do
  121 + backup_path_repo = File.join(Gitlab.config.backup_path, "repositories")
  122 + FileUtils.mkdir_p(backup_path_repo) until Dir.exists?(backup_path_repo)
  123 + puts "Dumping repositories:"
  124 + project = Project.all.map { |n| [n.name,n.path_to_repo] }
  125 + project << ["gitolite-admin.git", File.join(File.dirname(project.first.second), "gitolite-admin.git")]
  126 + project.each do |project|
  127 + print "- Dumping repository #{project.first}... "
  128 + if Kernel.system("cd #{project.second} > /dev/null 2>&1 && git bundle create #{backup_path_repo}/#{project.first}.bundle --all > /dev/null 2>&1")
  129 + puts "[DONE]".green
  130 + else
  131 + puts "[FAILED]".red
  132 + end
  133 + end
  134 + end
  135 +
  136 + task :repo_restore => :environment do
  137 + backup_path_repo = File.join(Gitlab.config.backup_path, "repositories")
  138 + puts "Restoring repositories:"
  139 + project = Project.all.map { |n| [n.name,n.path_to_repo] }
  140 + project << ["gitolite-admin.git", File.join(File.dirname(project.first.second), "gitolite-admin.git")]
  141 + project.each do |project|
  142 + print "- Restoring repository #{project.first}... "
  143 + FileUtils.rm_rf(project.second) if File.dirname(project.second) # delet old stuff
  144 + 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")
  145 + puts "[DONE]".green
  146 + else
  147 + puts "[FAILED]".red
  148 + end
  149 + end
  150 + end
  151 +
  152 + ###################################### DB ######################################
  153 +
  154 + task :db_dump => :environment do
  155 + backup_path_db = File.join(Gitlab.config.backup_path, "db")
  156 + FileUtils.mkdir_p(backup_path_db) until Dir.exists?(backup_path_db)
  157 + puts "Dumping database tables:"
  158 + ActiveRecord::Base.connection.tables.each do |tbl|
  159 + print "- Dumping table #{tbl}... "
  160 + count = 1
  161 + File.open(File.join(backup_path_db, tbl + ".yml"), "w+") do |file|
  162 + ActiveRecord::Base.connection.select_all("SELECT * FROM #{tbl}").each do |line|
  163 + line.delete_if{|k,v| v.blank?}
  164 + output = {tbl + '_' + count.to_s => line}
  165 + file << output.to_yaml.gsub(/^---\n/,'') + "\n"
  166 + count += 1
  167 + end
  168 + puts "[DONE]".green
  169 + end
  170 + end
  171 + end
  172 +
  173 + task :db_restore=> :environment do
  174 + backup_path_db = File.join(Gitlab.config.backup_path, "db")
  175 + puts "Restoring database tables:"
  176 + Rake::Task["db:reset"].invoke
  177 + Dir.glob(File.join(backup_path_db, "*.yml") ).each do |dir|
  178 + fixture_file = File.basename(dir, ".*" )
  179 + print "- Loading fixture #{fixture_file}..."
  180 + if File.size(dir) > 0
  181 + ActiveRecord::Fixtures.create_fixtures(backup_path_db, fixture_file)
  182 + puts "[DONE]".green
  183 + else
  184 + puts "[SKIPPING]".yellow
  185 + end
  186 + end
  187 + end
  188 +
  189 + end # namespace end: app
  190 +end # namespace end: gitlab
... ...