s3-mysql-backup 1.0.4 → 1.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/s3_mysql_backup.rb +11 -7
- data/lib/s3utils.rb +6 -9
- data/spec/s3_mysql_backup_spec.rb +53 -0
- data/spec/s3utils_spec.rb +7 -0
- data/spec/spec_helper.rb +9 -0
- metadata +56 -2
data/lib/s3_mysql_backup.rb
CHANGED
@@ -18,16 +18,24 @@ class S3MysqlBackup
|
|
18
18
|
def run
|
19
19
|
ensure_backup_dir_exists
|
20
20
|
|
21
|
-
|
21
|
+
connect_to_s3
|
22
22
|
|
23
|
-
@dumpfile = dump_db
|
24
23
|
remove_old_backups
|
25
|
-
|
24
|
+
|
25
|
+
mail_notification(dump_db)
|
26
26
|
end
|
27
27
|
|
28
28
|
|
29
29
|
protected
|
30
30
|
|
31
|
+
def config
|
32
|
+
@s3config ||= YAML::load_file(@path_to_config)
|
33
|
+
end
|
34
|
+
|
35
|
+
def connect_to_s3
|
36
|
+
@s3utils ||= S3Utils.new(config['s3_access_key_id'], config['s3_secret_access_key'], config['s3_bucket'])
|
37
|
+
end
|
38
|
+
|
31
39
|
# make the DB backup file
|
32
40
|
def dump_db
|
33
41
|
filename = Time.now.strftime("#{@backup_dir}/#{@db_name}.%Y%m%d.%H%M%S.sql.gz")
|
@@ -70,10 +78,6 @@ class S3MysqlBackup
|
|
70
78
|
end
|
71
79
|
end
|
72
80
|
|
73
|
-
def config
|
74
|
-
@s3config ||= YAML::load_file(@path_to_config)
|
75
|
-
end
|
76
|
-
|
77
81
|
# remove old backups
|
78
82
|
# - keep 30 days complete
|
79
83
|
# - keep 90 days weekly beyond that
|
data/lib/s3utils.rb
CHANGED
@@ -1,8 +1,5 @@
|
|
1
1
|
require 'aws/s3'
|
2
2
|
|
3
|
-
include AWS::S3
|
4
|
-
|
5
|
-
|
6
3
|
# wrapper for S3 operations
|
7
4
|
#
|
8
5
|
class S3Utils
|
@@ -15,19 +12,19 @@ class S3Utils
|
|
15
12
|
end
|
16
13
|
|
17
14
|
def copy(from, to)
|
18
|
-
S3Object.copy(File.basename(from), File.basename(to), @bucket)
|
15
|
+
AWS::S3::S3Object.copy(File.basename(from), File.basename(to), @bucket)
|
19
16
|
end
|
20
17
|
|
21
18
|
def store(file_path)
|
22
|
-
S3Object.store(File.basename(file_path), open(file_path), @bucket)
|
19
|
+
AWS::S3::S3Object.store(File.basename(file_path), open(file_path), @bucket)
|
23
20
|
end
|
24
21
|
|
25
22
|
def delete(file_path)
|
26
|
-
S3Object.delete(File.basename(file_path), @bucket)
|
23
|
+
AWS::S3::S3Object.delete(File.basename(file_path), @bucket)
|
27
24
|
end
|
28
25
|
|
29
26
|
def list
|
30
|
-
Bucket.find(@bucket).objects.each do |obj|
|
27
|
+
AWS::S3::Bucket.find(@bucket).objects.each do |obj|
|
31
28
|
puts "#{obj.bucket.name}/#{obj.key}"
|
32
29
|
end
|
33
30
|
end
|
@@ -42,10 +39,10 @@ class S3Utils
|
|
42
39
|
end
|
43
40
|
|
44
41
|
def ensure_bucket_exists
|
45
|
-
Bucket.find(@bucket)
|
42
|
+
AWS::S3::Bucket.find(@bucket)
|
46
43
|
|
47
44
|
rescue AWS::S3::NoSuchBucket
|
48
|
-
Bucket.create(@bucket)
|
45
|
+
AWS::S3::Bucket.create(@bucket)
|
49
46
|
end
|
50
47
|
|
51
48
|
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe S3MysqlBackup do
|
4
|
+
|
5
|
+
before :each do
|
6
|
+
@config = YAML::load_file(File.dirname(__FILE__) + '/s3_mysql_backup.yml')
|
7
|
+
stub.instance_of(S3MysqlBackup).config.times(any_times){ @config }
|
8
|
+
|
9
|
+
stub.instance_of(S3MysqlBackup).ensure_backup_dir_exists.times(any_times){ true }
|
10
|
+
|
11
|
+
@dump_file_name = "/tmp/test.20130101.010101.sql.gz"
|
12
|
+
stub.instance_of(S3MysqlBackup).dump_db.times(any_times){ @dump_file_name }
|
13
|
+
|
14
|
+
mock_stats = Object.new
|
15
|
+
stub(File).stat.with_any_args.times(any_times){ mock_stats }
|
16
|
+
stub(mock_stats).size{ 1024 }
|
17
|
+
|
18
|
+
mock_utils = Object.new
|
19
|
+
stub(S3Utils).new.with_any_args.times(any_times){ mock_utils }
|
20
|
+
stub.instance_of(S3MysqlBackup).connect_to_s3.times(any_times){ mock_utils }
|
21
|
+
|
22
|
+
stub.instance_of(Net::SMTP).enable_starttls.times(any_times)
|
23
|
+
stub.instance_of(Net::SMTP).start.with_any_args.times(any_times){ true }
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "#run" do
|
27
|
+
it "should call ensure_backup_dir_exists" do
|
28
|
+
stub.instance_of(S3MysqlBackup).ensure_backup_dir_exists.times(1)
|
29
|
+
S3MysqlBackup.new('test', @config_path).run
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should call connect_to_s3" do
|
33
|
+
stub.instance_of(S3MysqlBackup).connect_to_s3.times(1)
|
34
|
+
S3MysqlBackup.new('test', @config_path).run
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should call remove_old_backups" do
|
38
|
+
stub.instance_of(S3MysqlBackup).remove_old_backups.times(1)
|
39
|
+
S3MysqlBackup.new('test', @config_path).run
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should call dump_db" do
|
43
|
+
stub.instance_of(S3MysqlBackup).dump_db.times(1){ @dump_file_name }
|
44
|
+
S3MysqlBackup.new('test', @config_path).run
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should call mail_notification" do
|
48
|
+
stub.instance_of(S3MysqlBackup).mail_notification.times(1)
|
49
|
+
S3MysqlBackup.new('test', @config_path).run
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: s3-mysql-backup
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -27,6 +27,54 @@ dependencies:
|
|
27
27
|
- - ! '>='
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '2.12'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '2.12'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rr
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 1.0.5
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 1.0.5
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: fakefs
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 0.4.2
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 0.4.2
|
30
78
|
description: A simple mysql backup to Amazon S3
|
31
79
|
email: jeff@7compass.com
|
32
80
|
executables:
|
@@ -38,6 +86,9 @@ files:
|
|
38
86
|
- lib/s3_mysql_backup.rb
|
39
87
|
- lib/s3utils.rb
|
40
88
|
- README.md
|
89
|
+
- spec/s3_mysql_backup_spec.rb
|
90
|
+
- spec/s3utils_spec.rb
|
91
|
+
- spec/spec_helper.rb
|
41
92
|
- bin/s3-mysql-backup
|
42
93
|
homepage: https://github.com/7compass/s3-mysql-backup
|
43
94
|
licenses: []
|
@@ -64,4 +115,7 @@ rubygems_version: 1.8.24
|
|
64
115
|
signing_key:
|
65
116
|
specification_version: 3
|
66
117
|
summary: Simple mysql backup to S3
|
67
|
-
test_files:
|
118
|
+
test_files:
|
119
|
+
- spec/s3_mysql_backup_spec.rb
|
120
|
+
- spec/s3utils_spec.rb
|
121
|
+
- spec/spec_helper.rb
|