Class: PXCBackup::Backupper

Inherits:
Object
  • Object
show all
Defined in:
lib/pxcbackup/backupper.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Backupper

Returns a new instance of Backupper.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/pxcbackup/backupper.rb', line 14

def initialize(options)
  @verbose = options[:verbose] || false
  @threads = options[:threads] || 1
  @memory = options[:memory] || '100M'
  @throttle = options[:throttle] || nil
  @encrypt = options[:encrypt] || nil
  @encrypt_key = options[:encrypt_key] || nil

  @which = PathResolver.new(options)

  local_repo_path = options[:backup_dir]
  @local_repo = local_repo_path ? Repo.new(local_repo_path, options) : nil

  remote_repo_path = options[:remote]
  @remote_repo = remote_repo_path && !options[:local] ? RemoteRepo.new(remote_repo_path, options) : nil

  @mysql = MySQL.new(options)
end

Instance Method Details

#list_backupsObject



231
232
233
234
235
236
237
238
239
# File 'lib/pxcbackup/backupper.rb', line 231

def list_backups
  all_backups.each do |backup|
    if @verbose
      puts "#{backup} - #{backup.type.to_s[0..3]} (#{backup.remote? ? 'remote' : 'local'})"
    else
      puts backup
    end
  end
end

#make_backup(options = {}) ⇒ 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
83
84
85
86
87
88
89
90
# File 'lib/pxcbackup/backupper.rb', line 33

def make_backup(options = {})
  type = options[:type] || :full
  stream = options[:stream] || :xbstream
  compress = options[:compress] || false
  compact = options[:compact] || false
  desync_wait = options[:desync_wait] || 60
  retention = options[:retention] || 100

  raise 'cannot find backup dir' unless @local_repo && File.directory?(@local_repo.path)
  raise 'cannot enable encryption without encryption key' if @encrypt && !@encrypt_key

  arguments = [
    @mysql.auth,
    '--no-timestamp',
    "--extra-lsndir=#{@local_repo.path}",
    "--stream=#{stream.to_s}",
    '--galera-info',
  ]

  if compress
    arguments << '--compress'
  end

  if compact
    arguments << '--compact'
  end

  if @encrypt
    arguments << "--encrypt=#{@encrypt.shellescape}"
    arguments << "--encrypt-key=#{@encrypt_key.shellescape}"
  end

  filename = "#{Time.now.to_i}"
  if type == :incremental
    last_info = read_backup_info(File.join(@local_repo.path, 'xtrabackup_checkpoints'))
    arguments << '--incremental'
    arguments << "--incremental-lsn=#{last_info[:to_lsn]}"
    filename << "_incr"
  else
    filename << '_full'
  end
  filename << ".#{stream.to_s}"
  filename << '.xbcrypt' if @encrypt

  desync_enable(desync_wait)

  Dir.mktmpdir('pxcbackup-') do |dir|
    arguments << dir.shellescape
    log_action "Creating backup #{filename}" do
      innobackupex(arguments, File.join(@local_repo.path, filename))
    end
  end

  desync_disable
  rotate(retention)

  @remote_repo.sync(@local_repo) if @remote_repo
end

#restore_backup(time, skip_confirmation = false) ⇒ Object



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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/pxcbackup/backupper.rb', line 92

