backup-agent 1.0.0 → 1.0.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 30d19db4d2b7b5ccd2d5b22ba4984b12cbf2d91b
4
- data.tar.gz: eca7fd9183ba78f8b1bee805167ffcb30c967548
3
+ metadata.gz: 1689395dfc68dc2a53506dc5f7d450b0b3fbdceb
4
+ data.tar.gz: d68b183bcec4115436be20223d4aa91e4f4c3686
5
5
  SHA512:
6
- metadata.gz: 580cb22de5ecfdeb372e1b2fa163d979123f2099da0f4dd4b0d17105f8acd739627e5b6645db613fdae6d3a5ec8e624897a377edde1421924a1999506e09f293
7
- data.tar.gz: 5656f4668372fff2b3d1be5dedeb644aa97a26d672a9d90c52d21e5897512125ac2ef36ac077883a212ea138b5590d5a09d54b0f381f0c9f7e960c660c81df56
6
+ metadata.gz: 23ba26f5c0737f829ed774c6c1044333198c1bc2a74e90fc9c9fda504be0c53ea8158fef5d8a84b326c99ff9748a1670ca24841f3315288fe9241eda7a48e8b5
7
+ data.tar.gz: a246424a65914411b5f555a5d05afd5e614d7c6cd7c984c8e88ff2117fd1482ebd5983d1ff004b1416add2436559e59d7b12db073650cda7c74125a7a95853bf
data/backup-agent.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'backup-agent'
5
- s.version = '1.0.0'
5
+ s.version = '1.0.1'
6
6
  s.authors = ['Yaroslav Konoplov']
7
7
  s.email = ['yaroslav@inbox.com']
8
8
  s.summary = 'Easy AWS S3 backup'
data/lib/backup-agent.rb CHANGED
@@ -1,19 +1,19 @@
1
1
  puts "Ruby version #{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"
2
2
 
3
- %w(rubygems aws-sdk fileutils confo-config shellwords).each { |lib| require lib }
3
+ %w(rubygems aws-sdk fileutils confo-config shellwords pry).each { |el| require(el) }
4
4
 
5
- require_relative 'backup-agent/s3_config'
6
- require_relative 'backup-agent/task_config'
7
- require_relative 'backup-agent/service'
8
- require_relative 'backup-agent/features'
9
- require_relative 'backup-agent/s3'
5
+ %w( abstract_storage abstract_storage_config abstract_storage_object
6
+ s3_storage s3_config s3_object
7
+ features task performer ).each { |el| require_relative("backup-agent/#{el}") }
10
8
 
11
- module BackupAgent
9
+ module Backup
12
10
  class << self
13
- def perform_backup(&block)
14
- task_config = TaskConfig.new
15
- task_config.configure(&block) if block
16
- Service.new(task_config).perform_backup
11
+ def perform(storage, &block)
12
+ Performer.new.perform_backup(storage, Task.new(&block))
13
+ end
14
+
15
+ def features
16
+ @features ||= Features.new
17
17
  end
18
18
  end
19
19
  end
@@ -0,0 +1,32 @@
1
+ module Backup
2
+ class AbstractStorage
3
+ include Enumerable
4
+
5
+ attr_reader :config, :env
6
+
7
+ def initialize(config, env = {})
8
+ @config = config
9
+ @env = env
10
+ end
11
+
12
+ def open
13
+
14
+ end
15
+
16
+ def close
17
+
18
+ end
19
+
20
+ def upload(key, path)
21
+
22
+ end
23
+
24
+ def delete(key)
25
+
26
+ end
27
+
28
+ def each
29
+
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,5 @@
1
+ module Backup
2
+ class AbstractStorageConfig < Confo::Config
3
+
4
+ end
5
+ end
@@ -0,0 +1,19 @@
1
+ module Backup
2
+ class AbstractStorageObject
3
+ attr_reader :storage, :key, :env
4
+
5
+ def initialize(storage, key, env = {})
6
+ @storage = storage
7
+ @key = key
8
+ @env = env
9
+ end
10
+
11
+ def last_modified
12
+
13
+ end
14
+
15
+ def delete
16
+
17
+ end
18
+ end
19
+ end
@@ -1,5 +1,10 @@
1
- module BackupAgent
2
- class << self
1
+ module Backup
2
+ class Features
3
+ def initialize
4
+ check_mysql
5
+ check_mongodb
6
+ end
7
+
3
8
  def check_mysql
4
9
  if @mysql_check.nil?
5
10
  @mysql_check = system('/usr/bin/env mysql --version') ? true : (puts('MySQL is not installed'); false)
@@ -14,18 +19,11 @@ module BackupAgent
14
19
  @mongodb_check
15
20
  end
16
21
 
17
- def check_features
18
- check_mysql
19
- check_mongodb
20
- end
21
-
22
22
  def mysql_installed?
23
- check_features
24
23
  !!@mysql_check
25
24
  end
26
25
 
27
26
  def mongodb_installed?
28
- check_features
29
27
  !!@mongodb_check
30
28
  end
31
29
  end
