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}"
(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
(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
|