class NixAdm::Backup

Base backup class. Provides centralized logging facility and job start/stop templates.

Attributes

host[R]

Public Class Methods

new(host, options = {}) click to toggle source
Calls superclass method NixAdm::Command::new
# File src/lib/nixadm/backup.rb, line 12
def initialize(host, options = {})
  super(host, options)

  if not $backup_dir.nil?
    @logfile = File.open("#{$backup_dir}/backup.log", 'w')
  end
end

Public Instance Methods

endBackup() click to toggle source
# File src/lib/nixadm/backup.rb, line 30
def endBackup()

end
run() { || ... } click to toggle source
# File src/lib/nixadm/backup.rb, line 20
def run()
  startBackup()
  yield
  endBackup()
end
startBackup() click to toggle source
# File src/lib/nixadm/backup.rb, line 26
def startBackup()

end

Protected Instance Methods

backup_dir(dir) click to toggle source
# File src/lib/nixadm/backup.rb, line 116
def backup_dir(dir)
  return "#{$backup_dir}/#{dir}"
end
cd(path) click to toggle source
# File src/lib/nixadm/backup.rb, line 40
def cd(path)
  Dir.chdir(path)
end
endDestZfsReplication(slave_object) click to toggle source
# File src/lib/nixadm/backup.rb, line 209
def endDestZfsReplication(slave_object)
  return success()
end
endSourceZfsReplication(master_object) click to toggle source
# File src/lib/nixadm/backup.rb, line 213
def endSourceZfsReplication(master_object)
  return success()
end
replicate(host, objects, targets) { |master_object, slave_object| ... } click to toggle source

Replicates ZFS filesystem(s) on from host to backup hosts

@param host The main host we are backing up @param filesystems An array of ZFS filesystems to replicate

@param targets An array or hashes defining target hosts to replicate to. It has the following form:

targets = [ { host: 'root@midway',  root: 'pool' },
            { host: 'root@backup.c21bowman.com',  root: 'pool' },
            { host: 'root@arc2', root: 'pool' } ]
# File src/lib/nixadm/backup.rb, line 132
def replicate(host, objects, targets)
  admin = NixAdm::ZFS::Admin.new(host)

  objects.each do |object|

    # First look for matching filesystem
    master_object = admin.object(object)

    if master_object.nil?
      raise "Error: #{object} does not exist"
    end

    startSourceZfsReplication master_object

    failed = false
    targets.each do | target |

      if target[:host].nil? or target[:pool].nil?
        raise "Invalid arguments: missing host or pool"
      end

      slave = NixAdm::ZFS::Host.new(target[:host])
      pool  = slave.pool(target[:pool])

      attempts = 0
      while attempts < 2
        slave_object = pool.object(master_object.name)

        break if not slave_object.nil?

        # If we get here, it means the remote object does not exists. If this
        # is a filesystem, we must first create it. If it's a zvol, we do
        # nothing.

        $stderr.puts "Dest ZFS entity '#{pool.name}/#{master_object.name}' " +
          "does not exist ... creating"

        if master_object.is_a?(NixAdm::ZFS::Filesystem)
          if pool.createFilesystem(master_object.name) == false
            msg = "Error: ZFS entity '#{pool.name}/#{master_object.name}' " + \
            "does not exist and could not be created"
            raise msg
          end
        elsif master_object.is_a?(NixAdm::ZFS::Volume)
          if pool.createVolume(master_object.name) == false
            msg = "Error: ZFS entity '#{pool.name}/#{master_object.name}' " + \
            "does not exist and could not be created"
            raise msg
          end
        end

        attempts += 1
      end

      if block_given?
        yield master_object, slave_object
      end

      if admin.replicate(master_object, slave_object) == true
        endDestZfsReplication slave_object
      else
        failed = true
        msg    = "FAILED: #{job} - #{admin.status.msg}"
        log msg, admin.status.val
      end
    end

    if failed == false
      endSourceZfsReplication master_object
    end
  end
end
replicationStatus(mfs, sfs) click to toggle source
# File src/lib/nixadm/backup.rb, line 44
def replicationStatus(mfs, sfs)
  h1 = mfs.pool.host.name
  p1 = mfs.pool.name
  f1 = mfs.name
  h2 = sfs.pool.host.name
  p2 = sfs.pool.name
  f2 = sfs.name

  return "replicating #{h1}:#{p1}/#{f1} -> #{h2}:#{p2}/#{f2}"
end
run_command(command) click to toggle source

Run general shell command and log output

# File src/lib/nixadm/backup.rb, line 56
def run_command(command)
  sys = NixAdm::Pipeline.new()

  sys.classic(command) do |stdin, stdout|
    stdin.close()

    while true
      line = stdout.gets
      break if line.nil?
      @logfile.write(line)
    end
  end
end
startSourceZfsReplication(master_object) click to toggle source
# File src/lib/nixadm/backup.rb, line 205
def startSourceZfsReplication(master_object)
  return success()
end
store(host, command, filename, zip: false) click to toggle source

Executes a command, captures output into file @param command A command or array containing a pipeline of commands @param filename The name (path) of the file to dump database to @param zip If true, gzip the file.

# File src/lib/nixadm/backup.rb, line 81
def store(host, command, filename, zip: false)
  sys      = NixAdm::Pipeline.new()
  pipeline = nil

  sys.debug = debug()

  # If the host we are performing this command on is the same host, don't
  # resolve with host as secong argument, as this just makes that host ssh
  # back into itself to run the command.
  if @host == host
    pipeline = resolveCommand(command)
  else
    pipeline = resolveCommand(command, host)
  end

  sys.run([pipeline]) do |stdin, stdout|
    stdin.close()

    dumpfile = File.open(filename, 'wb')

    while true
      data = stdout.read(1024)
      break if data == nil
      dumpfile.write(data)
    end

    dumpfile.close()
  end

  if zip == true
    # Compress the file
    sys.run "gzip -f #{filename}"
  end
end
trim(host, objects) click to toggle source

Trim snaphots on given filesystem

@param host The host name @param filesystems A list of filesystems to trim snapshots

# File src/lib/nixadm/backup.rb, line 221
def trim(host, objects)
  admin = NixAdm::ZFS::Admin.new(host)

  objects.each do |object|
    master_object = admin.object(object)

    if not master_object.nil?
      master_object.trimSnapshots()
    end
  end
end
zipStore(host, command, filename) click to toggle source

Executes a command, captures output into file and zips the file @param command A command or array containing a pipeline of commands @param filename The name (path) of the file to dump database to

# File src/lib/nixadm/backup.rb, line 73
def zipStore(host, command, filename)
  store host, command, filename, zip: true
end