BigW Consortium Gitlab

database.rb 3.76 KB
Newer Older
1 2 3 4
require 'yaml'

module Backup
  class Database
5
    attr_reader :config, :db_file_name
6 7

    def initialize
8
      @config = YAML.load_file(File.join(Rails.root, 'config', 'database.yml'))[Rails.env]
9
      @db_file_name = File.join(Gitlab.config.backup.path, 'db', 'database.sql.gz')
10 11 12
    end

    def dump
13
      FileUtils.mkdir_p(File.dirname(db_file_name))
14 15
      FileUtils.rm_f(db_file_name)
      compress_rd, compress_wr = IO.pipe
16
      compress_pid = spawn(*%w(gzip -1 -c), in: compress_rd, out: [db_file_name, 'w', 0600])
17 18
      compress_rd.close

19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
      dump_pid =
        case config["adapter"]
        when /^mysql/ then
          $progress.print "Dumping MySQL database #{config['database']} ... "
          # Workaround warnings from MySQL 5.6 about passwords on cmd line
          ENV['MYSQL_PWD'] = config["password"].to_s if config["password"]
          spawn('mysqldump', *mysql_args, config['database'], out: compress_wr)
        when "postgresql" then
          $progress.print "Dumping PostgreSQL database #{config['database']} ... "
          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
          spawn('pg_dump', *pgsql_args, config['database'], out: compress_wr)
35
        end
36 37
      compress_wr.close

38 39 40 41
      success = [compress_pid, dump_pid].all? do |pid|
        Process.waitpid(pid)
        $?.success?
      end
42 43

      report_success(success)
44
      abort 'Backup failed' unless success
45 46 47
    end

    def restore
48
      decompress_rd, decompress_wr = IO.pipe
49
      decompress_pid = spawn(*%w(gzip -cd), out: decompress_wr, in: db_file_name)
50
      decompress_wr.close
51

52 53 54 55 56 57 58 59 60 61 62 63
      restore_pid =
        case config["adapter"]
        when /^mysql/ then
          $progress.print "Restoring MySQL database #{config['database']} ... "
          # Workaround warnings from MySQL 5.6 about passwords on cmd line
          ENV['MYSQL_PWD'] = config["password"].to_s if config["password"]
          spawn('mysql', *mysql_args, config['database'], in: decompress_rd)
        when "postgresql" then
          $progress.print "Restoring PostgreSQL database #{config['database']} ... "
          pg_env
          spawn('psql', config['database'], in: decompress_rd)
        end
64 65
      decompress_rd.close

66 67 68 69
      success = [decompress_pid, restore_pid].all? do |pid|
        Process.waitpid(pid)
        $?.success?
      end
70

71
      report_success(success)
72
      abort 'Restore failed' unless success
73 74 75 76 77 78 79 80 81 82
    end

    protected

    def mysql_args
      args = {
        'host'      => '--host',
        'port'      => '--port',
        'socket'    => '--socket',
        'username'  => '--user',
83 84 85 86 87 88 89
        'encoding'  => '--default-character-set',
        # SSL
        'sslkey'    => '--ssl-key',
        'sslcert'   => '--ssl-cert',
        'sslca'     => '--ssl-ca',
        'sslcapath' => '--ssl-capath',
        'sslcipher' => '--ssl-cipher'
90
      }
91
      args.map { |opt, arg| "#{arg}=#{config[opt]}" if config[opt] }.compact
92 93 94
    end

    def pg_env
95 96 97 98 99 100 101 102 103 104 105 106 107 108
      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] }
109
    end
110 111 112

    def report_success(success)
      if success
113
        $progress.puts '[DONE]'.color(:green)
114
      else
115
        $progress.puts '[FAILED]'.color(:red)
116 117
      end
    end
118 119
  end
end