2018-09-29 18:34:47 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-04-05 12:01:19 -04:00
|
|
|
require 'yaml'
|
|
|
|
|
|
|
|
module Backup
|
|
|
|
class Database
|
2019-03-07 03:38:14 -05:00
|
|
|
include Backup::Helper
|
2018-05-24 10:58:25 -04:00
|
|
|
attr_reader :progress
|
2015-10-06 09:06:15 -04:00
|
|
|
attr_reader :config, :db_file_name
|
2013-04-05 12:01:19 -04:00
|
|
|
|
2018-05-24 10:58:25 -04:00
|
|
|
def initialize(progress)
|
|
|
|
@progress = progress
|
2017-02-22 13:18:40 -05:00
|
|
|
@config = YAML.load_file(File.join(Rails.root, 'config', 'database.yml'))[Rails.env]
|
2015-10-06 09:21:15 -04:00
|
|
|
@db_file_name = File.join(Gitlab.config.backup.path, 'db', 'database.sql.gz')
|
2015-08-21 10:12:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def dump
|
2015-10-06 09:21:15 -04:00
|
|
|
FileUtils.mkdir_p(File.dirname(db_file_name))
|
2015-10-06 09:06:15 -04:00
|
|
|
FileUtils.rm_f(db_file_name)
|
|
|
|
compress_rd, compress_wr = IO.pipe
|
2019-03-07 03:38:14 -05:00
|
|
|
compress_pid = spawn(gzip_cmd, in: compress_rd, out: [db_file_name, 'w', 0600])
|
2015-10-06 09:06:15 -04:00
|
|
|
compress_rd.close
|
|
|
|
|
2017-02-22 13:18:40 -05:00
|
|
|
dump_pid =
|
|
|
|
case config["adapter"]
|
|
|
|
when "postgresql" then
|
2018-05-24 10:58:25 -04:00
|
|
|
progress.print "Dumping PostgreSQL database #{config['database']} ... "
|
2017-02-22 13:18:40 -05:00
|
|
|
pg_env
|
|
|
|
pgsql_args = ["--clean"] # Pass '--clean' to include 'DROP TABLE' statements in the DB dump.
|
|
|
|
if Gitlab.config.backup.pg_schema
|
|
|
|
pgsql_args << "-n"
|
|
|
|
pgsql_args << Gitlab.config.backup.pg_schema
|
|
|
|
end
|
2018-01-11 11:34:01 -05:00
|
|
|
|
2017-02-22 13:18:40 -05:00
|
|
|
spawn('pg_dump', *pgsql_args, config['database'], out: compress_wr)
|
2015-09-18 05:47:25 -04:00
|
|
|
end
|
2015-10-06 09:06:15 -04:00
|
|
|
compress_wr.close
|
|
|
|
|
2017-02-22 13:18:40 -05:00
|
|
|
success = [compress_pid, dump_pid].all? do |pid|
|
|
|
|
Process.waitpid(pid)
|
|
|
|
$?.success?
|
|
|
|
end
|
2015-06-23 09:45:24 -04:00
|
|
|
|
|
|
|
report_success(success)
|
2018-06-05 11:51:14 -04:00
|
|
|
raise Backup::Error, 'Backup failed' unless success
|
2013-04-05 12:01:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def restore
|
2015-10-06 09:06:15 -04:00
|
|
|
decompress_rd, decompress_wr = IO.pipe
|
2017-02-22 13:18:40 -05:00
|
|
|
decompress_pid = spawn(*%w(gzip -cd), out: decompress_wr, in: db_file_name)
|
2015-10-06 09:06:15 -04:00
|
|
|
decompress_wr.close
|
2015-06-23 09:45:24 -04:00
|
|
|
|
2017-02-22 13:18:40 -05:00
|
|
|
restore_pid =
|
|
|
|
case config["adapter"]
|
|
|
|
when "postgresql" then
|
2018-05-24 10:58:25 -04:00
|
|
|
progress.print "Restoring PostgreSQL database #{config['database']} ... "
|
2017-02-22 13:18:40 -05:00
|
|
|
pg_env
|
|
|
|
spawn('psql', config['database'], in: decompress_rd)
|
|
|
|
end
|
2015-10-06 09:06:15 -04:00
|
|
|
decompress_rd.close
|
|
|
|
|
2017-02-22 13:18:40 -05:00
|
|
|
success = [decompress_pid, restore_pid].all? do |pid|
|
|
|
|
Process.waitpid(pid)
|
|
|
|
$?.success?
|
|
|
|
end
|
2015-10-06 09:06:15 -04:00
|
|
|
|
2014-01-23 05:19:43 -05:00
|
|
|
report_success(success)
|
2018-06-05 11:51:14 -04:00
|
|
|
abort Backup::Error, 'Restore failed' unless success
|
2013-04-05 12:01:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def pg_env
|
2017-03-05 16:08:15 -05:00
|
|
|
args = {
|
|
|
|
'username' => 'PGUSER',
|
|
|
|
'host' => 'PGHOST',
|
|
|
|
'port' => 'PGPORT',
|
|
|
|
'password' => 'PGPASSWORD',
|
|
|
|
# SSL
|
|
|
|
'sslmode' => 'PGSSLMODE',
|
|
|
|
'sslkey' => 'PGSSLKEY',
|
|
|
|
'sslcert' => 'PGSSLCERT',
|
|
|
|
'sslrootcert' => 'PGSSLROOTCERT',
|
|
|
|
'sslcrl' => 'PGSSLCRL',
|
|
|
|
'sslcompression' => 'PGSSLCOMPRESSION'
|
|
|
|
}
|
|
|
|
args.each { |opt, arg| ENV[arg] = config[opt].to_s if config[opt] }
|
2013-04-05 12:01:19 -04:00
|
|
|
end
|
2014-01-23 05:19:43 -05:00
|
|
|
|
|
|
|
def report_success(success)
|
|
|
|
if success
|
2018-05-24 10:58:25 -04:00
|
|
|
progress.puts '[DONE]'.color(:green)
|
2014-01-23 05:19:43 -05:00
|
|
|
else
|
2018-05-24 10:58:25 -04:00
|
|
|
progress.puts '[FAILED]'.color(:red)
|
2014-01-23 05:19:43 -05:00
|
|
|
end
|
|
|
|
end
|
2013-04-05 12:01:19 -04:00
|
|
|
end
|
|
|
|
end
|