bup 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 66fb72e2d0fbc1251274b4b505e62f695cf975aba7daa6fb6896fe7c72f2f969
4
+ data.tar.gz: 38be925b9fb61f41475b495145b572e6244ce8a692b40e8af311816315cca517
5
+ SHA512:
6
+ metadata.gz: 0c9621fb9d2087df3767a57ce5e71f540e8316d1dc0be9a1a8eb47c3da81f9230f234de7832f2b507754764759305a3edb21131da182caeb1a40bb6e752e3615
7
+ data.tar.gz: d762849552b7c575d7a366eaeea96900a1fd822ef07f4e7cf736afb737368c004e2aab9a8a34563971f9124cc348fd5bca0aa6686327ef34edc73a2b37fdffe0
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.0
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2021-11-10
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in bup.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.7"
data/Gemfile.lock ADDED
@@ -0,0 +1,56 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ bup (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ diff-lcs (1.4.4)
11
+ parallel (1.21.0)
12
+ parser (3.0.2.0)
13
+ ast (~> 2.4.1)
14
+ rainbow (3.0.0)
15
+ rake (13.0.6)
16
+ regexp_parser (2.1.1)
17
+ rexml (3.2.5)
18
+ rspec (3.10.0)
19
+ rspec-core (~> 3.10.0)
20
+ rspec-expectations (~> 3.10.0)
21
+ rspec-mocks (~> 3.10.0)
22
+ rspec-core (3.10.1)
23
+ rspec-support (~> 3.10.0)
24
+ rspec-expectations (3.10.1)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.10.0)
27
+ rspec-mocks (3.10.2)
28
+ diff-lcs (>= 1.2.0, < 2.0)
29
+ rspec-support (~> 3.10.0)
30
+ rspec-support (3.10.3)
31
+ rubocop (1.22.3)
32
+ parallel (~> 1.10)
33
+ parser (>= 3.0.0.0)
34
+ rainbow (>= 2.2.2, < 4.0)
35
+ regexp_parser (>= 1.8, < 3.0)
36
+ rexml
37
+ rubocop-ast (>= 1.12.0, < 2.0)
38
+ ruby-progressbar (~> 1.7)
39
+ unicode-display_width (>= 1.4.0, < 3.0)
40
+ rubocop-ast (1.13.0)
41
+ parser (>= 3.0.1.1)
42
+ ruby-progressbar (1.11.0)
43
+ unicode-display_width (2.1.0)
44
+
45
+ PLATFORMS
46
+ x86_64-darwin-20
47
+ x86_64-linux
48
+
49
+ DEPENDENCIES
50
+ bup!
51
+ rake (~> 13.0)
52
+ rspec (~> 3.0)
53
+ rubocop (~> 1.7)
54
+
55
+ BUNDLED WITH
56
+ 2.2.26
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 TODO: Write your name
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,43 @@
1
+ # Bup
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/bup`. 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 'bup'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install bup
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.
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 the created tag, 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]/bup. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/bup/blob/main/CODE_OF_CONDUCT.md).
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the Bup project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/bup/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "bup"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/bup.gemspec ADDED
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/bup/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "bup"
7
+ spec.version = Bup::VERSION
8
+ spec.authors = ["Sam Baskinger"]
9
+ spec.email = ["basking2@yahoo.com"]
10
+
11
+ spec.summary = "Backup tool driver."
12
+ spec.description = "Backup tool driver that connects tar, gnupg, and other tools to accomplish backups."
13
+ spec.homepage = "https://rubygems.org/gems/bup"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 3.0.2"
16
+
17
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'https://mygemserver.com'"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = "https://gitlab.com/basking2/bup"
21
+ spec.metadata["changelog_uri"] = "https://gitlab.com/basking2/bup/-/blob/main/CHANGELOG.md"
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject do |f|
27
+ (f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
28
+ end
29
+ end
30
+ spec.bindir = "exe"
31
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ["lib"]
33
+
34
+ # Uncomment to register a new dependency of your gem
35
+ # spec.add_dependency "example-gem", "~> 1.0"
36
+
37
+ # For more information and examples about making a new gem, checkout our
38
+ # guide at: https://bundler.io/guides/creating_gem.html
39
+ end
data/buprc ADDED
@@ -0,0 +1,14 @@
1
+ ---
2
+ profiles:
3
+ default:
4
+ include:
5
+ - "$HOME"
6
+ exclude:
7
+ - "$HOME"
8
+ - "$HOME/backups"
9
+ lastrun: '2021-11-11T14:18:27 +0000'
10
+ destination: "$HOME/backups"
11
+ history: 10
12
+ tarcmd:
13
+ - sudo
14
+ - tar
data/exe/bup ADDED
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bup"
5
+ require "optparse"
6
+
7
+ configfile = File.join(ENV["HOME"], ".buprc")
8
+ config = Bup::Config.new
9
+
10
+ OptParse.new do |opt|
11
+ opt.banner = "#{$PROGRAM_NAME} #{Bup::VERSION}"
12
+
13
+ opt.on("-c", "--config=file", String, "Config file.") do |c|
14
+ configfile = c
15
+ end
16
+
17
+ opt.on("-p", "--profile=profile", String, "Profile name.") do |p|
18
+ config.runtime["profile"] = p
19
+ end
20
+
21
+ opt.on("-t", "--type=type", String, "Type of backup.") do |t|
22
+ config.runtime["type"] = t == "incremental" ? t : "full"
23
+ end
24
+ end.parse!
25
+
26
+ config.load(configfile) if File.exist?(configfile)
27
+
28
+ tar = Bup::Tar.new(config)
29
+
30
+ tar.call
31
+
32
+ config.save(configfile)
data/lib/bup/config.rb ADDED
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yaml"
4
+
5
+ require "date"
6
+
7
+ module Bup
8
+ class Config
9
+ @@timeformat = "%Y-%m-%dT%H:%M:%S %z"
10
+
11
+ attr_accessor :config, :runtime
12
+
13
+ def initialize
14
+ @runtime = {
15
+ "profile" => "default",
16
+ "type" => "full"
17
+ }
18
+
19
+ @config = {
20
+ "profiles" => {
21
+ "default" => {
22
+ }
23
+ }
24
+ }
25
+ end
26
+
27
+ def load(file)
28
+ c = File.open(file) do |io|
29
+ YAML.safe_load(io)
30
+ end
31
+
32
+ Config.merge(@config, c)
33
+ end
34
+
35
+ def save(file)
36
+ File.open(file, "w") do |io|
37
+ io.write(YAML.dump(@config))
38
+ end
39
+ end
40
+
41
+ def update_lastrun(name)
42
+ date = DateTime.now.new_offset(0)
43
+ set_lastrun(name, date)
44
+ end
45
+
46
+ # Return the last run time of the backup or nil if there is none.
47
+ def lastrun(name)
48
+ DateTime.strptime(profile(name)["lastrun"] || "", @@timeformat)
49
+ rescue Date::Error
50
+ nil
51
+ end
52
+
53
+ # Return the last run time of the backup or nil if there is none.
54
+ def set_lastrun(name, date)
55
+ profile(name)["lastrun"] = date.strftime(@@timeformat)
56
+ end
57
+
58
+ def profile(name)
59
+ @config["profiles"][name] || (raise RuntimeError("No such profile #{name}."))
60
+ end
61
+
62
+ def self.merge(c1, c2)
63
+ (c1.keys + c2.keys)
64
+ .each_with_object({}) do |r, l|
65
+ l[r] = 1
66
+ end.each_key do |key|
67
+ v1 = c1[key]
68
+ v2 = c2[key]
69
+ if v1.nil?
70
+ if v2.nil?
71
+ # nop
72
+ else
73
+ c1[key] = v2
74
+ end
75
+ elsif v2.nil?
76
+ # Nop - c1[key] already equals v1.
77
+ elsif v1.instance_of?(Array) && v2.instance_of?(Array)
78
+ c1[key] = v1 + v2
79
+ elsif v1.instance_of?(Hash) && v2.instance_of?(Hash)
80
+ c1[key] = merge(v1, v2)
81
+ elsif !v2.nil?
82
+ c1[key] = v2
83
+ end
84
+ end
85
+ c1
86
+ end
87
+ end
88
+ end
data/lib/bup/tar.rb ADDED
@@ -0,0 +1,121 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "English"
4
+ require "English"
5
+ require "tempfile"
6
+ require "open3"
7
+ require "date"
8
+
9
+ module Bup
10
+ class Tar
11
+ def initialize(config)
12
+ @config = config
13
+ end
14
+
15
+ def expand_string(str)
16
+ while str =~ /\${?([a-zA-Z0-9]+)}?/
17
+ all = $LAST_MATCH_INFO[0]
18
+ nm = $LAST_MATCH_INFO[1]
19
+ str = str.sub(all, ENV[nm] || "")
20
+ end
21
+
22
+ str
23
+ end
24
+
25
+ def build_exclude_file(patterns)
26
+ tf = Tempfile.new("bup")
27
+ patterns.each do |p|
28
+ p = expand_string(p)
29
+ tf.write(p)
30
+ tf.write("\n")
31
+ end
32
+ tf.close
33
+ tf
34
+ end
35
+
36
+ # Prepare the destination directory.
37
+ def prepare_destination(profile_name)
38
+ destination = expand_string(@config.profile(profile_name)["destination"] || ".")
39
+ suffix = ".tar.xz"
40
+ type = @config.runtime["type"] || "full"
41
+
42
+ FileUtils.mkdir_p(destination) unless File.exist?(destination)
43
+
44
+ filename_base = "#{profile_name}-#{type}"
45
+ filename = File.join(destination,
46
+ "#{filename_base}-#{DateTime.now.new_offset(0).strftime("%Y%m%d%H%M%S")}#{suffix}")
47
+
48
+ history = @config.profile(profile_name)["history"] || 0
49
+
50
+ if type == "full" && history.positive?
51
+
52
+ oldest_kept_file = nil
53
+
54
+ # Keep some full backups and remove the others.
55
+ # We capture the oldest full backup and get rid of preceeding incrementals.
56
+ Dir["#{destination}/#{filename_base}*"].sort.reverse.each_with_index do |fullbackup, idx|
57
+ if idx < history
58
+ oldest_kept_file = fullbackup
59
+ else
60
+ File.delete(fullbackup)
61
+ end
62
+ end
63
+
64
+ # Remove all incremental files that are older than the oldest kept full backup.
65
+ remove_before = File.stat(oldest_kept_file).ctime
66
+ Dir["#{destination}/#{profile_name}*"].each do |backupfile|
67
+ File.delete(backupfile) if File.stat(backupfile).ctime < remove_before
68
+ end
69
+ end
70
+
71
+ filename
72
+ end
73
+
74
+ def call(profile_name = nil)
75
+ profile_name ||= @config.runtime["profile"]
76
+ profile = @config.config["profiles"][profile_name]
77
+
78
+ raise "Missing profile #{profile_name}." unless profile
79
+
80
+ args = @config.profile(profile_name)["tarcmd"].dup || ["tar"]
81
+
82
+ args << "cJvf"
83
+
84
+ filename = prepare_destination(profile_name)
85
+ args << filename
86
+
87
+ if @config.runtime["type"] == "incremental"
88
+ lastrun = @config.lastrun(profile_name)
89
+ args += ["--newer", lastrun.strftime("%Y-%m-%d %H:%M:%S %z")] if lastrun
90
+ end
91
+
92
+ @config.update_lastrun(profile_name)
93
+
94
+ tf = build_exclude_file(profile["exclude"] || [])
95
+
96
+ args += ["--exclude-from", tf.path]
97
+ args += (@config.profile(profile_name)["include"] || ["."]).map do |str|
98
+ expand_string(str)
99
+ end
100
+
101
+ begin
102
+ Open3.popen3(*args) do |stdin, stdout, stderr, wait_thr|
103
+ stdin.close
104
+
105
+ while wait_thr.status
106
+ r, w, e = IO.select([stdout, stderr])
107
+ r.each do |stream|
108
+ print stream.read
109
+ end
110
+ end
111
+
112
+ wait_thr.join
113
+ print stdout.read
114
+ print stderr.read
115
+ end
116
+ ensure
117
+ tf.unlink
118
+ end
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bup
4
+ VERSION = "0.1.0"
5
+ end
data/lib/bup.rb ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "bup/version"
4
+ require_relative "bup/config"
5
+ require_relative "bup/tar"
6
+
7
+ module Bup
8
+ class Error < StandardError; end
9
+ # Your code goes here...
10
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bup
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sam Baskinger
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-11-11 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Backup tool driver that connects tar, gnupg, and other tools to accomplish
14
+ backups.
15
+ email:
16
+ - basking2@yahoo.com
17
+ executables:
18
+ - bup
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - ".rspec"
23
+ - ".rubocop.yml"
24
+ - CHANGELOG.md
25
+ - Gemfile
26
+ - Gemfile.lock
27
+ - LICENSE.txt
28
+ - README.md
29
+ - Rakefile
30
+ - bin/console
31
+ - bin/setup
32
+ - bup.gemspec
33
+ - buprc
34
+ - exe/bup
35
+ - lib/bup.rb
36
+ - lib/bup/config.rb
37
+ - lib/bup/tar.rb
38
+ - lib/bup/version.rb
39
+ homepage: https://rubygems.org/gems/bup
40
+ licenses:
41
+ - MIT
42
+ metadata:
43
+ homepage_uri: https://rubygems.org/gems/bup
44
+ source_code_uri: https://gitlab.com/basking2/bup
45
+ changelog_uri: https://gitlab.com/basking2/bup/-/blob/main/CHANGELOG.md
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 3.0.2
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubygems_version: 3.2.22
62
+ signing_key:
63
+ specification_version: 4
64
+ summary: Backup tool driver.
65
+ test_files: []