tarsnap_pruner 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.dotenv.rspec ADDED
@@ -0,0 +1,3 @@
1
+ TARSNAP=/usr/local/bin/tarsnap
2
+ KEY_DIRECTORY=spec/fixtures/keys
3
+ CACHES_DIRECTORY=tmp/spec
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tarsnap_pruner.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 West Arete Computing, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # TarsnapPruner
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/tarsnap_pruner`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'tarsnap_pruner'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install tarsnap_pruner
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. Run `bundle exec tarsnap_pruner` to use the gem in this directory, ignoring other installed copies of this gem.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/tarsnap_pruner. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "tarsnap_pruner"
5
+
6
+ require "irb"
7
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,40 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Prune old backups from tarsnap, in order to save on storage costs.
4
+
5
+ require "tarsnap_pruner"
6
+
7
+ TarsnapPruner.configure(ARGV.first)
8
+ unless ENV['KEY_DIRECTORY']
9
+ puts "Supply a config file or define ENV['KEY_DIRECTORY']"
10
+ exit 1
11
+ end
12
+
13
+ TarsnapPruner::Machine.all.each do |machine|
14
+ puts
15
+ puts "===== #{machine.hostname} ====="
16
+
17
+ scheduler = TarsnapPruner::Scheduler.new(machine.archives)
18
+
19
+ puts "Daily backups to keep:"
20
+ puts scheduler.dailies
21
+
22
+ puts "Weekly backups to keep:"
23
+ puts scheduler.weeklies_to_keep
24
+
25
+ puts "Monthly backups to keep:"
26
+ puts scheduler.monthlies_to_keep
27
+
28
+ puts "----- 8< -----"
29
+
30
+ puts "Weekly backups to prune:"
31
+ puts scheduler.weeklies_to_prune
32
+
33
+ puts "Monthly backups to prune:"
34
+ puts scheduler.monthlies_to_prune
35
+
36
+ puts "Unknown backups to prune:"
37
+ puts scheduler.unknowns
38
+
39
+ machine.delete(scheduler.archives_to_prune)
40
+ end
@@ -0,0 +1,32 @@
1
+ require 'date'
2
+
3
+ module TarsnapPruner
4
+
5
+ # An instance of this class represents a single tarsnap backup.
6
+ #
7
+ # We expect the name of each archive to contain an iso8601 date that
8
+ # represents the date that the backup was taken.
9
+ class Archive
10
+
11
+ # Expects the name of the archive, as given to tarsnap. It should contain
12
+ # an iso8601 date (e.g. 2015-05-15) that represents when the backup was
13
+ # taken.
14
+ def initialize(name)
15
+ @name = name
16
+ end
17
+
18
+ attr_reader :name
19
+
20
+ # Return a Ruby Date object for the backup's date, or nil if one could not
21
+ # be discerned from the archive name.
22
+ def date
23
+ name =~ /.*(\d{4}-\d{2}-\d{2})/ ? Date.parse($1) : nil
24
+ end
25
+
26
+ # The number of days that have elapsed since the backup date.
27
+ def elapsed
28
+ date ? Date.today - date : nil
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,48 @@
1
+ require 'tmpdir'
2
+
3
+ module TarsnapPruner
4
+
5
+ # An instance of this class represents one machine that's being backed up.
6
+ class Machine
7
+
8
+ def self.key_files
9
+ Dir.glob(File.join(ENV['KEY_DIRECTORY'], '*.key')).sort
10
+ end
11
+
12
+ def self.all
13
+ key_files.map { |f| new(f) }
14
+ end
15
+
16
+ def initialize(key_file, tarsnap=nil)
17
+ @key_file = key_file
18
+ @tarsnap = tarsnap || Tarsnap.new(@key_file)
19
+ end
20
+
21
+ attr_reader :key_file
22
+
23
+ def hostname
24
+ key_file =~ /tarsnap-(.*)\.key$/ ? $1 : 'unknown'
25
+ end
26
+
27
+ def archives
28
+ @tarsnap.list_archives.split.sort.map { |name| Archive.new(name) }
29
+ end
30
+
31
+ def delete(archives_to_delete)
32
+ if archives_to_delete.any?
33
+ Dir.mktmpdir("cache-", ENV['CACHES_DIRECTORY']) do |cache_directory|
34
+ # fsck so that we have an up-to-date cache (required for pruning). This
35
+ # means that each machine will have to fsck again, but there's apparently
36
+ # no way around it.
37
+ # Ref: http://mail.tarsnap.com/tarsnap-users/msg00512.html
38
+ @tarsnap.fsck(cache_directory)
39
+ archives_to_delete.each do |archive|
40
+ @tarsnap.delete(archive, cache_directory)
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+ end
47
+
48
+ end
@@ -0,0 +1,53 @@
1
+ module TarsnapPruner
2
+
3
+ class Scheduler
4
+
5
+ def initialize(archives, daily_boundary=182, weekly_boundary=730)
6
+ @archives = archives
7
+ @daily_boundary = daily_boundary
8
+ @weekly_boundary = weekly_boundary
9
+ end
10
+
11
+ def knowns
12
+ @archives.select(&:date)
13
+ end
14
+
15
+ def unknowns
16
+ @archives.reject(&:date)
17
+ end
18
+
19
+ def dailies
20
+ knowns.select { |a| a.elapsed < @daily_boundary }
21
+ end
22
+
23
+ def weeklies
24
+ knowns.select { |a| a.elapsed >= @daily_boundary && a.elapsed < @weekly_boundary }
25
+ end
26
+
27
+ def monthlies
28
+ knowns.select { |a| a.elapsed >= @weekly_boundary }
29
+ end
30
+
31
+ def weeklies_to_keep
32
+ weeklies.group_by { |a| a.date.cweek }.values.map(&:last)
33
+ end
34
+
35
+ def weeklies_to_prune
36
+ weeklies - weeklies_to_keep
37
+ end
38
+
39
+ def monthlies_to_keep
40
+ monthlies.group_by { |a| a.date.month }.values.map(&:last)
41
+ end
42
+
43
+ def monthlies_to_prune
44
+ monthlies - monthlies_to_keep
45
+ end
46
+
47
+ def archives_to_prune
48
+ unknowns + monthlies_to_prune + weeklies_to_prune
49
+ end
50
+
51
+ end
52
+
53
+ end
@@ -0,0 +1,30 @@
1
+ module TarsnapPruner
2
+
3
+ # A class to encapsulate invoking the command-line tarsnap commands.
4
+ class Tarsnap
5
+
6
+ def initialize(key_file)
7
+ @key_file = key_file
8
+ end
9
+
10
+ def list_archives
11
+ `#{tarsnap} --list-archives --keyfile #{@key_file}`
12
+ end
13
+
14
+ def fsck(cache_directory)
15
+ system "#{tarsnap} --fsck --keyfile #{@key_file} --cachedir #{cache_directory} > /dev/null"
16
+ end
17
+
18
+ def delete(archive, cache_directory)
19
+ system "#{tarsnap} -d --keyfile #{@key_file} --cachedir #{cache_directory} -f #{archive.name} > /dev/null"
20
+ end
21
+
22
+ private
23
+
24
+ def tarsnap
25
+ ENV['TARSNAP_COMMAND']
26
+ end
27
+
28
+ end
29
+
30
+ end
@@ -0,0 +1,3 @@
1
+ module TarsnapPruner
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,21 @@
1
+ require "tarsnap_pruner/version"
2
+ require "tarsnap_pruner/archive"
3
+ require "tarsnap_pruner/scheduler"
4
+ require "tarsnap_pruner/tarsnap"
5
+ require "tarsnap_pruner/machine"
6
+
7
+ require 'dotenv'
8
+
9
+ module TarsnapPruner
10
+
11
+ module_function
12
+
13
+ def configure(config_file=nil)
14
+ if config_file
15
+ Dotenv.load(config_file)
16
+ else
17
+ Dotenv.load
18
+ end
19
+ end
20
+
21
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'tarsnap_pruner/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "tarsnap_pruner"
8
+ spec.version = TarsnapPruner::VERSION
9
+ spec.authors = ["Scott Woods"]
10
+ spec.email = ["scott@westarete.com"]
11
+
12
+ spec.summary = %q{A method of deleting old tarsnap backups.}
13
+ spec.description = %q{The Tarsnap backup service charges based on storage and bandwith. This gem provides a technique for grandfathering old backups in order to save on storage costs.}
14
+ spec.homepage = "https://github.com/westarete/tarsnap_pruner"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_runtime_dependency "dotenv"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.10"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec"
27
+ spec.add_development_dependency "timecop"
28
+ end
data/tmp/spec/.gitkeep ADDED
File without changes
metadata ADDED
@@ -0,0 +1,149 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tarsnap_pruner
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Scott Woods
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2015-12-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: dotenv
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.10'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.10'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '10.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: timecop
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: The Tarsnap backup service charges based on storage and bandwith. This
95
+ gem provides a technique for grandfathering old backups in order to save on storage
96
+ costs.
97
+ email:
98
+ - scott@westarete.com
99
+ executables:
100
+ - tarsnap_pruner
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - .dotenv.rspec
105
+ - .gitignore
106
+ - .rspec
107
+ - .travis.yml
108
+ - CODE_OF_CONDUCT.md
109
+ - Gemfile
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - bin/console
114
+ - bin/setup
115
+ - exe/tarsnap_pruner
116
+ - lib/tarsnap_pruner.rb
117
+ - lib/tarsnap_pruner/archive.rb
118
+ - lib/tarsnap_pruner/machine.rb
119
+ - lib/tarsnap_pruner/scheduler.rb
120
+ - lib/tarsnap_pruner/tarsnap.rb
121
+ - lib/tarsnap_pruner/version.rb
122
+ - tarsnap_pruner.gemspec
123
+ - tmp/spec/.gitkeep
124
+ homepage: https://github.com/westarete/tarsnap_pruner
125
+ licenses:
126
+ - MIT
127
+ post_install_message:
128
+ rdoc_options: []
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ none: false
133
+ requirements:
134
+ - - ! '>='
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ none: false
139
+ requirements:
140
+ - - ! '>='
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ requirements: []
144
+ rubyforge_project:
145
+ rubygems_version: 1.8.23
146
+ signing_key:
147
+ specification_version: 3
148
+ summary: A method of deleting old tarsnap backups.
149
+ test_files: []