Reduce disk IO during SQL backup
By using light gzip compression we can save a lot of disk IO during the backup.
This commit is contained in:
parent
d3734fbd89
commit
90ddf140b9
2 changed files with 27 additions and 32 deletions
|
@ -2,26 +2,27 @@ require 'yaml'
|
||||||
|
|
||||||
module Backup
|
module Backup
|
||||||
class Database
|
class Database
|
||||||
attr_reader :config, :db_dir
|
attr_reader :config, :db_file_name
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@config = YAML.load_file(File.join(Rails.root,'config','database.yml'))[Rails.env]
|
@config = YAML.load_file(File.join(Rails.root,'config','database.yml'))[Rails.env]
|
||||||
@db_dir = File.join(Gitlab.config.backup.path, 'db')
|
@db_file_name = File.join(Gitlab.config.backup.path, 'database.sql.gz')
|
||||||
end
|
end
|
||||||
|
|
||||||
def dump
|
def dump
|
||||||
FileUtils.rm_rf(@db_dir)
|
FileUtils.rm_f(db_file_name)
|
||||||
# Ensure the parent dir of @db_dir exists
|
compress_rd, compress_wr = IO.pipe
|
||||||
FileUtils.mkdir_p(Gitlab.config.backup.path)
|
compress_pid = spawn(*%W(gzip -1 -c), in: compress_rd, out: [db_file_name, 'w', 0600])
|
||||||
# Fail if somebody raced to create @db_dir before us
|
compress_rd.close
|
||||||
FileUtils.mkdir(@db_dir, mode: 0700)
|
|
||||||
|
|
||||||
success = case config["adapter"]
|
FileUtils.mkdir_p(Gitlab.config.backup.path)
|
||||||
|
|
||||||
|
dump_pid = case config["adapter"]
|
||||||
when /^mysql/ then
|
when /^mysql/ then
|
||||||
$progress.print "Dumping MySQL database #{config['database']} ... "
|
$progress.print "Dumping MySQL database #{config['database']} ... "
|
||||||
# Workaround warnings from MySQL 5.6 about passwords on cmd line
|
# Workaround warnings from MySQL 5.6 about passwords on cmd line
|
||||||
ENV['MYSQL_PWD'] = config["password"].to_s if config["password"]
|
ENV['MYSQL_PWD'] = config["password"].to_s if config["password"]
|
||||||
system('mysqldump', *mysql_args, config['database'], out: db_file_name)
|
spawn('mysqldump', *mysql_args, config['database'], out: compress_wr)
|
||||||
when "postgresql" then
|
when "postgresql" then
|
||||||
$progress.print "Dumping PostgreSQL database #{config['database']} ... "
|
$progress.print "Dumping PostgreSQL database #{config['database']} ... "
|
||||||
pg_env
|
pg_env
|
||||||
|
@ -30,48 +31,42 @@ module Backup
|
||||||
pgsql_args << "-n"
|
pgsql_args << "-n"
|
||||||
pgsql_args << Gitlab.config.backup.pg_schema
|
pgsql_args << Gitlab.config.backup.pg_schema
|
||||||
end
|
end
|
||||||
system('pg_dump', *pgsql_args, config['database'], out: db_file_name)
|
spawn('pg_dump', *pgsql_args, config['database'], out: compress_wr)
|
||||||
end
|
end
|
||||||
|
compress_wr.close
|
||||||
|
|
||||||
|
success = [compress_pid, dump_pid].all? { |pid| Process.waitpid(pid); $?.success? }
|
||||||
|
|
||||||
report_success(success)
|
report_success(success)
|
||||||
abort 'Backup failed' unless success
|
abort 'Backup failed' unless success
|
||||||
|
|
||||||
$progress.print 'Compressing database ... '
|
|
||||||
success = system('gzip', db_file_name)
|
|
||||||
report_success(success)
|
|
||||||
abort 'Backup failed: compress error' unless success
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def restore
|
def restore
|
||||||
$progress.print 'Decompressing database ... '
|
decompress_rd, decompress_wr = IO.pipe
|
||||||
success = system('gzip', '-d', db_file_name_gz)
|
decompress_pid = spawn(*%W(gzip -cd), out: decompress_wr, in: db_file_name)
|
||||||
report_success(success)
|
decompress_wr.close
|
||||||
abort 'Restore failed: decompress error' unless success
|
|
||||||
|
|
||||||
success = case config["adapter"]
|
restore_pid = case config["adapter"]
|
||||||
when /^mysql/ then
|
when /^mysql/ then
|
||||||
$progress.print "Restoring MySQL database #{config['database']} ... "
|
$progress.print "Restoring MySQL database #{config['database']} ... "
|
||||||
# Workaround warnings from MySQL 5.6 about passwords on cmd line
|
# Workaround warnings from MySQL 5.6 about passwords on cmd line
|
||||||
ENV['MYSQL_PWD'] = config["password"].to_s if config["password"]
|
ENV['MYSQL_PWD'] = config["password"].to_s if config["password"]
|
||||||
system('mysql', *mysql_args, config['database'], in: db_file_name)
|
spawn('mysql', *mysql_args, config['database'], in: decompress_rd)
|
||||||
when "postgresql" then
|
when "postgresql" then
|
||||||
$progress.print "Restoring PostgreSQL database #{config['database']} ... "
|
$progress.print "Restoring PostgreSQL database #{config['database']} ... "
|
||||||
pg_env
|
pg_env
|
||||||
system('psql', config['database'], '-f', db_file_name)
|
spawn('psql', config['database'], in: decompress_rd)
|
||||||
end
|
end
|
||||||
|
decompress_rd.close
|
||||||
|
|
||||||
|
success = [decompress_pid, restore_pid].all? { |pid| Process.waitpid(pid); $?.success? }
|
||||||
|
|
||||||
report_success(success)
|
report_success(success)
|
||||||
abort 'Restore failed' unless success
|
abort 'Restore failed' unless success
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
def db_file_name
|
|
||||||
File.join(db_dir, 'database.sql')
|
|
||||||
end
|
|
||||||
|
|
||||||
def db_file_name_gz
|
|
||||||
File.join(db_dir, 'database.sql.gz')
|
|
||||||
end
|
|
||||||
|
|
||||||
def mysql_args
|
def mysql_args
|
||||||
args = {
|
args = {
|
||||||
'host' => '--host',
|
'host' => '--host',
|
||||||
|
|
|
@ -150,11 +150,11 @@ module Backup
|
||||||
private
|
private
|
||||||
|
|
||||||
def backup_contents
|
def backup_contents
|
||||||
folders_to_backup + ["uploads.tar.gz", "builds.tar.gz", "backup_information.yml"]
|
folders_to_backup + ["database.sql.gz", "uploads.tar.gz", "builds.tar.gz", "backup_information.yml"]
|
||||||
end
|
end
|
||||||
|
|
||||||
def folders_to_backup
|
def folders_to_backup
|
||||||
folders = %w{repositories db}
|
folders = %w{repositories}
|
||||||
|
|
||||||
if ENV["SKIP"]
|
if ENV["SKIP"]
|
||||||
return folders.reject{ |folder| ENV["SKIP"].include?(folder) }
|
return folders.reject{ |folder| ENV["SKIP"].include?(folder) }
|
||||||
|
|
Loading…
Reference in a new issue