def restore_backup(time, skip_confirmation = false)
  incremental_backups = []
  all_backups.reverse_each do |backup|
    incremental_backups.unshift(backup) if backup.time <= time
    break if incremental_backups.any? && backup.full?
  end
  raise "cannot find any backup before #{time}" if incremental_backups.empty?
  raise "cannot find a full backup before #{time}" unless incremental_backups.first.full?
  restore_time = incremental_backups.last.time

  full_backup = incremental_backups.shift

  log "[1/#{incremental_backups.size + 1}] Processing #{full_backup.type.to_s} backup from #{full_backup}"
  with_extracted_backup(full_backup) do |full_backup_path, full_backup_info|
    raise 'unexpected backup type' unless full_backup_info[:backup_type] == full_backup.type
    raise 'unexpected start LSN' unless full_backup_info[:from_lsn] == 0

    compact = full_backup_info[:compact]

    if full_backup_info[:compress]
      log_action '  Decompressing' do
        innobackupex(['--decompress', full_backup_path.shellescape])
      end
    end

    if incremental_backups.any?
      log_action "  Preparing base backup (LSN #{full_backup_info[:to_lsn]})" do
        innobackupex(['--apply-log', '--redo-only', full_backup_path.shellescape])
      end

      current_lsn = full_backup_info[:to_lsn]

      index = 2
      incremental_backups.each do |incremental_backup|
        log "[#{index}/#{incremental_backups.size + 1}] Processing #{incremental_backup.type.to_s} backup from #{incremental_backup}"
        index += 1
        with_extracted_backup(incremental_backup) do |incremental_backup_path, incremental_backup_info|
          raise 'unexpected backup type' unless incremental_backup_info[:backup_type] == incremental_backup.type
          raise 'unexpected start LSN' unless incremental_backup_info[:from_lsn] == current_lsn

          compact ||= incremental_backup_info[:compact]

          if incremental_backup_info[:compress]
            log_action '  Decompressing' do
              innobackupex(['--decompress', incremental_backup_path.shellescape])
            end
          end

          log_action "  Applying increment (LSN #{incremental_backup_info[:from_lsn]} -> #{incremental_backup_info[:to_lsn]})" do
            innobackupex(['--apply-log', '--redo-only', full_backup_path.shellescape, "--incremental-dir=#{incremental_backup_path.shellescape}"])
          end

          current_lsn = incremental_backup_info[:to_lsn]
        end
      end
    end

    action = 'Final prepare'
    arguments = [
      '--apply-log',
    ]

    if compact
      action << ' + rebuild indexes'
      arguments << '--rebuild-indexes'
    end

    log_action "#{action}" do
      arguments << full_backup_path.shellescape
      innobackupex(arguments)
    end

    log_action 'Attempting to restore Galera info' do
      restore_galera_info(full_backup_path)
    end

    mysql_datadir = @mysql.datadir.chomp('/')
    mysql_datadir_old = mysql_datadir + '_YYYYMMDDhhmmss'

    unless skip_confirmation
      puts
      puts '    BACKUP IS NOW READY TO BE RESTORED'
      puts "    BACKUP TIMESTAMP: #{restore_time}"
      puts '    PLEASE CONFIRM THIS ACTION'
      puts
      puts '    This will:'
      puts '    - stop the MySQL server'
      puts "    - move the current datadir to #{mysql_datadir_old}"
      puts "    - restore the backup to #{mysql_datadir}"
      puts '    - start the MySQL server'
      puts
      puts '    Afterwards you will have to:'
      puts '    - confirm everything is working and synced correctly'
      puts '    - manually create a new full backup (to re-allow incremental backups)'
      puts
      puts '    If MySQL server cannot be started, this might be because this is the'
      puts '    only (remaining) Galera node. If so, manually bootstrap the cluster:'
      puts '    # service mysql bootstrap-pxc'
      puts
      print '    Please type "yes" to continue: '
      confirmation = STDIN.gets.chomp
      puts
      raise 'did not confirm restore' unless confirmation == 'yes'
    end

    log_action 'Stopping MySQL server' do
      system("#{@which.service.shellescape} mysql stop")
    end

    stat = File.stat(mysql_datadir)
    uid = stat.uid
    gid = stat.gid

    mysql_datadir_old = mysql_datadir + '_' + Time.now.strftime('%Y%m%d%H%M%S')
    log_action "Moving current datadir to #{mysql_datadir_old}" do
      File.rename(mysql_datadir, mysql_datadir_old)
    end

    log_action "Restoring backup to #{mysql_datadir}" do
      Dir.mkdir(mysql_datadir)
      innobackupex(['--move-back', full_backup_path.shellescape])
    end

    log_action "Chowning #{mysql_datadir}" do
      FileUtils.chown_R(uid, gid, mysql_datadir)
    end

    if @local_repo
      log_action "Removing last backup info" do
        File.delete(File.join(@local_repo.path, 'xtrabackup_checkpoints'))
      end
    end

    log_action 'Starting MySQL server' do
      system("#{@which.service.shellescape} mysql start")
    end
  end
end