Class: ICFS::Utils::Backup
- Inherits:
-
Object
- Object
- ICFS::Utils::Backup
- Defined in:
- lib/icfs/utils/backup.rb
Overview
Backup and restore utilities
Instance Method Summary collapse
-
#copy(cid, dest, lnum_min = 1, lnum_max = 0) ⇒ Object
Transfer a case to another store.
-
#initialize(cache, store, log) ⇒ Backup
constructor
Instance.
-
#restore(cid, src, lnum_min = 0, lnum_max = 0) ⇒ Object
Restore a backup into a case.
Constructor Details
#initialize(cache, store, log) ⇒ Backup
Instance
32 33 34 35 36 |
# File 'lib/icfs/utils/backup.rb', line 32 def initialize(cache, store, log) @cache = cache @store = store @log = log end |
Instance Method Details
#copy(cid, dest, lnum_min = 1, lnum_max = 0) ⇒ Object
Transfer a case to another store
81 82 83 84 85 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 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 |
# File 'lib/icfs/utils/backup.rb', line 81 def copy(cid, dest, lnum_min=1, lnum_max=0) @log.info('ICFS copy: %s %d-%d'.freeze % [cid, lnum_min, lnum_max]) # if no max specified, pull from current if lnum_max == 0 json = @cache.current_read(cid) cur = Items.parse(json, 'current'.freeze, Items::ItemCurrent) lnum_max = cur['log'] end if lnum_min > lnum_max raise ArgumentError, 'ICFS copy, log num min is larger than max'.freeze end # each log lnum = lnum_min while lnum <= lnum_max # copy the log log = _copy_item(dest, 'log %d'.freeze % lnum, :log_read, :log_write, [cid, lnum], Items::ItemLog ) if !log lnum += 1 next end # entry enum = log['entry']['num'] _copy_item(dest, 'entry %d-%d'.freeze % [enum, lnum], :entry_read, :entry_write, [cid, enum, lnum] ) # index if log['index'] xnum = log['index']['num'] _copy_item(dest, 'index %d-%d'.freeze % [xnum, lnum], :index_read, :index_write, [cid, xnum, lnum] ) end # action if log['action'] anum = log['action']['num'] _copy_item(dest, 'action %d-%d'.freeze % [anum, lnum], :action_read, :action_write, [cid, anum, lnum] ) end # case if log['case_hash'] _copy_item(dest, 'case %d'.freeze % lnum, :case_read, :case_write, [cid, lnum] ) end # files if log['files_hash'] log['files_hash'].each_index do |fraw| fnum = fraw + 1 @log.debug('ICFS copy: file %d-%d-%d'.freeze % [enum, lnum, fnum]) # read fi = @store.file_read(cid, enum, lnum, fnum) if !fi @log.warn('ICFS copy: file %d-%d-%d missing'.freeze % [enum, lnum, fnum]) next end # copy tmp = dest.tempfile IO.copy_stream(fi, tmp) @store.close(fi) # write dest.file_write(cid, enum, lnum, fnum, tmp) end end lnum += 1 end end |
#restore(cid, src, lnum_min = 0, lnum_max = 0) ⇒ Object
Restore a backup into a case
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 |
# File 'lib/icfs/utils/backup.rb', line 224 def restore(cid, src, lnum_min=0, lnum_max=0) @log.info('ICFS restore: %s %d-%d'.freeze % [cid, lnum_min, lnum_max]) # take lock @cache.lock_take(cid) begin # read current json = @cache.current_read(cid) if json cur = Items.parse(json, 'current'.freeze, Items::ItemCurrent) else cur = { 'icfs' => 1, 'caseid' => cid, 'log' => 1, 'entry' => 1, 'action' => 0, 'index' => 0 } end # if no min specified, pull from current or default to 1 lnum_min = cur['log'] if lnum_min == 0 # sanity check min & max if (lnum_min > lnum_max) && (lnum_max != 0) raise ArgumentError: 'ICFS restore, log min is larger than max'.freeze end # max entry, action, index emax = cur['entry'] amax = cur['action'] imax = cur['index'] # each log lnum = lnum_min llast = nil while lnum != lnum_max # copy the log log, litem = _restore_item(src, 'log %d'.freeze % lnum, :log_read, :log_write, [cid, lnum], [cid, lnum], Items::ItemLog ) # no log - all done if !log break else llast = litem end # entry enum = log['entry']['num'] _restore_item(src, 'entry %d-%d'.freeze % [enum, lnum], :entry_read, :entry_write, [cid, enum, lnum], [cid, enum] ) emax = enum if enum > emax # index if log['index'] xnum = log['index']['num'] _restore_item(src, 'index %d-%d'.freeze % [xnum, lnum], :index_read, :index_write, [cid, xnum, lnum], [cid, xnum] ) imax = xnum if xnum > imax end # action if log['action'] anum = log['action']['num'] _restore_item(src, 'action %d-%d'.freeze % [anum, lnum], :action_read, :action_write, [cid, anum, lnum], [cid, anum] ) amax = anum if anum > amax end # case if log['case_hash'] _restore_item(src, 'case %d'.freeze % lnum, :case_read, :case_write, [cid, lnum], [cid] ) end # files if log['files_hash'] log['files_hash'].each_index do |fraw| fnum = fraw + 1 @log.debug('ICFS restore: file %d-%d-%d'.freeze % [enum, lnum, fnum]) # read fi = src.file_read(cid, enum, lnum, fnum) if !fi @log.warn('ICFS restore: file %d-%d-%d missing'.freeze % [enum, lnum, fnum]) next end # copy tmp = @store.tempfile IO.copy_stream(fi, tmp) src.close(fi) # write @store.file_write(cid, enum, lnum, fnum, tmp) end end lnum += 1 end # write current cur = { 'icfs' => 1, 'caseid' => cid, 'log' => lnum-1, 'entry' => emax, 'action' => amax, 'index' => imax, 'hash' => ICFS.hash(llast) } nitem = Items.generate(cur, 'current'.freeze, Items::ItemCurrent) @cache.current_write(cid, nitem) ensure # release lock @cache.lock_release(cid) end end |