BigW Consortium Gitlab

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

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

    def initialize
      @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 16 17 18 19
      FileUtils.rm_f(db_file_name)
      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

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

      success = [compress_pid, dump_pid].all? { |pid| Process.waitpid(pid); $?.success? }
38 39

      report_success(success)
40
      abort 'Backup failed' unless success
41 42 43
    end

    def restore
44 45 46
      decompress_rd, decompress_wr = IO.pipe
      decompress_pid = spawn(*%W(gzip -cd), out: decompress_wr, in: db_file_name)
      decompress_wr.close
47

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

      success = [decompress_pid, restore_pid].all? { |pid| Process.waitpid(pid); $?.success? }

63
      report_success(success)
64
      abort 'Restore failed' unless success
65 66 67 68 69 70 71 72 73 74
    end

    protected

    def mysql_args
      args = {
        'host'      => '--host',
        'port'      => '--port',
        'socket'    => '--socket',
        'username'  => '--user',
75
        'encoding'  => '--default-character-set'
76
      }
77
      args.map { |opt, arg| "#{arg}=#{config[opt]}" if config[opt] }.compact
78 79 80 81 82 83 84 85
    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
86 87 88

    def report_success(success)
      if success
89
        $progress.puts '[DONE]'.color(:green)
90
      else
91
        $progress.puts '[FAILED]'.color(:red)
92 93
      end
    end
94 95
  end
end