Class: ZFS::Dataset

Inherits:
Object
  • Object
show all
Defined in:
lib/zfs/dataset.rb

Overview

A zfs dataset such as ‘pool1/backup’

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Dataset

Returns a new instance of Dataset.



8
9
10
# File 'lib/zfs/dataset.rb', line 8

def initialize(name)
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/zfs/dataset.rb', line 7

def name
  @name
end

Instance Method Details

#snapshot(name, recursive = false) ⇒ Object

Snapshots the dataset with the aid of ‘zfs snapshot’. name is sanitized to something zfs accepts, which means that spaces and non-word chars are replaced with ‘_’.



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/zfs/dataset.rb', line 28

def snapshot(name, recursive=false)
  arguments = []
  
  snapshot_name = sanitize_snapshot_name(name)
  
  arguments << '-r' if recursive
  arguments << "'#{self.name}@#{snapshot_name}'"
  
  zfs_snapshot(*arguments)
  
  return snapshot_name
end

#snapshot_with_timestamp(comment = nil, time = Time.now) ⇒ Object

Snapshots the dataset with a timestamp that looks like 201101011345 (year, month, day, hour and minute). If a comment is provided, the snapshot will have the comment appended to it, separated by a dash (201101011345-my_example_comment). Note that the sanitizing that #snapshot does applies to the comment as well.

This snapshotting is always recursive.



49
50
51
52
53
54
# File 'lib/zfs/dataset.rb', line 49

def snapshot_with_timestamp(comment=nil, time=Time.now)
  name = time.strftime("%Y%m%d%H%M")
  name << "-#{comment}" if comment
  
  snapshot(name, true)
end

#snapshotsObject

Returns an array of snapshots on the dataset. This does not include snapshots on the datasets children.



15
16
17
18
19
20
21
22
# File 'lib/zfs/dataset.rb', line 15

def snapshots
  list(name).
    lines.
    select { |l| l.index('@') }.      # only snapshots
    map { |l| l.chomp.split('@') }.   # <path, snapshot_name>
    select { |path, sn| path==name }. # only direct snapshots
    map { |path,sn| sn }              # only snapshot names
end