@@ -1,22 +1,22 @@
1
- module BackupAgent
2
- class Service
3
- attr_reader :config
1
+ module Backup
2
+ class Performer
3
+ attr_reader :storage, :config
4
4
 
5
- def initialize(config)
5
+ def perform_backup(storage, config)
6
+ @storage = storage
6
7
  @config = config
7
- end
8
-
9
- def perform_backup
10
8
  @started_at = Time.now.utc
11
9
  @timestamp = @started_at.strftime('%s - %A %d %B %Y %H:%M')
10
+ storage.open
12
11
  make_tmp_dir
13
- backup_mysql if BackupAgent.mysql_installed?
14
- backup_mongodb if BackupAgent.mongodb_installed?
12
+ backup_mysql if Backup.features.mysql_installed?
13
+ backup_mongodb if Backup.features.mongodb_installed?
15
14
  backup_directories
16
15
  backup_files
17
16
  cleanup_old_backups
18
17
  ensure
19
18
  remove_tmp_dir
19
+ storage.close
20
20
  end
21
21
 
22
22
  protected
@@ -27,9 +27,7 @@ module BackupAgent
27
27
  cmd = "cd #{dir} && /usr/bin/env tar -czf #{tmp_path}/#{dir_filename} ."
28
28
  puts "Exec #{cmd}"
29
29
  system(cmd)
30
-
31
- obj = BackupAgent.s3.bucket(config.s3_bucket).object("#{@timestamp}/#{dir_filename}")
32
- obj.upload_file("#{tmp_path}/#{dir_filename}")
30
+ storage.upload("#{@timestamp}/#{dir_filename}", "#{tmp_path}/#{dir_filename}")
33
31
  end
34
32
  end
35
33
 
@@ -45,8 +43,7 @@ module BackupAgent
45
43
  cmd = "cd #{files_tmp_path} && /usr/bin/env tar -czf #{tmp_path}/#{file_bunch_name} ."
46
44
  system(cmd)
47
45
 
48
- obj = BackupAgent.s3.bucket(config.s3_bucket).object("#{@timestamp}/#{file_bunch_name}")
49
- obj.upload_file("#{tmp_path}/#{file_bunch_name}")
46
+ storage.upload("#{@timestamp}/#{file_bunch_name}", "#{tmp_path}/#{file_bunch_name}")
50
47
  ensure
51
48
  FileUtils.remove_dir(files_tmp_path)
52
49
  end
@@ -56,14 +53,13 @@ module BackupAgent
56
53
  def backup_mysql
57
54
  config.get(:mysql_databases).each do |db|
58
55
  db_filename = "mysql-#{db}.gz"
59
- dump = "/usr/bin/env mysqldump #{config.get(:mysql_connect)} #{config.get(:mysqldump_options).join(' ')} #{db}"
60
- gzip = "/usr/bin/env gzip -5 -c > #{tmp_path}/#{db_filename}"
56
+ dump = with_env "mysqldump #{config.get(:mysql_connect)} #{config.get(:mysqldump_options).join(' ')} #{db}"
57
+ gzip = with_env "gzip -5 -c > #{tmp_path}/#{db_filename}"
61
58
 
62
59
  puts "Exec #{dump} | #{gzip}"
63
60
  system "#{dump} | #{gzip}"
64
61
 
65
- obj = BackupAgent.s3.bucket(config.s3_bucket).object("#{@timestamp}/#{db_filename}")
66
- obj.upload_file("#{tmp_path}/#{db_filename}")
62
+ storage.upload("#{@timestamp}/#{db_filename}", "#{tmp_path}/#{db_filename}")
67
63
  end
68
64
  end
69
65
 
@@ -72,16 +68,15 @@ module BackupAgent
72
68
  FileUtils.mkdir_p(mongo_dump_dir)
73
69
 
74
70
  config.get(:mongo_databases).each do |db|
75
- db_filename = "mongo-#{db}.tgz"
76
- dump = "/usr/bin/env mongodump #{config.get(:mongo_connect)} -d #{db} -o #{mongo_dump_dir}"
71
+ db_filename = "mongo-#{db}.tar.gz"
72
+ dump = with_env "mongodump #{config.get(:mongo_connect)} -d #{db} -o #{mongo_dump_dir}"
77
73
  cd = "cd #{mongo_dump_dir}/#{db}"
78
- tar = "/usr/bin/env tar -czf #{tmp_path}/#{db_filename} ."
74
+ tar = with_env "tar -czf #{tmp_path}/#{db_filename} ."
79
75
 
80
76
  puts "Exec #{dump} && #{cd} && #{tar}"
81
77
  system "#{dump} && #{cd} && #{tar}"
82
78
 
83
- obj = BackupAgent.s3.bucket(config.s3_bucket).object("#{@timestamp}/#{db_filename}")
84
- obj.upload_file("#{tmp_path}/#{db_filename}")
79
+ storage.upload("#{@timestamp}/#{db_filename}", "#{tmp_path}/#{db_filename}")
85
80
  end
86
81
  ensure
87
82
  FileUtils.remove_dir(mongo_dump_dir)
@@ -89,8 +84,8 @@ module BackupAgent
89
84
 
