thinning 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in thinning.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Tobias Schwab
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,24 @@
1
+ # Thinning
2
+
3
+ Thinning helps you clean up backups when you want to keep e.g. hourly backups for a day, daily backups for amonth, etc…
4
+
5
+ ## Installation
6
+
7
+ $ gem install thinning
8
+
9
+ ## Usage
10
+
11
+ Thinning lists files which should be deleted according to a provided pattern.
12
+
13
+ The command
14
+
15
+ thinning --hourly=24 --daily=14
16
+
17
+ will give you a list with all files which do not match the proivided pattern.
18
+
19
+ In real world thinning could be used inside a cronjob like this:
20
+
21
+ find /path/to/backups/mysql/ -size +1G -name "*.sql.gz" | thinning --hourly=24 --daily=14 | xargs rm
22
+
23
+ ## License
24
+ Thinning is released under the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/thinning ADDED
@@ -0,0 +1,48 @@
1
+ #!/usr/bin/env ruby
2
+ require "optparse"
3
+ $:.push(File.expand_path("../../lib", __FILE__))
4
+ require "thinning"
5
+ require "thinning/file"
6
+ require "thinning/cleaner"
7
+
8
+ HOUR = 3600
9
+ DAY = 24 * HOUR
10
+ WEEK = 7 * DAY
11
+ MONTH = 30 * DAY
12
+
13
+ pattern = {}
14
+ opts = OptionParser.new do |o|
15
+ o.on("-h HOURS", "--hourly", Integer, "hours to keep 1 file per hour") do |value|
16
+ pattern[:hourly] = value * HOUR
17
+ end
18
+
19
+ o.on("-d DAYS", "--daily", Integer, "days to keep 1 file per day") do |value|
20
+ pattern[:daily] = value * DAY
21
+ end
22
+
23
+ o.on("-w WEEKS", "--weekly", Integer, "weeks to keep 1 file per week") do |value|
24
+ pattern[:weekly] = value * WEEK
25
+ end
26
+
27
+ o.on("-m MONTHS", "--monthly", Integer, "months to keep 1 file per month") do |value|
28
+ pattern[:monthly] = value * MONTH
29
+ end
30
+ end
31
+
32
+ opts.parse(ARGV)
33
+
34
+ abort opts.to_s if pattern.empty?
35
+ if $stdin.tty?
36
+ $stderr.puts "ERROR: list of file must be provided though STDIN"
37
+ abort opts.to_s
38
+ end
39
+
40
+ def files_from_stream(stream)
41
+ STDIN.readlines.map(&:strip).map { |path| Thinning::File.new(path, File.mtime(path)) }
42
+ end
43
+
44
+ $stderr.puts "using pattern #{pattern.inspect}"
45
+
46
+ Thinning::Cleaner.new(files_from_stream($stdin), pattern).files_to_delete.each do |file|
47
+ puts file.path
48
+ end
@@ -0,0 +1,55 @@
1
+ module Thinning
2
+ class Cleaner
3
+ def initialize(files, pattern, now = Time.now)
4
+ @files = files
5
+ @pattern = pattern
6
+ @now = now
7
+ end
8
+
9
+ def files_to_delete
10
+ self.class.select_files_to_delete(@files, timestamps)
11
+ end
12
+
13
+ private
14
+
15
+ def timestamps
16
+ self.class.timestamps_for_pattern(@pattern, @now)
17
+ end
18
+
19
+ class << self
20
+ HOUR = 3600
21
+ DAY = 24 * HOUR
22
+
23
+ TYPE_MAPPING = {
24
+ :hourly => 1 * HOUR,
25
+ :daily=> 24 * HOUR,
26
+ :weekly => 7 * DAY,
27
+ :monthly => 30 * DAY,
28
+ :yearly => 365 * DAY
29
+ }
30
+
31
+ def select_files_to_delete(files, timestamps)
32
+ tmp_file = files.clone
33
+ timestamps.each do |ts|
34
+ if file = tmp_file.sort_by { |f| (f.timestamp - ts).abs }.first
35
+ tmp_file.delete(file)
36
+ end
37
+ end
38
+ tmp_file
39
+ end
40
+
41
+ def timestamps_for_pattern(pattern, now = Time.now)
42
+ stamps = []
43
+ pattern.each_pair do |type, duration|
44
+ current = now
45
+ step = TYPE_MAPPING.fetch(type)
46
+ while current > now - duration
47
+ stamps << current
48
+ current -= step
49
+ end
50
+ end
51
+ stamps.uniq
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,10 @@
1
+ module Thinning
2
+ class File
3
+ attr_reader :path, :timestamp
4
+
5
+ def initialize(path, timestamp)
6
+ @path = path
7
+ @timestamp = timestamp
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module Thinning
2
+ VERSION = "0.1.0"
3
+ end
data/lib/thinning.rb ADDED
@@ -0,0 +1,8 @@
1
+ require "thinning/version"
2
+
3
+ module Thinning
4
+ # Your code goes here...
5
+ end
6
+
7
+ require "thinning/file"
8
+ require "thinning/cleaner"
@@ -0,0 +1,107 @@
1
+ require "spec_helper"
2
+ require "time"
3
+ require "thinning"
4
+
5
+ describe "Thinning::Cleaner" do
6
+ it "can be initialized" do
7
+ Thinning::Cleaner.new([], {}, Time.now)
8
+ Thinning::Cleaner.new([], {})
9
+ end
10
+
11
+ describe "#files_to_delete" do
12
+ it "returns the correct files" do
13
+ file01 = new_file("2012-01-01T03:00:00Z") # delte because too old
14
+ file02 = new_file("2012-01-02T03:00:00Z")
15
+ file03 = new_file("2012-01-03T03:00:00Z")
16
+ file04 = new_file("2012-01-04T03:00:00Z")
17
+ file05 = new_file("2012-01-05T03:00:00Z")
18
+ file06 = new_file("2012-01-06T03:00:00Z")
19
+ file07 = new_file("2012-01-07T02:00:00Z") # delete because not best matching
20
+ file08 = new_file("2012-01-07T03:00:00Z")
21
+ file09 = new_file("2012-01-08T01:00:00Z") # delete because 3 hours ago
22
+ file10 = new_file("2012-01-08T02:00:00Z")
23
+ file11 = new_file("2012-01-08T03:00:00Z")
24
+
25
+ files = [file01, file02, file03, file04, file05, file06, file07, file08, file09, file10, file11]
26
+
27
+ now = Time.parse("2012-01-08T03:00:00Z")
28
+
29
+ cleaner = Thinning::Cleaner.new(files, { hourly: 2 * 3600, daily: 7 * 24 * 3600 }, now)
30
+
31
+ cleaner.send(:timestamps).count.should == 8
32
+ to_delete = cleaner.files_to_delete.should
33
+ to_delete.should == [
34
+ file01, file07, file09
35
+ ]
36
+ end
37
+ end
38
+
39
+ def new_file(time, path = nil)
40
+ path ||= "/some/path/#{rand(1000)}"
41
+ Thinning::File.new(path, Time.parse(time))
42
+ end
43
+
44
+ let(:time) { Time.parse("2012-01-10T00:00:00Z") }
45
+
46
+ describe "#timestamps_for_pattern" do
47
+ it "returns the correct timestamps" do
48
+ stamps = Thinning::Cleaner.timestamps_for_pattern({ :hourly => 2 * 3600 }, time)
49
+ stamps.should == [Time.parse("2012-01-10T00:00:00Z"), Time.parse("2012-01-09T23:00Z")]
50
+ end
51
+
52
+ it "returns more complex timestamps" do
53
+ stamps = Thinning::Cleaner.timestamps_for_pattern({ :hourly => 2 * 3600, daily: 3600 * 24 * 7 }, time)
54
+ stamps.should == [
55
+ Time.parse("2012-01-10T00:00:00Z"),
56
+ Time.parse("2012-01-09T23:00:00Z"),
57
+ Time.parse("2012-01-09T00:00:00Z"),
58
+ Time.parse("2012-01-08T00:00:00Z"),
59
+ Time.parse("2012-01-07T00:00:00Z"),
60
+ Time.parse("2012-01-06T00:00:00Z"),
61
+ Time.parse("2012-01-05T00:00:00Z"),
62
+ Time.parse("2012-01-04T00:00:00Z")
63
+ ]
64
+ end
65
+
66
+ it "returns the correct pattern for daily" do
67
+ stamps = Thinning::Cleaner.timestamps_for_pattern({ :daily => 2592000 }, time)
68
+ stamps.count.should == 30
69
+ end
70
+ end
71
+
72
+ describe "#select_files_to_delete" do
73
+ it "returns an array" do
74
+ files = []
75
+ timestamps = []
76
+ now = Time.parse("2012-01-03T00:00:00Z")
77
+ to_keep = Thinning::Cleaner.select_files_to_delete(files, timestamps)
78
+ to_keep.should be_kind_of(Array)
79
+ end
80
+
81
+ it "returns the correct files" do
82
+ file1 = new_file("2012-01-01T00:00:00Z")
83
+ file2 = new_file("2012-01-01T01:00:00Z")
84
+ file3 = new_file("2012-01-01T02:00:00Z")
85
+ file4 = new_file("2012-01-01T03:00:00Z")
86
+ files = [file1, file2, file3, file4]
87
+
88
+ timestamps = [file1.timestamp, file3.timestamp]
89
+ to_delete = Thinning::Cleaner.select_files_to_delete(files, timestamps)
90
+ to_delete.should == [file2, file4]
91
+ end
92
+
93
+ it "returns the correct files without exact matches" do
94
+ file1 = new_file("2012-01-01T23:59:00Z")
95
+ file2 = new_file("2012-01-02T00:59:00Z")
96
+ file3 = new_file("2012-01-02T01:59:00Z")
97
+ file4 = new_file("2012-01-02T02:00:00Z")
98
+ file5 = new_file("2012-01-02T03:00:00Z")
99
+ files = [file1, file2, file3, file4]
100
+
101
+ timestamps = [file1.timestamp, file4.timestamp]
102
+ to_delete = Thinning::Cleaner.select_files_to_delete(files, timestamps)
103
+ to_delete.should == [file2, file3]
104
+ files.should == [file1, file2, file3, file4]
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,8 @@
1
+ require "spec_helper"
2
+ require "thinning/file"
3
+
4
+ describe "Thinning::File" do
5
+ it "can be initialzied" do
6
+ file = Thinning::File.new("/path", Time.now)
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ RSpec.configure do |c|
2
+ c.treat_symbols_as_metadata_keys_with_true_values = true
3
+ c.filter_run :focus => true
4
+ c.run_all_when_everything_filtered = true
5
+ end
6
+
7
+ $:.push(File.expand_path("../../lib", __FILE__))
8
+ require "thinning"
data/thinning.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'thinning/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "thinning"
8
+ gem.version = Thinning::VERSION
9
+ gem.authors = ["Tobias Schwab"]
10
+ gem.email = ["tobias.schwab@dynport.de"]
11
+ gem.summary = %q{Cleanup backup files}
12
+ gem.description = %q{Display a list of files to be deleted by a specific pattern (e.g. keep hourly backups for 1 day, dauly for 90 days, etc)}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ gem.add_dependency "active_support"
20
+ gem.add_development_dependency "rspec"
21
+ gem.add_development_dependency "guard"
22
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: thinning
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Tobias Schwab
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-29 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: active_support
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: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
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: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: guard
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '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: '0'
62
+ description: Display a list of files to be deleted by a specific pattern (e.g. keep
63
+ hourly backups for 1 day, dauly for 90 days, etc)
64
+ email:
65
+ - tobias.schwab@dynport.de
66
+ executables:
67
+ - thinning
68
+ extensions: []
69
+ extra_rdoc_files: []
70
+ files:
71
+ - .gitignore
72
+ - Gemfile
73
+ - LICENSE.txt
74
+ - README.md
75
+ - Rakefile
76
+ - bin/thinning
77
+ - lib/thinning.rb
78
+ - lib/thinning/cleaner.rb
79
+ - lib/thinning/file.rb
80
+ - lib/thinning/version.rb
81
+ - spec/lib/thinning/cleaner_spec.rb
82
+ - spec/lib/thinning/file_spec.rb
83
+ - spec/spec_helper.rb
84
+ - thinning.gemspec
85
+ homepage: ''
86
+ licenses: []
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 1.8.23
106
+ signing_key:
107
+ specification_version: 3
108
+ summary: Cleanup backup files
109
+ test_files:
110
+ - spec/lib/thinning/cleaner_spec.rb
111
+ - spec/lib/thinning/file_spec.rb
112
+ - spec/spec_helper.rb