class NixAdm::ZFS::Snapshot

Attributes

host[R]
object[R]

Public Class Methods

new(object, name) click to toggle source
Calls superclass method NixAdm::ZFS::Object::new
# File src/lib/nixadm/zfs.rb, line 413
def initialize(object, name)

  if object.is_a?(ZfsEntity) == false
    raise 'First argument must be filesystem or zvol'
  end

  super object.host, name

  @object = object
end

Public Instance Methods

id() click to toggle source
# File src/lib/nixadm/zfs.rb, line 424
def id()
  return @name.split('@')[1].to_i
end
previous() click to toggle source
# File src/lib/nixadm/zfs.rb, line 428
def previous()
  return @object.previousSnapshotId(id())
end
send(object) click to toggle source

Push this snapshot to other object

# File src/lib/nixadm/zfs.rb, line 433
def send(object)

  if object.is_a?(ZfsEntity) == false
    raise 'First argument must be filesystem or zvol'
  end

  # Used for incremental if applicable
  zfs_options = ''

  # See if remote object has a matching previous snapshot to use as
  # reference for incremental send
  set = @object.snapshotIds()[0..-1]
  previous_snap_id = object.latestMatchingSnapshotId(set)

  if previous_snap_id == set[-1]
    return true
  end

  if not previous_snap_id.nil?
    # We can send an incremental snapshot
    zfs_options = "-i #{previous_snap_id}"
  else
    # There are not previous reference snapshots we can use so we have to send
    # a full snapshot. We may as well clear out all remote snapshots (if any)
    # before doing do.
    #
    # Note: if this pukes for any reason, it should throw exception because
    # there's no way we can handle a failure here.
    object.deleteAllSnapshots()
  end

  # This sends either full or incremental depending on the value of
  # zfs_options variable.

  return push(object, zfs_options)
end

Private Instance Methods

push(object, zfs_options='') click to toggle source
# File src/lib/nixadm/zfs.rb, line 472
def push(object, zfs_options='')
  snapshot    = "#{@object.pool.name}/#{@name}"
  remote_port = object.pool.host.port
  remote_host = object.pool.host.name

  command_1 = "zfs send #{zfs_options} #{snapshot}"

  command_2 = "ssh -p #{remote_port} #{remote_host} " +
              "zfs receive -F #{object.pool.name}/#{object.name}"

  command = @host.resolveCommand [ command_1, command_2 ]

  return @host.run(command)
end