fs3_cloner 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
+ SHA1:
3
+ metadata.gz: 45a407c2fe438966d410c13761cb0d3ae6d481b6
4
+ data.tar.gz: 0e95f66ee1d328533c572d32dd19e551dddbacfd
5
+ SHA512:
6
+ metadata.gz: 1d00c6944b50d9bd2f56edbfd3b449bf066fe4a8ba1a615e3eb1ec9a09e88909251141fdebb80157d23af7186fed31f25a5a4b658d9fcbc05361291636adca02
7
+ data.tar.gz: ac906bbe48c00ca56f3347d3c57109228863440ed3f4d3d44cd30fa14eeda721767d98914d5bad4cdc9baf208293f39b142cd217e16ee44efeeb41c081b91b89
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.0
5
+ before_install: gem install bundler -v 1.16.0
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in fs3_cloner.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,45 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ fs3_cloner (0.1.0)
5
+ aws-sdk (~> 2)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ aws-sdk (2.11.65)
11
+ aws-sdk-resources (= 2.11.65)
12
+ aws-sdk-core (2.11.65)
13
+ aws-sigv4 (~> 1.0)
14
+ jmespath (~> 1.0)
15
+ aws-sdk-resources (2.11.65)
16
+ aws-sdk-core (= 2.11.65)
17
+ aws-sigv4 (1.0.2)
18
+ diff-lcs (1.3)
19
+ jmespath (1.4.0)
20
+ rake (10.5.0)
21
+ rspec (3.7.0)
22
+ rspec-core (~> 3.7.0)
23
+ rspec-expectations (~> 3.7.0)
24
+ rspec-mocks (~> 3.7.0)
25
+ rspec-core (3.7.1)
26
+ rspec-support (~> 3.7.0)
27
+ rspec-expectations (3.7.0)
28
+ diff-lcs (>= 1.2.0, < 2.0)
29
+ rspec-support (~> 3.7.0)
30
+ rspec-mocks (3.7.0)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.7.0)
33
+ rspec-support (3.7.1)
34
+
35
+ PLATFORMS
36
+ ruby
37
+
38
+ DEPENDENCIES
39
+ bundler (~> 1.16)
40
+ fs3_cloner!
41
+ rake (~> 10.0)
42
+ rspec (~> 3.0)
43
+
44
+ BUNDLED WITH
45
+ 1.16.0
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Faizal Zakaria
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,50 @@
1
+ # Fs3Cloner
2
+
3
+ Clone your s3 bucket either for backup or migration to a new bucket in new AWS account etc.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'fs3_cloner'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install fs3_cloner
20
+
21
+ ## Usage
22
+
23
+ ```
24
+ Fs3Cloner::BucketCloner.new(
25
+ {
26
+ aws_access_key_id: '123',
27
+ aws_secret_access_key: '123',
28
+ bucket: 'bucket_from'
29
+ },
30
+ {
31
+ aws_access_key_id: '123',
32
+ aws_secret_access_key: '123',
33
+ bucket: 'bucket_to'
34
+ }
35
+ ).clone
36
+ ```
37
+
38
+ ## Development
39
+
40
+ 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.
41
+
42
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
43
+
44
+ ## Contributing
45
+
46
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/fs3_cloner.
47
+
48
+ ## License
49
+
50
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "fs3_cloner"
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(__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
@@ -0,0 +1,29 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "fs3_cloner/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "fs3_cloner"
8
+ spec.version = Fs3Cloner::VERSION
9
+ spec.authors = ["Faizal Zakaria"]
10
+ spec.email = ["fai@code3.io"]
11
+
12
+ spec.summary = %q{S3 cloner},
13
+ spec.description = %q{S3 cloner for you to backup your s3 bucket, clone it to a different env etc},
14
+ spec.homepage = "https://github.com/faizalzakaria/fs3_cloner",
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
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_runtime_dependency "aws-sdk", "~> 2"
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.16"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+ end
@@ -0,0 +1,167 @@
1
+ require 'aws-sdk'
2
+
3
+ # BucketCloner
4
+ #
5
+ # Ex.
6
+ #
7
+ # Fs3Cloner::BucketCloner.new(
8
+ # {
9
+ # aws_access_key_id: '123',
10
+ # aws_secret_access_key: '123',
11
+ # bucket: 'bucket_from'
12
+ # },
13
+ # {
14
+ # aws_access_key_id: '123',
15
+ # aws_secret_access_key: '123',
16
+ # bucket: 'bucket_to'
17
+ # }
18
+ # ).clone
19
+ module Fs3Cloner
20
+ class BucketCloner
21
+ attr_reader :from_bucket, :to_bucket, :logger
22
+ attr_reader :excludes, :includes, :prefixes, :copy_opts, :output
23
+
24
+ # options
25
+ # - output : STDOUT
26
+ # - includes : ex. [%r(uploads/document), "uploads/1/document.pdf"]
27
+ # - excludes : ex. [%r(uploads/1)]
28
+ # - prefixes : ex. ["uploads/document", "uploads/test"] , this will boost to cloning
29
+ def initialize(from_credentials, to_credentials, options = {})
30
+ parse_options(options)
31
+
32
+ @from_bucket = bucket_from_credentials(from_credentials)
33
+ @to_bucket = bucket_from_credentials(to_credentials)
34
+ @logger = Logger.new(output)
35
+ end
36
+
37
+ def clone
38
+ total_sync = 0
39
+ total_skip = 0
40
+
41
+ logger.info "Started syncing ..."
42
+
43
+ if prefixes.nil?
44
+ sync_count, skip_count = process_objects(from_bucket.objects)
45
+
46
+ total_sync += sync_count
47
+ total_skip += skip_count
48
+ else
49
+ prefixes.each do |prefix|
50
+ logger.info "Processing prefix #{prefix} ..."
51
+ sync_count, skip_count = process_objects(from_bucket.objects(prefix: prefix))
52
+
53
+ puts sync_count
54
+ puts skip_count
55
+ total_sync += sync_count
56
+ total_skip += skip_count
57
+ end
58
+ end
59
+
60
+ logger.info "Done!"
61
+ logger.info "Synced #{total_sync}"
62
+ logger.info "Skipped #{total_skip}"
63
+ end
64
+
65
+ private
66
+
67
+ def process_objects(objects)
68
+ sync_count = 0
69
+ skip_count = 0
70
+
71
+ objects.each do |object|
72
+ if object_needs_syncing?(object)
73
+ sync(object)
74
+ sync_count += 1
75
+ else
76
+ skip(object)
77
+ skip_count += 1
78
+ end
79
+ end
80
+
81
+ [sync_count, skip_count]
82
+ end
83
+
84
+ def parse_options(options)
85
+ @output = options[:output] || STDOUT
86
+ @excludes = options[:excludes]
87
+ @includes = options[:includes]
88
+ @prefixes = options[:prefixes]
89
+ @copy_opts = options[:copy_opts] || {}
90
+ end
91
+
92
+ def sync(object)
93
+ logger.debug "Syncing #{pp object}"
94
+ object.copy_to(to_bucket.object(object.key), copy_opts)
95
+ end
96
+
97
+ def skip(object)
98
+ logger.debug "Skipped #{pp object}"
99
+ end
100
+
101
+ def pp(object)
102
+ content_length_in_kb = object.content_length / 1024
103
+
104
+ [
105
+ %Q(#{object.key} #{content_length_in_kb}k),
106
+ %Q(#{object.last_modified.strftime("%b %d %Y %H:%M")})
107
+ ].join(' ')
108
+ end
109
+
110
+ def skip_object?(object)
111
+ false ||
112
+ object_include?(object) ||
113
+ object_exclude?(object)
114
+ end
115
+
116
+ def object_needs_syncing?(object)
117
+ to_object = to_bucket.object(object.key)
118
+
119
+ return false if skip_object?(object)
120
+ return true if !to_object.exists?
121
+ return to_object.etag != object.etag
122
+ end
123
+
124
+ def bucket_from_credentials(credentials)
125
+ client = Aws::S3::Client.new(access_key_id: credentials[:aws_access_key_id],
126
+ secret_access_key: credentials[:aws_secret_access_key])
127
+
128
+ bucket = Aws::S3::Bucket.new(credentials[:bucket], client: client)
129
+
130
+ return bucket if bucket.exists?
131
+
132
+ bucket.create
133
+ end
134
+
135
+ def object_include?(object)
136
+ return true if includes.nil?
137
+
138
+ includes.any? do |pattern|
139
+ if pattern.is_a?(Regexp)
140
+ match?(pattern, object.key)
141
+ elsif pattern.is_a?(String)
142
+ pattern == object.key
143
+ else
144
+ logger.error "Unknown include pattern, #{pattern}"
145
+ end
146
+ end
147
+ end
148
+
149
+ def object_exclude?(object)
150
+ return false if excludes.nil?
151
+
152
+ excludes.any? do |pattern|
153
+ if pattern.is_a?(Regexp)
154
+ match?(pattern, object.key)
155
+ elsif pattern.is_a?(String)
156
+ pattern == object.key
157
+ else
158
+ logger.error "Unknown include pattern, #{pattern}"
159
+ end
160
+ end
161
+ end
162
+
163
+ def match?(regex, string)
164
+ !regex.match(string).nil?
165
+ end
166
+ end
167
+ end
@@ -0,0 +1,3 @@
1
+ module Fs3Cloner
2
+ VERSION = "0.1.0"
3
+ end
data/lib/fs3_cloner.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "fs3_cloner/version"
2
+ require 'fs3_cloner/bucket_cloner'
3
+
4
+ module Fs3Cloner
5
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fs3_cloner
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Faizal Zakaria
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-06-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws-sdk
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: S3 cloner for you to backup your s3 bucket, clone it to a different env
70
+ etc
71
+ email:
72
+ - fai@code3.io
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - Gemfile.lock
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - bin/setup
87
+ - fs3_cloner.gemspec
88
+ - lib/fs3_cloner.rb
89
+ - lib/fs3_cloner/bucket_cloner.rb
90
+ - lib/fs3_cloner/version.rb
91
+ homepage: https://github.com/faizalzakaria/fs3_cloner
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.6.11
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: '["S3 cloner", "S3 cloner for you to backup your s3 bucket, clone it to a
115
+ different env etc", "https://github.com/faizalzakaria/fs3_cloner", "MIT"]'
116
+ test_files: []