encbs 0.1.0 → 0.1.1
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/VERSION +1 -1
- data/bin/encbs +18 -15
- data/encbs.gemspec +6 -2
- data/lib/backup/jar.rb +13 -3
- data/lib/backup.rb +12 -22
- data/lib/crypto.rb +24 -1
- data/test/fixtures/etc/.hide +1 -0
- data/test/fixtures/etc/root/file +1 -0
- data/test/fixtures/test_crypto.rb +23 -0
- data/test/test_backup.rb +21 -0
- data/test/test_jar.rb +0 -0
- metadata +6 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.1
|
data/bin/encbs
CHANGED
@@ -20,8 +20,6 @@ require 'backup'
|
|
20
20
|
opts = Slop.parse :help => true do
|
21
21
|
on :a, :add, "Add path to backup", true
|
22
22
|
on :b, :bucket, "Set Amazon S3 bucket to backup", true
|
23
|
-
on :k, :key, "Set API key to access Amazon S3", true
|
24
|
-
on :s, :secret, "Set API secret to access Amazon S3", true
|
25
23
|
on :c, :config, "Use config file to upload backup", true #TODO
|
26
24
|
on :colorize, "Colorize print to console"
|
27
25
|
on :d, :date, "Date for backup restore (default: last)", true
|
@@ -29,11 +27,14 @@ opts = Slop.parse :help => true do
|
|
29
27
|
on :h, :hostname, "Set hostname (default: system)", true
|
30
28
|
on :i, :increment, "Use increment mode for backup (default: false)"
|
31
29
|
on :j, :jar, "Versions of jar (option: hash or path)", true
|
32
|
-
on :
|
30
|
+
on :k, :key, "Set API key to access Amazon S3", true
|
33
31
|
on :l, :local, "Backup in local directory", true
|
34
32
|
on :list, "List of jars"
|
35
33
|
on :r, :rescue, "Return data from backup (option: jar, path or filter)", true
|
34
|
+
on :s, :secret, "Set API secret to access Amazon S3", true
|
36
35
|
on :t, :to, "Path to recovery (default: /)", true
|
36
|
+
on :token, "RSA Key to encrypt/decrypt backup data", true
|
37
|
+
on :threads, "Set count of thread (default: 1)", true
|
37
38
|
on :v, :verbose, "Verbose mode"
|
38
39
|
|
39
40
|
banner "Usage:\n $ encbs [options]\n\nOptions:"
|
@@ -48,16 +49,19 @@ end
|
|
48
49
|
$PRINT_VERBOSE = opts.verbose?
|
49
50
|
$COLORIZE = opts.colorize?
|
50
51
|
|
51
|
-
|
52
|
-
|
53
|
-
#
|
54
|
-
#
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
52
|
+
if opts.generate?
|
53
|
+
bits = opts[:generate].to_i
|
54
|
+
puts_fail "Unsupport #{bits} bits" unless bits == 4096 or bits == 2048
|
55
|
+
puts "Generate #{bits} bits RSA keys"
|
56
|
+
Crypto::create_keys(
|
57
|
+
File.join(Dir.getwd, "rsa_key"),
|
58
|
+
File.join(Dir.getwd, "rsa_key.pub"),
|
59
|
+
bits
|
60
|
+
)
|
61
|
+
puts "Done!"
|
62
|
+
|
63
|
+
exit
|
64
|
+
end
|
61
65
|
|
62
66
|
if opts.local?
|
63
67
|
try_create_dir opts[:local]
|
@@ -92,8 +96,7 @@ if opts.list?
|
|
92
96
|
exit
|
93
97
|
end
|
94
98
|
|
95
|
-
|
96
|
-
# @backup.key = opts[:key] if opts.key?
|
99
|
+
@backup.key = opts[:token] if opts.token?
|
97
100
|
|
98
101
|
if opts.date?
|
99
102
|
date = opts[:date].split("-")
|
data/encbs.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{encbs}
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Timothy Klim"]
|
@@ -35,10 +35,14 @@ Gem::Specification.new do |s|
|
|
35
35
|
"lib/backup/timestamp.rb",
|
36
36
|
"lib/crypto.rb",
|
37
37
|
"lib/helpers.rb",
|
38
|
+
"test/fixtures/etc/.hide",
|
39
|
+
"test/fixtures/etc/root/file",
|
40
|
+
"test/fixtures/test_crypto.rb",
|
38
41
|
"test/helper.rb",
|
39
42
|
"test/test_backup.rb",
|
40
43
|
"test/test_backup_file_item.rb",
|
41
|
-
"test/test_backup_timestamp.rb"
|
44
|
+
"test/test_backup_timestamp.rb",
|
45
|
+
"test/test_jar.rb"
|
42
46
|
]
|
43
47
|
s.homepage = %q{http://github.com/TimothyKlim/encbs}
|
44
48
|
s.licenses = ["MIT"]
|
data/lib/backup/jar.rb
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
module Backup
|
2
2
|
class Jar
|
3
|
-
def initialize(file_item, root_path, local_path)
|
3
|
+
def initialize(file_item, root_path, local_path, key = nil)
|
4
4
|
@root_path = root_path
|
5
5
|
@local_path = local_path
|
6
6
|
@timestamp = Backup::Timestamp.create
|
7
7
|
@file_item = file_item
|
8
|
+
@key = key
|
8
9
|
end
|
9
10
|
|
10
11
|
def jar_hash
|
@@ -73,8 +74,17 @@ module Backup
|
|
73
74
|
|
74
75
|
@local_files.keys.each do |file|
|
75
76
|
unless Dir.exists?(file)
|
76
|
-
|
77
|
-
|
77
|
+
data = if @key
|
78
|
+
@key.encrypt_to_stream(File.open(file).read)
|
79
|
+
else
|
80
|
+
File.open(file)
|
81
|
+
end
|
82
|
+
|
83
|
+
@file_item.create_file_once(
|
84
|
+
"#{jar_data_path}/#{@file_item.file_hash file}",
|
85
|
+
data
|
86
|
+
)
|
87
|
+
|
78
88
|
pbar.inc
|
79
89
|
end
|
80
90
|
end
|
data/lib/backup.rb
CHANGED
@@ -25,11 +25,11 @@ module Backup
|
|
25
25
|
end
|
26
26
|
|
27
27
|
def key=(path)
|
28
|
-
@key =
|
28
|
+
@key = Crypto::Key.from_file(path)
|
29
29
|
end
|
30
30
|
|
31
31
|
def create!(local_path, increment = false)
|
32
|
-
jar = Jar.new(@file_item, @root_path, local_path)
|
32
|
+
jar = Jar.new(@file_item, @root_path, local_path, @key)
|
33
33
|
jar.save(increment)
|
34
34
|
end
|
35
35
|
|
@@ -44,6 +44,12 @@ module Backup
|
|
44
44
|
def restore_jar_to(hash, timestamp, to)
|
45
45
|
files = Jar.fetch_index_for(@file_item, @root_path, hash, timestamp)
|
46
46
|
|
47
|
+
pbar = ProgressBar.new(
|
48
|
+
"Restoring",
|
49
|
+
files.keys.length
|
50
|
+
)
|
51
|
+
pbar.bar_mark = '*'
|
52
|
+
|
47
53
|
files.keys.sort.each do |file|
|
48
54
|
restore_file = File.join(to, file)
|
49
55
|
current_file = files[file]
|
@@ -76,6 +82,7 @@ module Backup
|
|
76
82
|
remote_path += "/#{@file_item.file_hash file}"
|
77
83
|
|
78
84
|
data = @file_item.read_file remote_path
|
85
|
+
data = @key.decrypt_from_stream data if @key
|
79
86
|
f.puts data
|
80
87
|
end
|
81
88
|
|
@@ -93,28 +100,11 @@ module Backup
|
|
93
100
|
puts_fail "Permission denied for #{restore_file.dark_green}"
|
94
101
|
end
|
95
102
|
end
|
103
|
+
|
104
|
+
pbar.inc
|
96
105
|
end
|
97
106
|
|
107
|
+
pbar.finish
|
98
108
|
end
|
99
109
|
end
|
100
|
-
|
101
|
-
def self.fetch_versions_of_backup(path)
|
102
|
-
Dir["#{path}/*"].map do |backup|
|
103
|
-
backup.match(/[0-9]{12}$/)[0] if backup.match(/[0-9]{12}$/)
|
104
|
-
end.compact.sort
|
105
|
-
end
|
106
|
-
|
107
|
-
def self.aes(command, key, data)
|
108
|
-
aes = OpenSSL::Cipher::Cipher.new('aes-256-cbc').send(command)
|
109
|
-
aes.key = key
|
110
|
-
aes.update(data) << aes.final
|
111
|
-
end
|
112
|
-
|
113
|
-
def self.encrypt_data(key, data)
|
114
|
-
Backup::aes(:encrypt, key, data) unless data.empty?
|
115
|
-
end
|
116
|
-
|
117
|
-
def self.decrypt_data(key, data)
|
118
|
-
Backup::aes(:decrypt, key, data)
|
119
|
-
end
|
120
110
|
end
|
data/lib/crypto.rb
CHANGED
@@ -3,7 +3,7 @@ require 'base64'
|
|
3
3
|
|
4
4
|
module Crypto
|
5
5
|
|
6
|
-
def self.create_keys(priv
|
6
|
+
def self.create_keys(priv, pub, bits)
|
7
7
|
private_key = OpenSSL::PKey::RSA.new(bits)
|
8
8
|
File.open(priv, "w+") { |fp| fp << private_key.to_s }
|
9
9
|
File.open(pub, "w+") { |fp| fp << private_key.public_key.to_s }
|
@@ -19,7 +19,30 @@ module Crypto
|
|
19
19
|
def self.from_file(filename)
|
20
20
|
self.new File.read( filename )
|
21
21
|
end
|
22
|
+
|
23
|
+
def encrypt_to_stream(data)
|
24
|
+
encrypt_data = StringIO.new
|
25
|
+
i = 0
|
26
|
+
|
27
|
+
while buf = data[i..(i+=117)] do
|
28
|
+
encrypt_data << encrypt(buf)
|
29
|
+
end
|
30
|
+
|
31
|
+
encrypt_data.seek(0)
|
32
|
+
encrypt_data
|
33
|
+
end
|
34
|
+
|
35
|
+
def decrypt_from_stream(data)
|
36
|
+
encrypt_data = StringIO.new data
|
37
|
+
decrypt_data = ""
|
22
38
|
|
39
|
+
while buf = encrypt_data.read(256) do
|
40
|
+
decrypt_data += decrypt(buf)
|
41
|
+
end
|
42
|
+
|
43
|
+
decrypt_data
|
44
|
+
end
|
45
|
+
|
23
46
|
def encrypt(text)
|
24
47
|
@key.send("#{key_type}_encrypt", text)
|
25
48
|
end
|
@@ -0,0 +1 @@
|
|
1
|
+
Hide data
|
@@ -0,0 +1 @@
|
|
1
|
+
Root file
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require File.expand_path('../helper', __FILE__)
|
2
|
+
|
3
|
+
class TestCrypto < Test::Unit::TestCase
|
4
|
+
def fixtures
|
5
|
+
File.expand_path('../fixtures/rsa_keys', __FILE__)
|
6
|
+
end
|
7
|
+
|
8
|
+
def create_keys!(bits = 4096)
|
9
|
+
Crypto.create_keys("#{fixtures}/rsa_key", "#{fixtures}/rsa_key.pub", bits)
|
10
|
+
end
|
11
|
+
|
12
|
+
def load_keys
|
13
|
+
@priv_key = Crypto::Key.from_file("#{fixtures}/rsa_key")
|
14
|
+
@pub_key = Crypto::Key.from_file("#{fixtures}/rsa_key.pub")
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_create_keys
|
18
|
+
create_keys!
|
19
|
+
|
20
|
+
assert_not_nil File.open("#{fixtures}/rsa_key").read
|
21
|
+
assert_not_nil File.open("#{fixtures}/rsa_key.pub").read
|
22
|
+
end
|
23
|
+
end
|
data/test/test_backup.rb
CHANGED
@@ -9,6 +9,27 @@ class TestBackup < Test::Unit::TestCase
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def test_backup_attributes
|
12
|
+
hostname = Socket.gethostname
|
13
|
+
file = File.expand_path("../fixtures/backups", __FILE__)
|
12
14
|
|
15
|
+
assert_equal(
|
16
|
+
@backup.root_path,
|
17
|
+
"#{file}/#{hostname}"
|
18
|
+
)
|
19
|
+
assert_equal @backup.hostname, Socket.gethostname
|
20
|
+
assert_equal @backup.file_item.class, Backup::FileItem::Local
|
21
|
+
assert_not_nil @backup.timestamp
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_create
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_show_jars
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_jar_versions
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_restore
|
13
34
|
end
|
14
35
|
end
|
data/test/test_jar.rb
ADDED
File without changes
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: encbs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.1.
|
5
|
+
version: 0.1.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Timothy Klim
|
@@ -105,10 +105,14 @@ files:
|
|
105
105
|
- lib/backup/timestamp.rb
|
106
106
|
- lib/crypto.rb
|
107
107
|
- lib/helpers.rb
|
108
|
+
- test/fixtures/etc/.hide
|
109
|
+
- test/fixtures/etc/root/file
|
110
|
+
- test/fixtures/test_crypto.rb
|
108
111
|
- test/helper.rb
|
109
112
|
- test/test_backup.rb
|
110
113
|
- test/test_backup_file_item.rb
|
111
114
|
- test/test_backup_timestamp.rb
|
115
|
+
- test/test_jar.rb
|
112
116
|
has_rdoc: true
|
113
117
|
homepage: http://github.com/TimothyKlim/encbs
|
114
118
|
licenses:
|
@@ -123,7 +127,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
123
127
|
requirements:
|
124
128
|
- - ">="
|
125
129
|
- !ruby/object:Gem::Version
|
126
|
-
hash:
|
130
|
+
hash: -2711619165124038743
|
127
131
|
segments:
|
128
132
|
- 0
|
129
133
|
version: "0"
|