db_time_machine 0.1.0
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 +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +3 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +7 -0
- data/README.md +70 -0
- data/Rakefile +3 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/db_time_machine.gemspec +34 -0
- data/lib/db_time_machine/dump.rb +105 -0
- data/lib/db_time_machine/railtie.rb +10 -0
- data/lib/db_time_machine/upload.rb +20 -0
- data/lib/db_time_machine/version.rb +3 -0
- data/lib/db_time_machine.rb +34 -0
- data/lib/tasks/db_time_machine.rake +12 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 393349f622b817f14f43d634f8d4f527504d3780
|
4
|
+
data.tar.gz: ea9209b1f37acfdc352a6690ab4b9f7183e4f8f6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 612b7dd06220bf4582c9db9e4b595dcb906adee62f73a239fdffb11525962073aad42fcedb89e0b1f32e62974024b48fe7794f9aab6ab2ac6460659c7fd4343c
|
7
|
+
data.tar.gz: 6a8fabfb2cb9de7aabe827acf8889435f43dc224be65f54bae8f5eee081f896749c0cd5ce285b2a8675014fb119f9433535530b8e9cc80b33c25dcbc2e5bef4c
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
# DbTimeMachine
|
2
|
+
|
3
|
+
This gem takes the dump of database for a rails application and upload it to fog directory. This can be used as cron job.
|
4
|
+
|
5
|
+
## Supports
|
6
|
+
|
7
|
+
1. Mysql
|
8
|
+
2. PostgreSql
|
9
|
+
3. MongoDB
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'db_time_machine'
|
17
|
+
```
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install db_time_machine
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
Initialize the configuration
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
# /config/initializers/db_time_machine.rb
|
33
|
+
|
34
|
+
DbTimeMachine.configure do |config|
|
35
|
+
# Optional configuration
|
36
|
+
# To dump only specific tables and not all the tables. default is []
|
37
|
+
config.tables = %w(table1 table2 table3 table4)
|
38
|
+
# Location to save the dump locally. default is /tmp
|
39
|
+
config.tmp_folder = "/tmp"
|
40
|
+
# Mandatory configuration
|
41
|
+
config.fog_connection = Fog::Storage.new({
|
42
|
+
provider: 'AWS_OR_ANY_OTHER_PROVIDER',
|
43
|
+
aws_access_key_id: 'AK**********************',
|
44
|
+
aws_secret_access_key: 'qwef*************************************'
|
45
|
+
})
|
46
|
+
# Directory must exist on provider's storage
|
47
|
+
config.fog_dir = 'DIRECTORY_OR_BUCKET_NAME'
|
48
|
+
end
|
49
|
+
|
50
|
+
For more providers check [http://fog.io/about/provider_documentation.html]: http://fog.io/about/provider_documentation.html
|
51
|
+
|
52
|
+
```
|
53
|
+
Use following rake task to upload the dump.
|
54
|
+
```sh
|
55
|
+
rake db_time_machine:start
|
56
|
+
```
|
57
|
+
|
58
|
+
## Development
|
59
|
+
|
60
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
61
|
+
|
62
|
+
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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
63
|
+
|
64
|
+
## Contributing
|
65
|
+
|
66
|
+
1. Fork it ( https://github.com/suratpyari/db_time_machine/fork )
|
67
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
68
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
69
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
70
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "db_time_machine"
|
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
|
data/bin/setup
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'db_time_machine/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "db_time_machine"
|
8
|
+
spec.version = DbTimeMachine::VERSION
|
9
|
+
spec.authors = ["SURAT PYARI"]
|
10
|
+
spec.email = ["suratpyari.db21@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Dump data and upload to fog directory.}
|
13
|
+
spec.description = %q{This gem takes the dump of database for a rails application and upload it to fog directory.}
|
14
|
+
spec.homepage = "https://github.com/suratpyari/db_time_machine"
|
15
|
+
|
16
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
17
|
+
# delete this section to allow pushing this gem to any host.
|
18
|
+
if spec.respond_to?(:metadata)
|
19
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
20
|
+
else
|
21
|
+
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
22
|
+
end
|
23
|
+
|
24
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
25
|
+
spec.bindir = "exe"
|
26
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
27
|
+
spec.require_paths = ["lib"]
|
28
|
+
|
29
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
30
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
31
|
+
spec.add_runtime_dependency "rails", [">= 4.0"]
|
32
|
+
spec.add_runtime_dependency "fog", [">= 1.32"]
|
33
|
+
|
34
|
+
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
module Dump
|
2
|
+
|
3
|
+
class DatabaseConfig
|
4
|
+
attr_accessor :host, :port, :db, :user, :password, :cmd, :file, :tables, :tmp_folder
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
self.cmd = nil
|
8
|
+
self.tables = DbTimeMachine.configuration.tables
|
9
|
+
self.tmp_folder = DbTimeMachine.configuration.tmp_folder
|
10
|
+
DatabaseConfig.find_config do |app, d, u, p, h, a, p1|
|
11
|
+
self.db, self.user, self.password, self.host, self.port = d, u, p, h, p1
|
12
|
+
end
|
13
|
+
self.file = File.join(tmp_folder, "#{db}_#{Time.now.to_i}.sql")
|
14
|
+
end
|
15
|
+
|
16
|
+
def start_dump
|
17
|
+
puts "exporting the latest dump to #{file}"
|
18
|
+
inprocess = File.join(tmp_folder, "dumping_data")
|
19
|
+
`touch #{inprocess};#{cmd};rm #{inprocess}`
|
20
|
+
while File.exist?('#{inprocess}')
|
21
|
+
puts "database dump is in progress."
|
22
|
+
puts "sleeping"
|
23
|
+
sleep(0.5)
|
24
|
+
end
|
25
|
+
puts "database dump is complete."
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.find_config
|
29
|
+
yield Rails.application.class.parent_name.underscore,
|
30
|
+
ActiveRecord::Base.connection_config[:database],
|
31
|
+
ActiveRecord::Base.connection_config[:username],
|
32
|
+
ActiveRecord::Base.connection_config[:password],
|
33
|
+
ActiveRecord::Base.connection_config[:host],
|
34
|
+
ActiveRecord::Base.connection_config[:port]
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.find_adapter
|
38
|
+
return 'mongodb' if File.exists?("#{Rails.root}/config/mongoid.yml")
|
39
|
+
ActiveRecord::Base.connection_config[:adapter]
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
class Mysql2 < DatabaseConfig
|
44
|
+
|
45
|
+
def start_dump
|
46
|
+
self.cmd = "mysqldump "
|
47
|
+
self.cmd << "-u #{self.user} " if self.user
|
48
|
+
self.cmd << "-p#{self.password} " if self.password
|
49
|
+
self.cmd << "-h #{self.host} " if self.host
|
50
|
+
self.cmd << "-P #{self.port} " if self.port
|
51
|
+
self.cmd << "#{db} #{tables.join(' ')} > #{self.file} "
|
52
|
+
super
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
class Postgresql < DatabaseConfig
|
57
|
+
|
58
|
+
def start_dump
|
59
|
+
self.cmd = "pg_dump "
|
60
|
+
self.cmd = "PGPASSWORD='#{self.password}' "+self.cmd if self.password
|
61
|
+
self.cmd << "-U #{self.user} " if self.user
|
62
|
+
self.cmd << "-h #{self.host} " if self.host
|
63
|
+
self.cmd << "-p #{self.port} " if self.port
|
64
|
+
self.cmd << "#{db} "
|
65
|
+
self.cmd << "-t #{tables.join(' -t ')}" if tables.size > 0
|
66
|
+
self.cmd << " > #{self.file} "
|
67
|
+
|
68
|
+
super
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
class Mongodb < DatabaseConfig
|
73
|
+
|
74
|
+
def initialize
|
75
|
+
self.cmd = nil
|
76
|
+
self.tables = DbTimeMachine.configuration.tables
|
77
|
+
self.tmp_folder = DbTimeMachine.configuration.tmp_folder
|
78
|
+
Mongodb.find_config do |app, d, u, p, h, a, p1|
|
79
|
+
self.db, self.user, self.password, self.host, self.port = d, u, p, h, p1
|
80
|
+
end
|
81
|
+
self.file = File.join(tmp_folder, "#{db}_#{Time.now.to_i}")
|
82
|
+
end
|
83
|
+
|
84
|
+
def start_dump
|
85
|
+
self.cmd = "mongodump "
|
86
|
+
self.cmd << "--host #{self.host} " if self.host
|
87
|
+
self.cmd << "--port #{self.port} " if self.port
|
88
|
+
self.cmd << "--username #{self.user} " if self.user
|
89
|
+
self.cmd << "--password #{self.password} " if self.password
|
90
|
+
self.cmd << "--out #{self.file}"
|
91
|
+
super
|
92
|
+
end
|
93
|
+
|
94
|
+
def self.find_config
|
95
|
+
mongoid_config = File.read("#{Rails.root}/config/mongoid.yml")
|
96
|
+
yield Rails.application.class.parent_name.underscore,
|
97
|
+
YAML.load(mongoid_config)[Rails.env]["clients"]["default"]["database"],
|
98
|
+
(YAML.load(mongoid_config)[Rails.env]["clients"]["default"]["options"]["user"] rescue nil),
|
99
|
+
(YAML.load(mongoid_config)[Rails.env]["clients"]["default"]["options"]["password"] rescue nil),
|
100
|
+
YAML.load(mongoid_config)[Rails.env]["clients"]["default"]["hosts"][0].split(':')[0],
|
101
|
+
YAML.load(mongoid_config)[Rails.env]["clients"]["default"]["hosts"][0].split(':')[1]
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class Upload
|
2
|
+
|
3
|
+
attr_accessor :fog_connection, :fog_directory, :file
|
4
|
+
|
5
|
+
def initialize(f)
|
6
|
+
self.fog_connection = DbTimeMachine.configuration.fog_connection
|
7
|
+
self.fog_directory = self.fog_connection.directories.get(DbTimeMachine.configuration.fog_dir)
|
8
|
+
self.file = f
|
9
|
+
end
|
10
|
+
|
11
|
+
def start
|
12
|
+
puts "uploading data to fog directory"
|
13
|
+
self.fog_directory.files.create(
|
14
|
+
:key => file.split('/').last,
|
15
|
+
:body => File.open(file),
|
16
|
+
:public => false
|
17
|
+
)
|
18
|
+
puts "upload complate"
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require "db_time_machine/version"
|
2
|
+
|
3
|
+
module DbTimeMachine
|
4
|
+
class Configuration
|
5
|
+
attr_accessor :tables, :tmp_folder, :fog_connection, :fog_dir
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
self.tables = []
|
9
|
+
self.tmp_folder = "/tmp"
|
10
|
+
self.fog_connection = nil
|
11
|
+
self.fog_dir = nil
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
class << self
|
17
|
+
attr_accessor :configuration
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.configuration
|
21
|
+
@configuration ||= Configuration.new
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.configure
|
25
|
+
yield(configuration) if block_given?
|
26
|
+
raise ArgumentError.new("Fog connection not set.") unless configuration.fog_connection
|
27
|
+
raise ArgumentError.new("Fog directory not set.") unless configuration.fog_dir
|
28
|
+
end
|
29
|
+
if defined?(Rails)
|
30
|
+
require "db_time_machine/railtie"
|
31
|
+
require "db_time_machine/dump"
|
32
|
+
require "db_time_machine/upload"
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
namespace :db_time_machine do
|
2
|
+
desc "upload database dump to s3"
|
3
|
+
task start: :environment do
|
4
|
+
dump = eval("Dump::#{Dump::DatabaseConfig.find_adapter.capitalize}.new()")
|
5
|
+
dump.start_dump
|
6
|
+
`tar -zcvf #{dump.file}.tar.gz #{dump.file}`
|
7
|
+
upload = Upload.new(dump.file+".tar.gz")
|
8
|
+
upload.start
|
9
|
+
`rm -r #{dump.file} #{dump.file}.tar.gz`
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: db_time_machine
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- SURAT PYARI
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-12-01 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.9'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.9'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rails
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '4.0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '4.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: fog
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.32'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.32'
|
69
|
+
description: This gem takes the dump of database for a rails application and upload
|
70
|
+
it to fog directory.
|
71
|
+
email:
|
72
|
+
- suratpyari.db21@gmail.com
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- ".travis.yml"
|
79
|
+
- CODE_OF_CONDUCT.md
|
80
|
+
- Gemfile
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- bin/console
|
84
|
+
- bin/setup
|
85
|
+
- db_time_machine.gemspec
|
86
|
+
- lib/db_time_machine.rb
|
87
|
+
- lib/db_time_machine/dump.rb
|
88
|
+
- lib/db_time_machine/railtie.rb
|
89
|
+
- lib/db_time_machine/upload.rb
|
90
|
+
- lib/db_time_machine/version.rb
|
91
|
+
- lib/tasks/db_time_machine.rake
|
92
|
+
homepage: https://github.com/suratpyari/db_time_machine
|
93
|
+
licenses: []
|
94
|
+
metadata:
|
95
|
+
allowed_push_host: https://rubygems.org
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
requirements: []
|
111
|
+
rubyforge_project:
|
112
|
+
rubygems_version: 2.4.6
|
113
|
+
signing_key:
|
114
|
+
specification_version: 4
|
115
|
+
summary: Dump data and upload to fog directory.
|
116
|
+
test_files: []
|