Commit 38d23c0e5f816937047c9326f9dd33fb10490032

Authored by Dmitriy Zaporozhets
1 parent b65903e0

Replace db:backup/restore with native mysq/pg solution

Showing 2 changed files with 62 additions and 41 deletions   Show diff stats
lib/backup.rb 0 → 100644
... ... @@ -0,0 +1,56 @@
  1 +require 'yaml'
  2 +
  3 +class Backup
  4 + attr_reader :config, :db_dir
  5 +
  6 + def initialize
  7 + @config = YAML.load_file(File.join(Rails.root,'config','database.yml'))[Rails.env]
  8 + @db_dir = File.join(Gitlab.config.backup.path, 'db')
  9 + FileUtils.mkdir_p(@db_dir) unless Dir.exists?(@db_dir)
  10 + end
  11 +
  12 + def backup_db
  13 + case config["adapter"]
  14 + when /^mysql/ then
  15 + system("mysqldump #{mysql_args} #{config['database']} > #{db_file_name}")
  16 + when "postgresql" then
  17 + pg_env
  18 + system("pg_dump #{config['database']} > #{db_file_name}")
  19 + end
  20 + end
  21 +
  22 + def restore_db
  23 + case config["adapter"]
  24 + when /^mysql/ then
  25 + system("mysql #{mysql_args} #{config['database']} < #{db_file_name}")
  26 + when "postgresql" then
  27 + pg_env
  28 + system("pg_restore #{config['database']} #{db_file_name}")
  29 + end
  30 + end
  31 +
  32 + protected
  33 +
  34 + def db_file_name
  35 + File.join(db_dir, 'database.sql')
  36 + end
  37 +
  38 + def mysql_args
  39 + args = {
  40 + 'host' => '--host',
  41 + 'port' => '--port',
  42 + 'socket' => '--socket',
  43 + 'username' => '--user',
  44 + 'encoding' => '--default-character-set',
  45 + 'password' => '--password'
  46 + }
  47 + args.map { |opt, arg| "#{arg}=#{config[opt]}" if config[opt] }.compact.join(' ')
  48 + end
  49 +
  50 + def pg_env
  51 + ENV['PGUSER'] = config["username"] if config["username"]
  52 + ENV['PGHOST'] = config["host"] if config["host"]
  53 + ENV['PGPORT'] = config["port"].to_s if config["port"]
  54 + ENV['PGPASSWORD'] = config["password"].to_s if config["password"]
  55 + end
  56 +end
... ...
lib/tasks/gitlab/backup.rake
... ... @@ -109,11 +109,6 @@ namespace :gitlab do
109 109 end
110 110 end
111 111  
112   - ################################################################################
113   - ################################# invoked tasks ################################
114   -
115   - ################################# REPOSITORIES #################################
116   -
117 112 namespace :repo do
118 113 task :create => :environment do
119 114 backup_path_repo = File.join(Gitlab.config.backup.path, "repositories")
... ... @@ -167,48 +162,18 @@ namespace :gitlab do
167 162 end
168 163 end
169 164  
170   - ###################################### DB ######################################
171   -
172 165 namespace :db do
173 166 task :create => :environment do
174   - backup_path_db = File.join(Gitlab.config.backup.path, "db")
175   - FileUtils.mkdir_p(backup_path_db) unless Dir.exists?(backup_path_db)
176   -
177   - puts "Dumping database tables ... ".blue
178   - ActiveRecord::Base.connection.tables.each do |tbl|
179   - print " * #{tbl.yellow} ... "
180   - count = 1
181   - safe_tablename = ActiveRecord::Base.connection.quote_table_name(tbl)
182   - File.open(File.join(backup_path_db, tbl + ".yml"), "w+") do |file|
183   - ActiveRecord::Base.connection.select_all("SELECT * FROM #{safe_tablename}").each do |line|
184   - line.delete_if{|k,v| v.blank?}
185   - output = {tbl + '_' + count.to_s => line}
186   - file << output.to_yaml.gsub(/^---\n/,'') + "\n"
187   - count += 1
188   - end
189   - puts "done".green
190   - end
191   - end
  167 + puts "Dumping database ... ".blue
  168 + Backup.new.backup_db
  169 + puts "done".green
192 170 end
193 171  
194 172 task :restore => :environment do
195   - backup_path_db = File.join(Gitlab.config.backup.path, "db")
196   -
197   - puts "Restoring database tables (loading fixtures) ... "
198   - Rake::Task["db:reset"].invoke
199   -
200   - Dir.glob(File.join(backup_path_db, "*.yml") ).each do |dir|
201   - fixture_file = File.basename(dir, ".*" )
202   - print "#{fixture_file.yellow} ... "
203   - if File.size(dir) > 0
204   - ActiveRecord::Fixtures.create_fixtures(backup_path_db, fixture_file)
205   - puts "done".green
206   - else
207   - puts "skipping".yellow
208   - end
209   - end
  173 + puts "Restoring database ... ".blue
  174 + Backup.new.restore_db
  175 + puts "done".green
210 176 end
211 177 end
212   -
213 178 end # namespace end: backup
214 179 end # namespace end: gitlab
... ...