Commit 12b34c8115bbb35524880bca4f21ad0939de4b77

Authored by Stefan Morgenthaler
1 parent be1b4080

add: rake task to backup/restore gitlab db and repos

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