Commit 38d23c0e authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

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

parent b65903e0
require 'yaml'
class Backup
attr_reader :config, :db_dir
def initialize
@config = YAML.load_file(File.join(Rails.root,'config','database.yml'))[Rails.env]
@db_dir = File.join(Gitlab.config.backup.path, 'db')
FileUtils.mkdir_p(@db_dir) unless Dir.exists?(@db_dir)
end
def backup_db
case config["adapter"]
when /^mysql/ then
system("mysqldump #{mysql_args} #{config['database']} > #{db_file_name}")
when "postgresql" then
pg_env
system("pg_dump #{config['database']} > #{db_file_name}")
end
end
def restore_db
case config["adapter"]
when /^mysql/ then
system("mysql #{mysql_args} #{config['database']} < #{db_file_name}")
when "postgresql" then
pg_env
system("pg_restore #{config['database']} #{db_file_name}")
end
end
protected
def db_file_name
File.join(db_dir, 'database.sql')
end
def mysql_args
args = {
'host' => '--host',
'port' => '--port',
'socket' => '--socket',
'username' => '--user',
'encoding' => '--default-character-set',
'password' => '--password'
}
args.map { |opt, arg| "#{arg}=#{config[opt]}" if config[opt] }.compact.join(' ')
end
def pg_env
ENV['PGUSER'] = config["username"] if config["username"]
ENV['PGHOST'] = config["host"] if config["host"]
ENV['PGPORT'] = config["port"].to_s if config["port"]
ENV['PGPASSWORD'] = config["password"].to_s if config["password"]
end
end
......@@ -109,11 +109,6 @@ namespace :gitlab do
end
end
################################################################################
################################# invoked tasks ################################
################################# REPOSITORIES #################################
namespace :repo do
task :create => :environment do
backup_path_repo = File.join(Gitlab.config.backup.path, "repositories")
......@@ -167,48 +162,18 @@ namespace :gitlab do
end
end
###################################### DB ######################################
namespace :db do
task :create => :environment do
backup_path_db = File.join(Gitlab.config.backup.path, "db")
FileUtils.mkdir_p(backup_path_db) unless Dir.exists?(backup_path_db)
puts "Dumping database tables ... ".blue
ActiveRecord::Base.connection.tables.each do |tbl|
print " * #{tbl.yellow} ... "
count = 1
safe_tablename = ActiveRecord::Base.connection.quote_table_name(tbl)
File.open(File.join(backup_path_db, tbl + ".yml"), "w+") do |file|
ActiveRecord::Base.connection.select_all("SELECT * FROM #{safe_tablename}").each do |line|
line.delete_if{|k,v| v.blank?}
output = {tbl + '_' + count.to_s => line}
file << output.to_yaml.gsub(/^---\n/,'') + "\n"
count += 1
end
puts "Dumping database ... ".blue
Backup.new.backup_db
puts "done".green
end
end
end
task :restore => :environment do
backup_path_db = File.join(Gitlab.config.backup.path, "db")
puts "Restoring database tables (loading fixtures) ... "
Rake::Task["db:reset"].invoke
Dir.glob(File.join(backup_path_db, "*.yml") ).each do |dir|
fixture_file = File.basename(dir, ".*" )
print "#{fixture_file.yellow} ... "
if File.size(dir) > 0
ActiveRecord::Fixtures.create_fixtures(backup_path_db, fixture_file)
puts "Restoring database ... ".blue
Backup.new.restore_db
puts "done".green
else
puts "skipping".yellow
end
end
end
end
end # namespace end: backup
end # namespace end: gitlab
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment