ariranha 0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f80d87e69641df59e81524d908ffb4af892c3c50
4
+ data.tar.gz: b4aca1264eb8337ceb9ddd0a923e857e4d65f570
5
+ SHA512:
6
+ metadata.gz: dd495e0b7279832fcb1f6510eeeaaf2cd1b054e84e4d61c02d1ed188d4681aa08ea84e28fc4e073e397719cd041fa6da56b0ad8eb1357db71452ac9124e885a2
7
+ data.tar.gz: 6d807b45cf7d2c9e5acede2ff7ca69b53da37f266263b1664c93bf3307ae247cc7dd60327b9b88f5b0da561f9bc12e44567f28c3b77ddd3286ce4096c2245bdd
@@ -0,0 +1,6 @@
1
+ config.yml
2
+
3
+ .ruby-version
4
+ .ruby-gemset
5
+ Gemfile.lock
6
+ /pkg/
@@ -0,0 +1,4 @@
1
+ Documentation:
2
+ Enabled: false
3
+ Metrics/AbcSize:
4
+ Max: 20
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Artur Rodrigues
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,10 @@
1
+ <img alt="ariranha" src="ariranha.jpg" />
2
+
3
+ # Ariranha
4
+
5
+ Backs up databases and sends them to multiple clouds
6
+
7
+ # Credits
8
+
9
+ * Maintener: Artur Rodrigues <arturhoo@gmail.com>
10
+ * Drawing: Copyright [Ann Ranlet](http://annran.blogspot.com.br/2010/07/river-otter.html)
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ariranha/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'ariranha'
8
+ spec.version = Ariranha::VERSION
9
+ spec.authors = ['Artur Rodrigues']
10
+ spec.email = ['arturhoo@gmail.com']
11
+ spec.description = 'Backs up databases and sends them to multiple clouds'
12
+ spec.summary = 'Backs up databases and sends them to multiple clouds'
13
+ spec.homepage = 'https://github.com/arturhoo/ariranha'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.10'
22
+ spec.add_development_dependency 'pry-byebug'
23
+
24
+ spec.add_dependency 'fog'
25
+ end
Binary file
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ begin
4
+ require 'ariranha'
5
+ rescue LoadError
6
+ require 'rubygems'
7
+ require 'ariranha'
8
+ end
9
+
10
+ Ariranha.backup
@@ -0,0 +1,7 @@
1
+ require_relative 'ariranha/base'
2
+
3
+ module Ariranha
4
+ def self.backup
5
+ Base.new.backup
6
+ end
7
+ end
@@ -0,0 +1,42 @@
1
+ require_relative 'directory'
2
+ require_relative 'drivers/mysql'
3
+ require_relative 'drivers/postgresql'
4
+
5
+ module Ariranha
6
+ class Base
7
+ def initialize(config_file = 'config.yml')
8
+ @config = YAML.load(File.open(config_file))
9
+ config_instances
10
+ config_directories
11
+ end
12
+
13
+ def backup
14
+ instances.each do |instance|
15
+ filename = instance.backup
16
+ directories.each { |dir| dir.upload(filename, instance.database) }
17
+ puts "deleting /tmp/#{filename}..."
18
+ Open3.capture3 "rm -rf /tmp/#{filename}"
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ attr_reader :config, :instances, :directories
25
+
26
+ def config_instances
27
+ @instances = []
28
+ config['databases'].each do |driver, instances|
29
+ driver_str = "Ariranha::Drivers::#{driver.capitalize}"
30
+ driver = driver_str.split('::').reduce(Object) { |a, e| a.const_get(e) }
31
+ instances.each { |instance_cfg| @instances << driver.new(instance_cfg) }
32
+ end
33
+ end
34
+
35
+ def config_directories
36
+ @directories = []
37
+ config['providers'].each do |provider, provider_cfg|
38
+ @directories << Directory.new(provider, provider_cfg)
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,43 @@
1
+ require 'fog'
2
+
3
+ module Ariranha
4
+ class Directory
5
+ ADAPTER_KEYS = {
6
+ 'AWS' => [:aws_access_key_id, :aws_secret_access_key],
7
+ 'Google' => [:google_storage_access_key_id,
8
+ :google_storage_secret_access_key]
9
+ }
10
+
11
+ def initialize(adapter, config, backups_to_keep = 3)
12
+ @fog_storage = Fog::Storage.new(
13
+ ADAPTER_KEYS[adapter][0] => config['access_key'],
14
+ ADAPTER_KEYS[adapter][1] => config['secret_key'],
15
+ provider: adapter)
16
+ @adapter = adapter
17
+ @fog_directory = configure_fog_directory(config['directory'])
18
+ @backups_to_keep = backups_to_keep
19
+ end
20
+
21
+ def upload(filename, parent_dir)
22
+ puts "uploading #{filename} to #{adapter}..."
23
+ fog_directory.files.create(
24
+ key: "#{parent_dir}/#{filename}",
25
+ body: File.open("/tmp/#{filename}")
26
+ )
27
+ sorted_files = fog_directory.files.all(prefix: parent_dir + '/')
28
+ .reload.sort do |x, y|
29
+ x.last_modified <=> y.last_modified
30
+ end
31
+ sorted_files[0..(-backups_to_keep - 1)].each(&:destroy)
32
+ end
33
+
34
+ private
35
+
36
+ attr_reader :adapter, :fog_storage, :fog_directory, :backups_to_keep
37
+
38
+ def configure_fog_directory(directory_name)
39
+ fog_storage.directories.get(directory_name) ||
40
+ fog_storage.directories.create(key: directory_name)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,25 @@
1
+ require 'open3'
2
+
3
+ module Ariranha
4
+ module Drivers
5
+ class BaseDriver
6
+ def initialize(config)
7
+ @config = config
8
+ config_driver
9
+ @timestamp = Time.now.getutc.strftime('%Y%m%d%H%M%S')
10
+ end
11
+
12
+ attr_reader :database
13
+
14
+ def backup
15
+ puts "running #{backup_cmd}..."
16
+ _out, _err, _status = Open3.capture3 backup_cmd
17
+ filename
18
+ end
19
+
20
+ private
21
+
22
+ attr_reader :config, :filename, :timestamp
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,35 @@
1
+ require_relative 'base_driver'
2
+
3
+ module Ariranha
4
+ module Drivers
5
+ class Mysql < BaseDriver
6
+ def database
7
+ mysql_database
8
+ end
9
+
10
+ private
11
+
12
+ attr_reader :mysql_database, :mysql_host, :mysql_user, :mysql_password,
13
+ :mysql_ssl, :mysql_cert_path
14
+
15
+ def config_driver
16
+ config.each do |k, v|
17
+ instance_variable_set("@mysql_#{k}".to_sym, v)
18
+ end
19
+ end
20
+
21
+ def backup_cmd
22
+ cmd = "mysqldump -u#{mysql_user} "
23
+ cmd += "-p#{mysql_password} " if mysql_password
24
+ cmd += "--ssl_ca=#{mysql_cert_path} " if mysql_ssl
25
+ cmd + '--single-transaction --routines --triggers '\
26
+ "-h #{mysql_host} #{mysql_database} "\
27
+ "| gzip -c > /tmp/#{filename}"
28
+ end
29
+
30
+ def filename
31
+ "#{mysql_database}-#{timestamp}.sql.gz"
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,33 @@
1
+ require_relative 'base_driver'
2
+
3
+ module Ariranha
4
+ module Drivers
5
+ class Postgresql < BaseDriver
6
+ def database
7
+ pgsql_database
8
+ end
9
+
10
+ private
11
+
12
+ attr_reader :pgsql_database, :pgsql_host, :pgsql_user, :pgsql_password
13
+
14
+ def config_driver
15
+ config.each do |k, v|
16
+ instance_variable_set("@pgsql_#{k}".to_sym, v)
17
+ end
18
+ end
19
+
20
+ def backup_cmd
21
+ cmd = 'pg_dump -Fc --no-acl --no-owner '
22
+ cmd += "--host #{pgsql_host} " if pgsql_host
23
+ cmd += "--username #{pgsql_user} " if pgsql_user
24
+ cmd += "--password #{pgsql_password} " if pgsql_password
25
+ cmd + "#{pgsql_database} > /tmp/#{filename}"
26
+ end
27
+
28
+ def filename
29
+ "#{pgsql_database}-#{timestamp}.dump"
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module Ariranha
2
+ VERSION = '0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ariranha
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Artur Rodrigues
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry-byebug
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: fog
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Backs up databases and sends them to multiple clouds
56
+ email:
57
+ - arturhoo@gmail.com
58
+ executables:
59
+ - ariranha
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rubocop.yml"
65
+ - Gemfile
66
+ - LICENSE
67
+ - README.md
68
+ - Rakefile
69
+ - ariranha.gemspec
70
+ - ariranha.jpg
71
+ - bin/ariranha
72
+ - lib/ariranha.rb
73
+ - lib/ariranha/base.rb
74
+ - lib/ariranha/directory.rb
75
+ - lib/ariranha/drivers/base_driver.rb
76
+ - lib/ariranha/drivers/mysql.rb
77
+ - lib/ariranha/drivers/postgresql.rb
78
+ - lib/ariranha/version.rb
79
+ homepage: https://github.com/arturhoo/ariranha
80
+ licenses:
81
+ - MIT
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.4.5.1
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: Backs up databases and sends them to multiple clouds
103
+ test_files: []