Class: Zfs::Snapshot

Inherits:
Object
  • Object
show all
Defined in:
lib/zfstools/snapshot.rb

Constant Summary collapse

@@stale_snapshot_size =
false

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, used = nil) ⇒ Snapshot

Returns a new instance of Snapshot.



5
6
7
8
# File 'lib/zfstools/snapshot.rb', line 5

def initialize(name, used=nil)
  @name = name
  @used = used
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/zfstools/snapshot.rb', line 4

def name
  @name
end

Class Method Details

.create(snapshot, options = {}) ⇒ Object

Create a snapshot



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/zfstools/snapshot.rb', line 46

def self.create(snapshot, options = {})
  flags=[]
  flags << "-r" if options['recursive']
  cmd = "zfs snapshot #{flags.join(" ")} #{snapshot}"

  if options['db']
    case options['db']
    when 'mysql'
      sql_query=<<-EOF.gsub(/^ {10}/, '')

        FLUSH LOGS;
        FLUSH TABLES WITH READ LOCK;
        SYSTEM #{cmd};
        UNLOCK TABLES;
      EOF
      cmd = %Q[mysql -e "#{sql_query}"]
    when 'postgresql'
      sql_pre_query = "SELECT PG_START_BACKUP('zfs-auto-snapshot');"
      sql_post_query = "SELECT PG_STOP_BACKUP();"
      zfs_cmd = cmd
      cmd = %Q[(psql -c "#{sql_pre_query}" postgres ; #{zfs_cmd} ) ; psql -c "#{sql_post_query}" postgres]
    end
  end

  puts cmd if $debug || $verbose
  system(cmd) unless $dry_run
end

.list(dataset = nil, options = {}) ⇒ Object

List all snapshots



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/zfstools/snapshot.rb', line 27

def self.list(dataset=nil, options={})
  snapshots = []
  flags=[]
  flags << "-d 1" if dataset and !options['recursive']
  flags << "-r" if options['recursive']
  cmd = "zfs list #{flags.join(" ")} -H -t snapshot -o name,used -S name"
  cmd += " #{dataset}" if dataset
  puts cmd if $debug
  IO.popen cmd do |io|
    io.readlines.each do |line|
      line.chomp!
      snapshot_name,used = line.split(' ')
      snapshots << self.new(snapshot_name, used.to_i)
    end
  end
  snapshots
end

Instance Method Details

#destroy(options = {}) ⇒ Object

Destroy a snapshot



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/zfstools/snapshot.rb', line 75

def destroy(options = {})
  # If destroying a snapshot, need to flag all other snapshot sizes as stale
  # so they will be relooked up.
  @@stale_snapshot_size = true
  # Default to deferred snapshot destroying
  flags=["-d"]
  flags << "-r" if options['recursive']
  cmd = "zfs destroy #{flags.join(" ")} #{@name}"
  puts cmd if $debug
  system(cmd) unless $dry_run
end

#is_zero?Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
# File 'lib/zfstools/snapshot.rb', line 19

def is_zero?
  if @used != 0
    return false
  end
  used
end

#usedObject



10
11
12
13
14
15
16
17
# File 'lib/zfstools/snapshot.rb', line 10

def used
  if @used.nil? or @@stale_snapshot_size
    cmd = "zfs get -Hp -o value used #{@name}"
    puts cmd if $debug
    @used = %x[#{cmd}].to_i
  end
  @used
end