wolfe 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5f3507d28088e498ba540c4cbe92f6e510f17d01
4
+ data.tar.gz: 8ee66841f21b71d418dca828b3b44fc7de7a0541
5
+ SHA512:
6
+ metadata.gz: 63e9c6f52d05dba90bd22c62fdc10de05b2be85ada6da2af1c4dff75dbd16cdf52527ad1c67dfda75b1234d51585c28dc7f225bb145821262ebe28bffd2467de
7
+ data.tar.gz: 3a1156453f097e830adf7f94d109569a1aa740d095d889a76428e2b829f36cf17201bd30bda4397a5495c2d61600ecd3bf33091727e4fbd9e08868c6401d92ca
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.5
@@ -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 wolfe.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Martin Sereinig
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.
@@ -0,0 +1,47 @@
1
+ # Wolfe
2
+
3
+ Wolfe's purpose is to clean up database (and other) backups that encode year, month, day and hour in their filename. That's also what wolfe will look at when deciding if a file has to be removed or not. Wolfe can be configured to keep daily and monthly backups for a given timespan and will always keep one backup for every year.
4
+
5
+ Original code was written by [@michaelem](https://github.com/michaelem), tests and gemification by [@srecnig](https://github.com/srecnig). In case you're wondering, the name wolfe is a [reference](http://www.imdb.com/title/tt0110912/quotes?item=qt0447112).
6
+
7
+ ## Installation
8
+
9
+ Wolfe can be installed through the usual sources: add it to your application's ``Gemfile`` or install manually through ``gem install``.
10
+
11
+ ## Usage
12
+
13
+ Call wolfe from the command line and give a rule file as one single parameter.
14
+
15
+ ```bash
16
+ wolfe cleanup rules.yml
17
+ ```
18
+
19
+ Or call wolfe from within your project if you've added it to the Gemfile.
20
+
21
+ ```ruby
22
+ Wolfe.run_cleanup "/path/to/rules.yml"
23
+ ```
24
+
25
+ ## Rules.yml
26
+
27
+ The rule file contains the information on where backups are stored, how the date metadata is encoded in the filename and how many backups to keep per timespan.
28
+
29
+ ```yaml
30
+ ---
31
+ backup_name:
32
+ path: /mnt/data/backups/database
33
+ filename: 'backup-database-%{year}-%{month}-%{day}-%{hour}.tar.gz'
34
+ one_per_day_timespan: 15.days
35
+ one_per_month_timespan: 1.year
36
+ ```
37
+
38
+ To configure the timespans we rely on ``active support``'s [time extensions](http://guides.rubyonrails.org/active_support_core_extensions.html#time) to ``Numeric``, so something like ``1.month``, ``2.years``, etc. will work. No spaces or ruby code please.
39
+
40
+ ## Contributing
41
+
42
+ Bug reports and pull requests are welcome on GitHub at https://github.com/lomography/wolfe. 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.
43
+
44
+ ## License
45
+
46
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
47
+
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "wolfe"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -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,4 @@
1
+ #!/usr/bin/env ruby
2
+ require 'wolfe'
3
+
4
+ Wolfe::CLI.start
@@ -0,0 +1,15 @@
1
+ require "yaml"
2
+
3
+ require "wolfe/cleanup"
4
+ require "wolfe/cli"
5
+ require "wolfe/version"
6
+
7
+ module Wolfe
8
+ def self.run_cleanup file_path
9
+ raise ArgumentError.new("Cleanup configuration file does not exist.") unless File.exist? file_path
10
+
11
+ config = YAML::load_file( file_path )
12
+ cleanup = Cleanup.new(config)
13
+ cleanup.start
14
+ end
15
+ end
@@ -0,0 +1,114 @@
1
+ require "active_support"
2
+ require "active_support/core_ext"
3
+ require "fileutils"
4
+
5
+ module Wolfe
6
+ class Cleanup
7
+ attr_accessor :configuration
8
+
9
+ def initialize(configuration)
10
+ @configuration = configuration
11
+ validate_configuration
12
+ end
13
+
14
+ def start
15
+ configuration.each do |name, config|
16
+ puts "-----------------------------------------------------"
17
+ puts "Cleaning up backups: #{name}"
18
+ puts "-----------------------------------------------------"
19
+
20
+ cleanup( config )
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ #
27
+ # validation
28
+ #
29
+
30
+ def validate_configuration
31
+ raise ArgumentError.new("Configuration must be hash.") unless configuration.is_a? Hash
32
+ configuration.each do |name, config|
33
+ raise ArgumentError.new("Configuration keys for #{name} missing.") unless configuration_keys.all? { |k| config.key?(k) }
34
+ raise ArgumentError.new("Invalid timespan argument for #{name}") unless config['one_per_day_timespan'] =~ timespan_regex
35
+ raise ArgumentError.new("Invalid timespan argument for #{name}") unless config['one_per_month_timespan'] =~ timespan_regex
36
+ raise ArgumentError.new("Path for #{name} does not exist.") unless Dir.exist?(config['path'])
37
+ end
38
+ end
39
+
40
+ def configuration_keys
41
+ ['path', 'filename', 'one_per_day_timespan', 'one_per_month_timespan']
42
+ end
43
+
44
+ def timespan_regex
45
+ /^\d+\.(days?|weeks?|months?|years?)$/
46
+ end
47
+
48
+ #
49
+ # cleanup
50
+ #
51
+
52
+ def cleanup( config )
53
+ daily_date = Date.today - eval( config['one_per_day_timespan'] )
54
+ monthly_date = Date.today - eval( config['one_per_month_timespan'] )
55
+ first_relevant_date = Date.today - 5.years
56
+
57
+ if File.directory?(config['path'])
58
+ clean_monthly( monthly_date, daily_date, config )
59
+ clean_yearly( first_relevant_date, monthly_date, config )
60
+ else
61
+ puts "Path '#{config['path']}' is not a directory."
62
+ end
63
+ end
64
+
65
+ def clean_monthly( monthly_date, daily_date, config )
66
+ daily_date.downto( monthly_date ) do |date|
67
+ delete_but_keep_one_per_month( config['path'], config['filename'], date )
68
+ end
69
+ end
70
+
71
+ def clean_yearly( first_relevant_date, monthly_date, config )
72
+ monthly_date.downto( first_relevant_date ) do |date|
73
+ delete_but_keep_one_per_year( config['path'], config['filename'], date )
74
+ end
75
+ end
76
+
77
+ def delete_but_keep_one_per_month( path, filename, date )
78
+ filename_month = filename % { year: date.strftime('%Y'),
79
+ month: date.strftime('%m'),
80
+ day: '*',
81
+ hour: '*' }
82
+ filename_day = filename % { year: date.strftime('%Y'),
83
+ month: date.strftime('%m'),
84
+ day: date.strftime('%d'),
85
+ hour: '*' }
86
+ delete_but_keep_one( full_path( path, filename_month ), full_path( path, filename_day ) )
87
+ end
88
+
89
+ def delete_but_keep_one_per_year( path, filename, date )
90
+ filename_year = filename % { year: date.strftime('%Y'),
91
+ month: '*',
92
+ day: '*',
93
+ hour: '*' }
94
+ filename_day = filename % { year: date.strftime('%Y'),
95
+ month: date.strftime('%m'),
96
+ day: date.strftime('%d'),
97
+ hour: '*' }
98
+ delete_but_keep_one( full_path( path, filename_year ), full_path( path, filename_day ) )
99
+ end
100
+
101
+ def full_path( path, filename )
102
+ File.expand_path(File.join(path, filename))
103
+ end
104
+
105
+ def delete_but_keep_one( keep_path, delete_path )
106
+ Dir.glob(delete_path).each do |f|
107
+ if Dir.glob(keep_path).count > 1
108
+ puts "Delete: #{f}"
109
+ FileUtils.rm(f)
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,14 @@
1
+ require 'thor'
2
+
3
+ module Wolfe
4
+ class CLI < Thor
5
+ desc "cleanup path/to/rule/file.yml", "Clean up files according to rules specified in given yaml file."
6
+ def cleanup file
7
+ begin
8
+ Wolfe.run_cleanup file
9
+ rescue ArgumentError => e
10
+ puts "Error: #{e}"
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module Wolfe
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'wolfe/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "wolfe"
8
+ spec.version = Wolfe::VERSION
9
+ spec.authors = ["Michael Emhofer", "Martin Sereinig"]
10
+ spec.email = ["dev@lomography.com"]
11
+
12
+ spec.summary = %q{Cleanup (backup) files by date.}
13
+ spec.description = "Often backup files have year, month, date and hour encoded in their filename. " \
14
+ "Wolfe uses this information to clean up such files and can be configured to " \
15
+ "keep daily/monthly backups for a certain timespans. It will always keep one backup per year."
16
+ spec.homepage = "http://github.com/lomography/wolfe"
17
+ spec.license = "MIT"
18
+
19
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency "thor"
25
+ spec.add_dependency "activesupport"
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.10"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "minitest"
30
+ spec.add_development_dependency "byebug"
31
+ end
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wolfe
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Michael Emhofer
8
+ - Martin Sereinig
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2015-10-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: thor
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: activesupport
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: bundler
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '1.10'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '1.10'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rake
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '10.0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '10.0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: minitest
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: byebug
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ description: Often backup files have year, month, date and hour encoded in their filename.
99
+ Wolfe uses this information to clean up such files and can be configured to keep
100
+ daily/monthly backups for a certain timespans. It will always keep one backup per
101
+ year.
102
+ email:
103
+ - dev@lomography.com
104
+ executables:
105
+ - wolfe
106
+ extensions: []
107
+ extra_rdoc_files: []
108
+ files:
109
+ - ".gitignore"
110
+ - ".travis.yml"
111
+ - CODE_OF_CONDUCT.md
112
+ - Gemfile
113
+ - LICENSE.txt
114
+ - README.md
115
+ - Rakefile
116
+ - bin/console
117
+ - bin/setup
118
+ - exe/wolfe
119
+ - lib/wolfe.rb
120
+ - lib/wolfe/cleanup.rb
121
+ - lib/wolfe/cli.rb
122
+ - lib/wolfe/version.rb
123
+ - wolfe.gemspec
124
+ homepage: http://github.com/lomography/wolfe
125
+ licenses:
126
+ - MIT
127
+ metadata: {}
128
+ post_install_message:
129
+ rdoc_options: []
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ requirements: []
143
+ rubyforge_project:
144
+ rubygems_version: 2.4.5
145
+ signing_key:
146
+ specification_version: 4
147
+ summary: Cleanup (backup) files by date.
148
+ test_files: []