90
85
  def cleanup_old_backups
91
86
  cutoff_date = Time.now.utc.to_i - (config.get(:days_to_keep_backups) * 86400)
92
- BackupAgent.s3.bucket(config.s3_bucket).objects.each do |o|
93
- o.delete if o.last_modified.to_i < cutoff_date
87
+ storage.each do |obj|
88
+ obj.delete if obj.last_modified.to_i < cutoff_date
94
89
  end
95
90
  end
96
91
 
@@ -105,5 +100,9 @@ module BackupAgent
105
100
  def tmp_path
106
101
  "/tmp/backup-agent-#{@started_at.strftime('%d-%m-%Y-%H:%M')}"
107
102
  end
103
+
104
+ def with_env(cmd)
105
+ "/usr/bin/env #{cmd}"
106
+ end
108
107
  end
109
108
  end
@@ -1,10 +1,11 @@
1
- module BackupAgent
2
- class S3Config < Confo::Config
1
+ module Backup
2
+ class S3Config < AbstractStorageConfig
3
3
  def initialize(*)
4
- super
5
4
  set :access_key_id, nil
6
5
  set :secret_access_key, nil
7
6
  set :region, nil
7
+ set :bucket, nil
8
+ super
8
9
  end
9
10
  end
10
11
  end
@@ -0,0 +1,17 @@
1
+ module Backup
2
+ class S3Object < AbstractStorageObject
3
+ def initialize(*)
4
+ super
5
+ @object = env.fetch(:object)
6
+ @bucket = env.fetch(:bucket)
7
+ end
8
+
9
+ def last_modified
10
+ @object.last_modified
11
+ end
12
+
13
+ def delete
14
+ storage.delete(key)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,45 @@
1
+ module Backup
2
+ class S3Storage < AbstractStorage
3
+
4
+ def initialize(*)
5
+ super
6
+ @bucket_name = env.fetch(:bucket)
7
+ end
8
+
9
+ def s3
10
+ @s3 ||= begin
11
+ Aws.config.update(
12
+ region: config.region,
13
+ credentials: Aws::Credentials.new(config.access_key_id, config.secret_access_key)
14
+ )
15
+ Aws::S3::Resource.new
16
+ end
17
+ end
18
+
19
+ def bucket
20
+ s3.bucket(@bucket_name)
21
+ end
22
+
23
+ def open
24
+ s3
25
+ end
26
+
27
+ def upload(key, path)
28
+ bucket.object(key).upload_file(path)
29
+ end
30
+
31
+ def delete(key)
32
+ bucket.object(key).delete
33
+ end
34
+
35
+ def object(key)
36
+ S3Object.new(self, key, object: bucket.object(key), bucket: bucket)
37
+ end
38
+
39
+ def each
40
+ bucket.objects.each do |s3_obj|
41
+ yield S3Object.new(self, s3_obj.key, object: s3_obj, bucket: bucket)
42
+ end
43
+ end
44
+ end
45
+ end
@@ -1,5 +1,5 @@
1
- module BackupAgent
2
- class TaskConfig < Confo::Config
1
+ module Backup
2
+ class Task < Confo::Config
3
3
  def initialize(*)
4
4
  super
5
5
  set :mysql_user, 'root'
@@ -47,8 +47,6 @@ module BackupAgent
47
47
 
48
48
  set :files, {}
49
49
 
50
- set :s3_bucket, nil
51
-
52
50
  set :days_to_keep_backups, 30
53
51
  end
54
52
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: backup-agent
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yaroslav Konoplov
@@ -66,11 +66,15 @@ files:
66
66
  - Rakefile
67
67
  - backup-agent.gemspec
68
68
  - lib/backup-agent.rb
69
+ - lib/backup-agent/abstract_storage.rb
70
+ - lib/backup-agent/abstract_storage_config.rb
71
+ - lib/backup-agent/abstract_storage_object.rb
69
72
  - lib/backup-agent/features.rb
70
- - lib/backup-agent/s3.rb
73
+ - lib/backup-agent/performer.rb
71
74
  - lib/backup-agent/s3_config.rb
72
- - lib/backup-agent/service.rb
73
- - lib/backup-agent/task_config.rb
75
+ - lib/backup-agent/s3_object.rb
76
+ - lib/backup-agent/s3_storage.rb
77
+ - lib/backup-agent/task.rb
74
78
  homepage: http://github.com/yivo/backup-agent
75
79
  licenses:
76
80
  - MIT
@@ -1,22 +0,0 @@
1
- module BackupAgent
2
- class << self
3
- def s3_config
4
- @s3_config ||= S3Config.new
5
- end
6
-
7
- def configure_s3(&block)
8
- @s3 = nil
9
- s3_config.configure(&block)
10
- end
11
-
12
- def s3
13
- @s3 ||= begin
14
- Aws.config.update(
15
- region: s3_config.region,
16
- credentials: Aws::Credentials.new(s3_config.access_key_id, s3_config.secret_access_key)
17
- )
18
- Aws::S3::Resource.new
19
- end
20
- end
21
- end
22
- end