zfs-snapshot 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/bin/zfs-snapshot +87 -0
  2. data/lib/zfs-snapshot.rb +96 -0
  3. metadata +54 -0
data/bin/zfs-snapshot ADDED
@@ -0,0 +1,87 @@
1
+ #!/usr/local/bin/ruby
2
+ #
3
+ # Simple ZFS Snapshotting
4
+ # Copyright (c) 2009, Jeremy Tregunna, All Rights Reserved
5
+ #
6
+ # This work `as-is' we provide.
7
+ # No warranties of any kind, express or implied.
8
+ # We've done our best,
9
+ # to debug and test.
10
+ # Liability for damages denied.
11
+ #
12
+ # Permission is granted hereby,
13
+ # to copy, share, and modify.
14
+ # Use as is fit,
15
+ # free or for profit.
16
+ # These rights, on this notice, rely.
17
+ #
18
+ # == Synopsis
19
+ # Simple utility to take snapshots at periodic intervals defined by the user
20
+ # with the assistance of a cron daemon.
21
+ # This utility is meant to be run through cron, and not at the command line.
22
+ #
23
+ # == Examples
24
+ # To take snapshots hourly, on the hour, one might use:
25
+ #
26
+ # 0 * * * * root /usr/local/bin/zfs-snapshot tank/home hourly
27
+ #
28
+ # To recursively take a snapshot monthly, at 4:20 AM on the 1st one might use:
29
+ #
30
+ # 20 4 1 * * root /usr/local/bin/zfs-snapshot -r tank/home monthly
31
+ #
32
+ # == Usage
33
+ # zfs-snapshot [options] filesystem_name snapshot_name
34
+ #
35
+ # == Options
36
+ # -h, --help Display this help message
37
+ # -v, --version Display the version of this script, and exit
38
+ # -r, --recursive Recursively create snapshots
39
+ #
40
+ # == Author
41
+ # Jeremy Tregunna
42
+ #
43
+ # == Copyright
44
+ # Copyright (c) 2009, Jeremy Tregunna, All Rights Reserved.
45
+ # Released under the Poetic Licence.
46
+ #
47
+ # This work `as-is' we provide.
48
+ # No warranties of any kind, express or implied.
49
+ # We've done our best,
50
+ # to debug and test.
51
+ # Liability for damages denied.
52
+ #
53
+ # Permission is granted hereby,
54
+ # to copy, share, and modify.
55
+ # Use as is fit,
56
+ # free or for profit.
57
+ # These rights, on this notice, rely.
58
+
59
+ require 'rdoc/usage'
60
+ require 'time'
61
+ require 'ostruct'
62
+ require 'optparse'
63
+ require 'zfs-snapshot'
64
+
65
+ SZS_VERSION = "1.0.1"
66
+ $options = OpenStruct.new
67
+ $options.recursive = false
68
+
69
+ def output_version
70
+ puts "#{File.basename(__FILE__)} version #{SZS_VERSION}"
71
+ end
72
+
73
+ def output_help
74
+ output_version
75
+ RDoc::usage()
76
+ end
77
+
78
+ opts = OptionParser.new
79
+ opts.on("-h", "--help") { output_help }
80
+ opts.on("-v", "--version") { output_version; exit 0 }
81
+ opts.on("-r", "--recursive") { $options.recursive = true }
82
+ opts.parse!(ARGV)
83
+
84
+ output_help if ARGV[0].nil?
85
+
86
+ fs = Filesystem.new(ARGV[0], ARGV[1], $options.recursive)
87
+ fs.take_snapshot
@@ -0,0 +1,96 @@
1
+ #!/usr/local/bin/ruby
2
+ #
3
+ # Simple ZFS Snapshotting
4
+ # Copyright (c) 2009, Jeremy Tregunna, All Rights Reserved
5
+ #
6
+ # This work `as-is' we provide.
7
+ # No warranties of any kind, express or implied.
8
+ # We've done our best,
9
+ # to debug and test.
10
+ # Liability for damages denied.
11
+ #
12
+ # Permission is granted hereby,
13
+ # to copy, share, and modify.
14
+ # Use as is fit,
15
+ # free or for profit.
16
+ # These rights, on this notice, rely.
17
+ #
18
+ # == Synopsis
19
+ # Simple utility to take snapshots at periodic intervals defined by the user
20
+ # with the assistance of a cron daemon.
21
+ # This utility is meant to be run through cron, and not at the command line.
22
+ #
23
+ # == Examples
24
+ # To take snapshots hourly, on the hour, one might use:
25
+ #
26
+ # 0 * * * * root /usr/local/bin/zfs-snapshot tank/home hourly
27
+ #
28
+ # To recursively take a snapshot monthly, at 4:20 AM on the 1st one might use:
29
+ #
30
+ # 20 4 1 * * root /usr/local/bin/zfs-snapshot -r tank/home monthly
31
+ #
32
+ # == Usage
33
+ # zfs-snapshot [options] filesystem_name snapshot_name
34
+ #
35
+ # == Options
36
+ # -h, --help Display this help message
37
+ # -v, --version Display the version of this script, and exit
38
+ # -r, --recursive Recursively create snapshots
39
+ #
40
+ # == Author
41
+ # Jeremy Tregunna
42
+ #
43
+ # == Copyright
44
+ # Copyright (c) 2009, Jeremy Tregunna, All Rights Reserved.
45
+ # Released under the Poetic Licence.
46
+ #
47
+ # This work `as-is' we provide.
48
+ # No warranties of any kind, express or implied.
49
+ # We've done our best,
50
+ # to debug and test.
51
+ # Liability for damages denied.
52
+ #
53
+ # Permission is granted hereby,
54
+ # to copy, share, and modify.
55
+ # Use as is fit,
56
+ # free or for profit.
57
+ # These rights, on this notice, rely.
58
+
59
+ require 'rdoc/usage'
60
+ require 'time'
61
+
62
+ class Filesystem
63
+ attr_reader :name, :recursive, :period
64
+
65
+ def initialize(name, period, recurse)
66
+ @name = name
67
+ @period = period
68
+ @recursive = recurse
69
+ end
70
+
71
+ def take_snapshot
72
+ if existing_snapshot?
73
+ p "found a snapshot"
74
+ remove_snapshot
75
+ end
76
+ create_snapshot
77
+ end
78
+
79
+ private
80
+
81
+ def remove_snapshot
82
+ system("zfs destroy #{@recursive ? '-r' : ''} #{@name}@#{@period}")
83
+ end
84
+
85
+ def create_snapshot
86
+ system("zfs snapshot #{@recursive ? '-r' : ''} #{@name}@#{@period}")
87
+ end
88
+
89
+ def existing_snapshot?
90
+ IO.popen("zfs list -t snapshot").readlines.collect do |line|
91
+ line.split(" ")
92
+ end.select do |item|
93
+ item.first == "#{@name}@#{@period}"
94
+ end.collect { |item| item.first.split("@")[1] == @period }.first
95
+ end
96
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zfs-snapshot
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jeremy Tregunna
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-12 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Keeps one copy of the snapshot you create. Removes the old one before creating the new one.
17
+ email: jeremy.tregunna@me.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/zfs-snapshot.rb
26
+ - bin/zfs-snapshot
27
+ has_rdoc: true
28
+ homepage:
29
+ post_install_message:
30
+ rdoc_options: []
31
+
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: "0"
39
+ version:
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ requirements: []
47
+
48
+ rubyforge_project:
49
+ rubygems_version: 1.3.1
50
+ signing_key:
51
+ specification_version: 2
52
+ summary: Simple ZFS Snapshotting script.
53
+ test_files: []
54
+