handy 0.0.3 → 0.0.4
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/README.md +11 -0
- data/lib/handy/dump2s3.rb +80 -3
- data/lib/handy/s3.rb +0 -52
- data/lib/handy/tasks.rb +17 -0
- data/lib/handy/version.rb +1 -1
- data/lib/handy.rb +3 -0
- metadata +3 -3
data/README.md
CHANGED
@@ -1,11 +1,22 @@
|
|
1
1
|
# handy provides follwing tools
|
2
2
|
|
3
|
+
##rake handy:db:backup##
|
4
|
+
Creates a dump of data and structure which can been safely backed up
|
5
|
+
|
3
6
|
##rake handy:db:restore file=xyz.sql.gz##
|
4
7
|
restores the data and structure from file
|
5
8
|
|
6
9
|
##rake handy:db:db2db##
|
7
10
|
restores the data from production database to staging database
|
8
11
|
|
12
|
+
##rake handy:db:dump2s3##
|
13
|
+
Creates a backup and then stores that backup on s3
|
14
|
+
|
15
|
+
##rake handy:db:dump2s3:list##
|
16
|
+
Prints a list of all files stored on s3
|
17
|
+
|
18
|
+
##rake handy:db:dump2s3:restore file=xxxx.sql.gz##
|
19
|
+
Restores the database with the data from s3.
|
9
20
|
|
10
21
|
|
11
22
|
Copyright (c) 2010 Neeraj Singh. See LICENSE for details.
|
data/lib/handy/dump2s3.rb
CHANGED
@@ -1,9 +1,86 @@
|
|
1
|
-
|
1
|
+
module Handy
|
2
|
+
class S3
|
3
|
+
|
4
|
+
attr_accessor :bucket_name, :access_key_id, :secret_access_key, :bucket_instance
|
5
|
+
|
6
|
+
def initialize(env)
|
7
|
+
file = Rails.root.join('config', 'amazon_s3.yml')
|
8
|
+
unless File.exists?(file)
|
9
|
+
raise "file config/amazon_s3.yml was not found. Create a file as per http://gist.github.com/619432"
|
10
|
+
end
|
11
|
+
config = YAML.load_file(file)
|
12
|
+
@bucket_name = config[env]['bucket_name']
|
13
|
+
@access_key_id = config[env]['access_key_id']
|
14
|
+
@secret_access_key = config[env]['secret_access_key']
|
15
|
+
@s3_instance = Aws::S3.new(access_key_id, secret_access_key)
|
16
|
+
@bucket_instance = Aws::S3::Bucket.create(@s3_instance, bucket_name)
|
17
|
+
begin
|
18
|
+
@bucket_instance.keys
|
19
|
+
rescue
|
20
|
+
@bucket_instance = Aws::S3::Bucket.create(@s3_instance, bucket_name, true)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def list
|
25
|
+
@bucket_instance.keys
|
26
|
+
end
|
27
|
+
|
28
|
+
def store(file_name, file_data)
|
29
|
+
@bucket_instance.put(file_name, file_data)
|
30
|
+
end
|
31
|
+
|
32
|
+
def fetch(file_name)
|
33
|
+
connected
|
34
|
+
AWS::S3::S3Object.find(file_name, bucket)
|
35
|
+
|
36
|
+
file = Tempfile.new("dump")
|
37
|
+
open(file.path, 'w') do |f|
|
38
|
+
AWS::S3::S3Object.stream(file_name, bucket) do |chunk|
|
39
|
+
f.write chunk
|
40
|
+
end
|
41
|
+
end
|
42
|
+
file
|
43
|
+
end
|
44
|
+
|
45
|
+
|
46
|
+
def delete(file_name)
|
47
|
+
if object = AWS::S3::S3Object.find(file_name, bucket)
|
48
|
+
object.delete
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
2
56
|
|
3
57
|
module Handy
|
4
58
|
class Dump2s3
|
5
|
-
|
6
|
-
|
59
|
+
|
60
|
+
def self.run(env, file_name)
|
61
|
+
s3 = Handy::S3.new(Rails.env)
|
62
|
+
s3.store(file_name, open(Rails.root.join('tmp', file_name)))
|
63
|
+
Handy::Util.pretty_msg("#{file_name} has been backedup at s3.")
|
7
64
|
end
|
65
|
+
|
66
|
+
def self.list(env)
|
67
|
+
s3 = Handy::S3.new(Rails.env)
|
68
|
+
Handy::Util.pretty_msg("List of files on s3")
|
69
|
+
s3.list.each {|e| puts e}
|
70
|
+
end
|
71
|
+
|
72
|
+
def self.restore(env, file_name)
|
73
|
+
s3 = Handy::S3.new(Rails.env)
|
74
|
+
keyinfo = s3.bucket_instance.key(file_name)
|
75
|
+
raise "no file named #{file_name} was found on s3. Please check the file list on s3" if keyinfo.blank?
|
76
|
+
data = s3.bucket_instance.get(file_name)
|
77
|
+
storage_dir = Rails.root.join('tmp', file_name)
|
78
|
+
open(storage_dir, 'w') do |f|
|
79
|
+
f.write data
|
80
|
+
end
|
81
|
+
Handy::Util.pretty_msg("file #{file_name} has been downloaded to #{storage_dir.expand_path}")
|
82
|
+
end
|
83
|
+
|
8
84
|
end
|
85
|
+
|
9
86
|
end
|
data/lib/handy/s3.rb
CHANGED
@@ -1,52 +0,0 @@
|
|
1
|
-
module Handy
|
2
|
-
class S3
|
3
|
-
|
4
|
-
attr_accessor :bucket_name, :access_key_id, :secret_access_key
|
5
|
-
def initialize
|
6
|
-
config = YAML.load_file(Rails.root.join('config', 'amazon_s3.yml'))
|
7
|
-
unless File.exists?(config)
|
8
|
-
raise "file config/amazon_s3.yml was not found. Create a file as per http://gist.github.com/619432"
|
9
|
-
end
|
10
|
-
self.new(config[env]['username'], config[env]['password'], config[env]['database'])
|
11
|
-
@bucket_name = config[env]['bucket_name']
|
12
|
-
@access_key_id = config[env]['access_key_id']
|
13
|
-
@secret_access_key = config[env]['secret_access_key']
|
14
|
-
end
|
15
|
-
|
16
|
-
def connect
|
17
|
-
AWS::S3::Base.establish_connection!(access_key_id, secret_access_key)
|
18
|
-
AWS::S3::Bucket.create(bucket)
|
19
|
-
end
|
20
|
-
|
21
|
-
def store
|
22
|
-
connect
|
23
|
-
AWS::S3::S3Object.store(file_name, file, bucket)
|
24
|
-
end
|
25
|
-
|
26
|
-
def fetch(file_name)
|
27
|
-
connected
|
28
|
-
AWS::S3::S3Object.find(file_name, bucket)
|
29
|
-
|
30
|
-
file = Tempfile.new("dump")
|
31
|
-
open(file.path, 'w') do |f|
|
32
|
-
AWS::S3::S3Object.stream(file_name, bucket) do |chunk|
|
33
|
-
f.write chunk
|
34
|
-
end
|
35
|
-
end
|
36
|
-
file
|
37
|
-
end
|
38
|
-
|
39
|
-
def list
|
40
|
-
connect
|
41
|
-
AWS::S3::Bucket.find(bucket).objects.collect {|x| x.path }
|
42
|
-
end
|
43
|
-
|
44
|
-
def delete(file_name)
|
45
|
-
if object = AWS::S3::S3Object.find(file_name, bucket)
|
46
|
-
object.delete
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
end
|
51
|
-
|
52
|
-
end
|
data/lib/handy/tasks.rb
CHANGED
@@ -49,5 +49,22 @@ namespace :handy do
|
|
49
49
|
Handy::Dump2s3.run(Rails.env, file)
|
50
50
|
end
|
51
51
|
|
52
|
+
namespace :dump2s3 do
|
53
|
+
|
54
|
+
desc "list all files stored at s3"
|
55
|
+
task :list => :environment do
|
56
|
+
Handy::Dump2s3.list(Rails.env)
|
57
|
+
end
|
58
|
+
|
59
|
+
desc "restore data from s3"
|
60
|
+
task :restore => :environment do
|
61
|
+
file = ENV['file']
|
62
|
+
raise "No file was specified. Usage: rake handy:db:dump2s3:restore file=xxxx" if file.blank?
|
63
|
+
Handy::Dump2s3.restore(Rails.env, file)
|
64
|
+
Rake::Task["handy:db:restore"].invoke
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
|
52
69
|
end
|
53
70
|
end
|
data/lib/handy/version.rb
CHANGED
data/lib/handy.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: handy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 4
|
10
|
+
version: 0.0.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Neeraj Singh
|