bak 0.0.2
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.
- checksums.yaml +7 -0
- data/bin/bak +53 -0
- data/lib/bak.rb +5 -0
- data/lib/bak/backup_name_generator.rb +54 -0
- data/lib/bak/copier.rb +29 -0
- data/lib/bak/version.rb +3 -0
- metadata +51 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1d511bf4ec46e0a76e74b1ee7713a8b4d6cac462
|
4
|
+
data.tar.gz: 51b1ef8b67f7b88ddf4d40a5867dcabb6e5cfdc3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c6fa1b9ee4c596bd74572ee7b1778a83f418f5f56b60fcfd7ce17da15cffc8015f8437148623aba36ac813e9590cd136e47c0c0de55432f48f143020b6bb1f62
|
7
|
+
data.tar.gz: e3e2b8882d92c0d7435c0e88c52032c08a1900cdde1b05d0f958c0dca9367330c6269558f08eac96abddbe15368b463fabe7831922a950170e1d3c00c7049c4c
|
data/bin/bak
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
4
|
+
require 'bak'
|
5
|
+
require 'optparse'
|
6
|
+
|
7
|
+
options = {}
|
8
|
+
patternCount = 0
|
9
|
+
option_parser = OptionParser.new do |opts|
|
10
|
+
opts.on("-d", "--date") do
|
11
|
+
options[:date] = true
|
12
|
+
end
|
13
|
+
|
14
|
+
opts.on("-f", "--force") do
|
15
|
+
options[:force] = true
|
16
|
+
end
|
17
|
+
|
18
|
+
opts.on("-n", "--no-bak") do
|
19
|
+
options[:no_bak] = true
|
20
|
+
end
|
21
|
+
|
22
|
+
opts.on("-p POSTFIX") do |postfix|
|
23
|
+
options[:postfix] = postfix
|
24
|
+
end
|
25
|
+
|
26
|
+
opts.on("-s PREFIX") do |prefix|
|
27
|
+
options[:prefix] = prefix
|
28
|
+
end
|
29
|
+
|
30
|
+
opts.on("-R TEXT") do |text|
|
31
|
+
options[:replace] ||= {}
|
32
|
+
patternCount += 1
|
33
|
+
options[:replace][:pattern] = Regexp.new(text) if patternCount == 1
|
34
|
+
options[:replace][:replace] = text if patternCount == 2
|
35
|
+
end
|
36
|
+
end
|
37
|
+
option_parser.parse!
|
38
|
+
|
39
|
+
# set defaults for values that weren't provided
|
40
|
+
options[:force] ||= false
|
41
|
+
options[:no_bak] ||= false
|
42
|
+
|
43
|
+
if ARGV.length > 0
|
44
|
+
filenames = ARGV
|
45
|
+
else
|
46
|
+
filenames = STDIN.read().split("\n")
|
47
|
+
end
|
48
|
+
|
49
|
+
filenames.each do |filename|
|
50
|
+
generator = Bak::BackupNameGenerator.new(filename, options)
|
51
|
+
copier = Bak::FileCopier.new(generator, STDERR)
|
52
|
+
copier.start
|
53
|
+
end
|
data/lib/bak.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
module Bak
|
2
|
+
class BackupNameGenerator
|
3
|
+
attr_accessor :options, :filename
|
4
|
+
|
5
|
+
def initialize(filename, options_hash)
|
6
|
+
@filename = filename
|
7
|
+
@options = options_hash
|
8
|
+
end
|
9
|
+
|
10
|
+
def [](item)
|
11
|
+
@options[item]
|
12
|
+
end
|
13
|
+
|
14
|
+
def []=(k, v)
|
15
|
+
@options[k] = v
|
16
|
+
end
|
17
|
+
|
18
|
+
def start
|
19
|
+
filename = @filename
|
20
|
+
if @options != {}
|
21
|
+
if @options[:date] then filename = _with_date(filename) end
|
22
|
+
if @options[:postfix] then filename = _with_postfix(filename, @options[:postfix]) end
|
23
|
+
if @options[:prefix] then filename = _with_prefix(filename, @options[:prefix]) end
|
24
|
+
if @options[:replace] then filename = _with_replace(filename, @options[:replace]) end
|
25
|
+
end
|
26
|
+
unless @options[:no_bak] then filename = "#{filename}.bak" end
|
27
|
+
|
28
|
+
filename
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
def _with_date(filename)
|
33
|
+
return "#{filename}.#{_get_date}"
|
34
|
+
end
|
35
|
+
|
36
|
+
def _with_postfix(filename, postfix)
|
37
|
+
return "#{filename}_#{postfix}"
|
38
|
+
end
|
39
|
+
|
40
|
+
def _with_prefix(filename, prefix)
|
41
|
+
return "#{prefix}_#{filename}"
|
42
|
+
end
|
43
|
+
|
44
|
+
def _with_replace(filename, replace_info)
|
45
|
+
pattern = replace_info[:pattern]
|
46
|
+
replace = replace_info[:replace]
|
47
|
+
return filename.gsub(pattern, replace)
|
48
|
+
end
|
49
|
+
|
50
|
+
def _get_date
|
51
|
+
return Time.new.strftime("%Y-%m-%d")
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
data/lib/bak/copier.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
module Bak
|
2
|
+
class FileCopier
|
3
|
+
attr_accessor :generator, :start_file, :end_file
|
4
|
+
def initialize(backupNameGenerator, output_stream)
|
5
|
+
@generator = backupNameGenerator
|
6
|
+
@output = output_stream
|
7
|
+
@start_file = @generator.filename
|
8
|
+
@end_file = @generator.start
|
9
|
+
end
|
10
|
+
|
11
|
+
def check_errors
|
12
|
+
unless File.exists?(@start_file)
|
13
|
+
return "#{start_file}: No such file or directory"
|
14
|
+
end
|
15
|
+
if File.exists?(@end_file) && !@generator[:force]
|
16
|
+
return "#{end_file}: File already exists"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def start
|
21
|
+
errors = check_errors
|
22
|
+
if errors
|
23
|
+
@output.puts errors
|
24
|
+
else
|
25
|
+
FileUtils.cp_r(start_file, end_file)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/lib/bak/version.rb
ADDED
metadata
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bak
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alex Freidah
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-12-09 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: quickly create backup files and directories with extensions, prefixes,
|
14
|
+
replacements, and other patterns
|
15
|
+
email: alex.freidah@gmail.com
|
16
|
+
executables:
|
17
|
+
- bak
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/bak.rb
|
22
|
+
- lib/bak/copier.rb
|
23
|
+
- lib/bak/backup_name_generator.rb
|
24
|
+
- lib/bak/version.rb
|
25
|
+
- bin/bak
|
26
|
+
homepage: http://bak.com
|
27
|
+
licenses:
|
28
|
+
- MIT
|
29
|
+
metadata: {}
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
require_paths:
|
33
|
+
- lib
|
34
|
+
- lib/bak
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
requirements: []
|
46
|
+
rubyforge_project:
|
47
|
+
rubygems_version: 2.1.11
|
48
|
+
signing_key:
|
49
|
+
specification_version: 4
|
50
|
+
summary: small utility for creating named backup files and directories
|
51
|
+
test_files: []
|