Class: ICFS::Utils::Backup

Inherits:
Object
  • Object
show all
Defined in:
lib/icfs/utils/backup.rb

Overview

Backup and restore utilities

Instance Method Summary collapse

Constructor Details

#initialize(cache, store, log) ⇒ Backup

Instance

Parameters:

  • cache (Cache)

    The live cache

  • store (Store)

    The live store

  • log (Logger)

    Where to log



33
34
35
36
37
# File 'lib/icfs/utils/backup.rb', line 33

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

Parameters:

  • cid (String)

    The case ID

  • dest (Store)

    The destination store

  • lnum_max (Integer) (defaults to: 0)

    The highest log

  • lnum_min (Integer) (defaults to: 1)

    The lowest log



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
182
183
184
# File 'lib/icfs/utils/backup.rb', line 82

def copy(cid, dest, lnum_min=1, lnum_max=0)
  @log.info('ICFS copy: %s %d-%d' % [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', Items::ItemCurrent)
    lnum_max = cur['log']
  end

  if lnum_min > lnum_max
    raise ArgumentError, 'ICFS copy, log num min is larger than max'
  end

  # each log
  lnum = lnum_min
  while lnum <= lnum_max

    # copy the log
    log = _copy_item(dest,
      'log %d' % lnum,
      :log_read,
      :log_write,
      [cid, lnum],
      Items::ItemLog
    )
    if !log
      lnum += 1
      next
    end

    # entry
    if log['entry']
      enum = log['entry']['num']
      _copy_item(dest,
        'entry %d-%d' % [enum, lnum],
        :entry_read,
        :entry_write,
        [cid, enum, lnum]
      )
    end

    # index
    if log['index']
      xnum = log['index']['num']
      _copy_item(dest,
        'index %d-%d' % [xnum, lnum],
        :index_read,
        :index_write,
        [cid, xnum, lnum]
      )
    end

    # action
    if log['action']
      anum = log['action']['num']
      _copy_item(dest,
        'action %d-%d' % [anum, lnum],
        :action_read,
        :action_write,
        [cid, anum, lnum]
      )
    end

    # case
    if log['case']
      _copy_item(dest,
        'case %d' % 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' % [enum, lnum, fnum])

        # read
        fi = @store.file_read(cid, enum, lnum, fnum)
        if !fi
          @log.warn('ICFS copy: file %d-%d-%d missing' %
              [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

Parameters:

  • cid (String)

    The case ID

  • src (Store)

    Source store

  • lnum_max (Integer) (defaults to: 0)

    The highest log

  • lnum_min (Integer) (defaults to: 0)

    The lowest log



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
376
377
378
379
380
# File 'lib/icfs/utils/backup.rb', line 227

def restore(cid, src, lnum_min=0, lnum_max=0)
  @log.info('ICFS restore: %s %d-%d' % [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', 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'
    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' % lnum,
        :log_read,
        :log_write,
        [cid, lnum],
        [cid, lnum],
        Items::ItemLog
      )

      # no log - all done
      if !log
        break
      else
        llast = litem
      end

      # entry
      if log['entry']
        enum = log['entry']['num']
        _restore_item(src,
          'entry %d-%d' % [enum, lnum],
          :entry_read,
          :entry_write,
          [cid, enum, lnum],
          [cid, enum]
        )
        emax = enum if enum > emax
      end

      # index
      if log['index']
        xnum = log['index']['num']
        _restore_item(src,
          'index %d-%d' % [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' % [anum, lnum],
          :action_read,
          :action_write,
          [cid, anum, lnum],
          [cid, anum]
        )
        amax = anum if anum > amax
      end

      # case
      if log['case']
        _restore_item(src,
          'case %d' % 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' % [enum, lnum, fnum])

          # read
          fi = src.file_read(cid, enum, lnum, fnum)
          if !fi
            @log.warn('ICFS restore: file %d-%d-%d missing' %
              [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', Items::ItemCurrent)
    @cache.current_write(cid, nitem)

  ensure
    # release lock
    @cache.lock_release(cid)
  end

end