Commit 90ddf140 authored by Jacob Vosmaer's avatar Jacob Vosmaer

Reduce disk IO during SQL backup

By using light gzip compression we can save a lot of disk IO during
the backup.
parent d3734fbd
...@@ -2,26 +2,27 @@ require 'yaml' ...@@ -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
compress_pid = spawn(*%W(gzip -1 -c), in: compress_rd, out: [db_file_name, 'w', 0600])
compress_rd.close
FileUtils.mkdir_p(Gitlab.config.backup.path) FileUtils.mkdir_p(Gitlab.config.backup.path)
# Fail if somebody raced to create @db_dir before us
FileUtils.mkdir(@db_dir, mode: 0700)
success = case config["adapter"] 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 ...@@ -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
report_success(success) compress_wr.close
abort 'Backup failed' unless success
success = [compress_pid, dump_pid].all? { |pid| Process.waitpid(pid); $?.success? }
$progress.print 'Compressing database ... '
success = system('gzip', db_file_name)
report_success(success) report_success(success)
abort 'Backup failed: compress error' unless success abort 'Backup failed' 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 ...@@ -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) }
......
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