Class: SmarbsBackupDir

Inherits:
Directory show all
Defined in:
lib/helpers.rb

Overview

Specifies a directory that contains smarbs backups.

Instance Attribute Summary

Attributes inherited from Directory

#dir

Instance Method Summary collapse

Methods inherited from Directory

#==, #files, #free, prepare, #space, #to_s, writable?

Constructor Details

#initialize(directory, ignore_leftovers = false) ⇒ SmarbsBackupDir

Creates or reads out a SmarbsBackupDir, testing if it has valid contents. Can raise a NoConfigException, or a RuntimeError



327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/helpers.rb', line 327

def initialize(directory, ignore_leftovers=false)
  super(directory, :writable)

  return if files.size == 0 # Return if the directory is empty.
  list = dirlist
  numbers = list.collect {|x| x[0]}
  raise "Two directories with same 'number' detected in " + @dir + "!" if numbers.uniq != numbers
  raise "Directory with negative 'number' detected in #{@dir}!" if numbers[0] < 0
  if (not ignore_leftovers) && directory_left
    raise(NoConfigException, "\nAborted backup directory '#{directory_left}' left in #{@dir}\nMove or delete it to continue!")
  end
end

Instance Method Details

#delete(logger, minbackuplevels, oldestfirst = false) ⇒ Object

Deletes the directory differing most from the “1, 2, 4, 8, 16, …” scheme. If no such is found, deletes the last of all the directories. Also logs what is happening.

Raises an error if the next deletion would go below the ‘minbackuplevels’.



379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# File 'lib/helpers.rb', line 379

def delete(logger, minbackuplevels, oldestfirst = false)
  list=dirlist
  deleted=false

  unless list.size > minbackuplevels or minbackuplevels <= 1
    raise "Not enough space available, next deletion would go below your minbackuplevels of #{minbackuplevels}!"
  end

  unless list.size > 1
    raise "Not enough space available to do an incremental backup!"
  end

  if oldestfirst then
    a = dirlist[-1][1]
    b = dirlist[-1][0]
  else
    for i in 0...(dirlist.size-1)
      if (dirlist[i+1][0]-(2**i)).abs < (dirlist[i][0]-(2**i)).abs  && dirlist[i][0] != 0 #Smarbs CORE, the secret formula :)
        deleted=true
        a = dirlist[i][1]
        b = dirlist[i][0]
        break
      end
    end

    if not deleted
      a = dirlist[-1][1]
      b = dirlist[-1][0]
    end
  end
  logger.p("...removing backup." + b.to_s + "...", 1)
  FileUtils.remove_dir(self.to_s + a + "-backup." + b.to_s)
  logger.p(@dir.to_s + a + "-backup." + b.to_s + " removed!", 2, true)
end

#directory_leftObject

Returns the name of the directory (like 12-34-56-backup.0) when a ‘…backup.0’ directory is left. Otherwise returns false.



344
345
346
347
348
349
350
351
352
# File 'lib/helpers.rb', line 344

def directory_left
  list = dirlist

  if not list.empty? and list[0][0] == 0
    return (list[0][1]).to_s+"-backup.0"
  else
    return false
  end
end

#dirlistObject

Returns a sorted array of arrays of the splitted directories within the smarbsbackupdir of the form [7, 12-34-56] (from “12-34-56-backup.7”).

Raises an error, if some directories are “malformed”, respectively not in the form <date>-backup.<positive_number>.



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/helpers.rb', line 358

def dirlist
  list=files
  for i in 0...list.size
    raise "File '#{list[i]}' left in the destination directory #{@dir}!" if not File.directory?(@dir+"/"+list[i]) # No files are allowed.  

    tmp = list[i].split("-backup.")
    raise @dir + list[i] + " has wrong format!" if tmp.size != 2
    raise @dir + list[i] + " has wrong format in number!" if tmp[1].to_i.to_s != tmp[1]

    list[i]=[tmp[1].to_i, tmp[0]] # Converts "12-34-56-backup.7" to an Array entry [7, 12-34-56].
  end
  list.sort!
  list.compact!
  return list
end

#incrementObject

Increments all the directory numbers by 1. So for example “12-34-56-backup.7” becomes “12-34-56-backup.8”.



426
427
428
429
430
431
432
# File 'lib/helpers.rb', line 426

def increment
  dirlist.reverse.each do |element|
    original=@dir.to_s+element[1].to_s+"-backup."+element[0].to_s
    incremented=@dir.to_s+element[1].to_s+"-backup."+(element[0]+1).to_s
    FileUtils.move(original, incremented)
  end
end

#remove_except_oneObject

Removes all backups directories except the last one (the one with the smallest ‘number’).

Usually, this is “*-backup.0”.



418
419
420
421
422
423
# File 'lib/helpers.rb', line 418

def remove_except_one
  list = dirlist
  for i in 1...list.size do
    FileUtils.remove_dir "#{@dir}/#{list[i][1]}-backup.#{list[i][0]}"
  end    
end