Class: Backup::Targets::Database

Inherits:
Target
  • Object
show all
Extended by:
Gitlab::Utils::Override
Includes:
Helper
Defined in:
lib/backup/targets/database.rb

Constant Summary collapse

IGNORED_ERRORS =
[
  # Ignore warnings
  /WARNING:/,
  # Ignore the DROP errors; recent database dumps will use --if-exists with pg_dump
  /does not exist$/,
  # User may not have permissions to drop extensions or schemas
  /must be owner of/
].freeze
IGNORED_ERRORS_REGEXP =
Regexp.union(IGNORED_ERRORS).freeze

Instance Attribute Summary collapse

Attributes inherited from Target

#options, #progress

Instance Method Summary collapse

Methods included from Gitlab::Utils::Override

extended, extensions, included, method_added, override, prepended, queue_verification, verify!

Methods included from Helper

#compress_cmd, #decompress_cmd

Constructor Details

#initialize(progress, options:) ⇒ Database

Returns a new instance of Database.



23
24
25
26
27
28
29
# File 'lib/backup/targets/database.rb', line 23

def initialize(progress, options:)
  super(progress, options: options)

  @errors = []
  @force = options.force?
  @logger = Gitlab::BackupLogger.new(progress)
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



11
12
13
# File 'lib/backup/targets/database.rb', line 11

def errors
  @errors
end

#forceObject (readonly)

Returns the value of attribute force.



11
12
13
# File 'lib/backup/targets/database.rb', line 11

def force
  @force
end

#loggerObject (readonly)

Returns the value of attribute logger.



11
12
13
# File 'lib/backup/targets/database.rb', line 11

def logger
  @logger
end

Instance Method Details

#dump(destination_dir, _) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/backup/targets/database.rb', line 33

def dump(destination_dir, _)
  FileUtils.mkdir_p(destination_dir)

  each_database(destination_dir) do |backup_connection|
    pg_env = backup_connection.database_configuration.pg_env_variables
    active_record_config = backup_connection.database_configuration.activerecord_variables
    pg_database_name = active_record_config[:database]

    dump_file_name = file_name(destination_dir, backup_connection.connection_name)
    FileUtils.rm_f(dump_file_name)

    logger.info "Dumping PostgreSQL database #{pg_database_name} ... "

    schemas = []

    if Gitlab.config.backup.pg_schema
      schemas << Gitlab.config.backup.pg_schema
      schemas.push(*Gitlab::Database::EXTRA_SCHEMAS.map(&:to_s))
    end

    pg_dump = ::Gitlab::Backup::Cli::Utils::PgDump.new(
      database_name: pg_database_name,
      snapshot_id: backup_connection.snapshot_id,
      schemas: schemas,
      env: pg_env)

    success = Backup::Dump::Postgres.new.dump(dump_file_name, pg_dump)

    backup_connection.release_snapshot! if backup_connection.snapshot_id

    raise DatabaseBackupError.new(active_record_config, dump_file_name) unless success

    report_success(success)
    logger.flush
  end
ensure
  if multiple_databases?
    ::Gitlab::Database::EachDatabase.each_connection(
      only: base_models_for_backup.keys, include_shared: false
    ) do |_, database_connection_name|
      backup_connection = Backup::DatabaseConnection.new(database_connection_name)
      backup_connection.restore_timeouts!
    rescue ActiveRecord::ConnectionNotEstablished
      raise Backup::DatabaseBackupError.new(
        backup_connection.database_configuration.activerecord_variables,
        file_name(destination_dir, database_connection_name)
      )
    end
  end
end

#restore(destination_dir, _) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/backup/targets/database.rb', line 86

def restore(destination_dir, _)
  @errors = []

  base_models_for_backup.each do |database_name, _|
    backup_connection = Backup::DatabaseConnection.new(database_name)

    config = backup_connection.database_configuration.activerecord_variables

    db_file_name = file_name(destination_dir, database_name)
    database = config[:database]

    unless File.exist?(db_file_name)
      raise(Backup::Error, "Source database file does not exist #{db_file_name}") if main_database?(database_name)

      logger.info "Source backup for the database #{database_name} doesn't exist. Skipping the task"
      return false
    end

    unless force
      logger.info 'Removing all tables. Press `Ctrl-C` within 5 seconds to abort'
      sleep(5)
    end

    # Drop all tables Load the schema to ensure we don't have any newer tables
    # hanging out from a failed upgrade
    drop_tables(database_name)

    tracked_errors = []
    pg_env = backup_connection.database_configuration.pg_env_variables
    success = with_transient_pg_env(pg_env) do
      decompress_rd, decompress_wr = IO.pipe
      decompress_pid = spawn(decompress_cmd, out: decompress_wr, in: db_file_name)
      decompress_wr.close

      status, tracked_errors =
        case config[:adapter]
        when "postgresql" then
          logger.info "Restoring PostgreSQL database #{database} ... "
          execute_and_track_errors(pg_restore_cmd(database), decompress_rd)
        end
      decompress_rd.close

      Process.waitpid(decompress_pid)
      $?.success? && status.success?
    end

    unless tracked_errors.empty?
      logger.error "------ BEGIN ERRORS -----\n"
      logger.error tracked_errors.join
      logger.error "------ END ERRORS -------\n"

      @errors += tracked_errors
    end

    report_success(success)
    raise Backup::Error, 'Restore failed' unless success
  end
end