data_sync 1.0.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.
- data/Readme.markdown +74 -0
- data/data_sync.gemspec +15 -0
- data/lib/data_sync/railtie.rb +12 -0
- data/lib/data_sync.rb +3 -0
- data/lib/data_sync_recipes.rb +1 -0
- data/lib/recipes/data_sync.rb +60 -0
- data/lib/tasks/data.rake +7 -0
- data/lib/tasks/data_sync.rake +93 -0
- metadata +74 -0
data/Readme.markdown
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
Data sync
|
2
|
+
=======
|
3
|
+
|
4
|
+
Rails plugin for syncing database and files between production and local development server.
|
5
|
+
|
6
|
+
Requirements
|
7
|
+
============
|
8
|
+
|
9
|
+
* Rails 3
|
10
|
+
* Capistrano
|
11
|
+
|
12
|
+
Install
|
13
|
+
=======
|
14
|
+
|
15
|
+
To install this gem, add the gem to your Gemfile
|
16
|
+
|
17
|
+
gem 'data_sync'
|
18
|
+
|
19
|
+
And then add this line to your Capfile
|
20
|
+
|
21
|
+
require 'data_sync_recipes'
|
22
|
+
|
23
|
+
Usage
|
24
|
+
=====
|
25
|
+
|
26
|
+
For pulling the database of the remote server
|
27
|
+
|
28
|
+
rake db:pull
|
29
|
+
|
30
|
+
For pushing the local database to the remote server
|
31
|
+
|
32
|
+
rake db:push
|
33
|
+
|
34
|
+
For pulling the database and the files of the remote server
|
35
|
+
|
36
|
+
rake data:pull
|
37
|
+
|
38
|
+
MongoDB
|
39
|
+
====
|
40
|
+
|
41
|
+
If you're planning to use this with MongoDB your config/database.yml should use mongodb as adapter value.
|
42
|
+
|
43
|
+
* development:
|
44
|
+
* adapter: mongodb
|
45
|
+
* database: surveys-development
|
46
|
+
* host: localhost
|
47
|
+
* ...
|
48
|
+
|
49
|
+
And initalizer for MongoDB in config/initializers/mongo.rb
|
50
|
+
default_mongodb_port = 27107
|
51
|
+
|
52
|
+
database_configurations = YAML::load(File.read(Rails.root.join('config', 'database.yml')))
|
53
|
+
|
54
|
+
if database_configurations[Rails.env] && database_configurations[Rails.env]['adapter'] == 'mongodb'
|
55
|
+
database = database_configurations[Rails.env]
|
56
|
+
database['port'] ||= default_mongodb_port
|
57
|
+
|
58
|
+
MongoMapper.connection = Mongo::Connection.new(database['hostname'], database['port'])
|
59
|
+
MongoMapper.database = database['database']
|
60
|
+
|
61
|
+
if defined?(PhusionPassenger)
|
62
|
+
PhusionPassenger.on_event(:starting_worker_process) do |forked|
|
63
|
+
MongoMapper.connection.connect_to_master if forked
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
TODO
|
69
|
+
====
|
70
|
+
* Prompt ("All files will be overwritten..")
|
71
|
+
* database and files push
|
72
|
+
* change usage to "push:files", "push:db", "push:data", "pull:files", "pull:db", "pull:files"
|
73
|
+
* refactor rake task, duplicate code
|
74
|
+
|
data/data_sync.gemspec
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'data_sync'
|
3
|
+
s.version = '1.0.0'
|
4
|
+
s.date = '2012-12-10'
|
5
|
+
s.summary = "Data sync rake tasks."
|
6
|
+
s.description = "Rake tasks to sync data between development and production environments (servers)."
|
7
|
+
s.authors = ["Tomislav Car", "Nikica Kapraljević", "Josip Bišćan"]
|
8
|
+
s.email = ["tomislav@infinum.hr", "nikola@infinum.hr", "josip@infinum.hr"]
|
9
|
+
s.homepage = 'https://github.com/infinum/data_sync'
|
10
|
+
|
11
|
+
s.add_dependency 'capistrano'
|
12
|
+
|
13
|
+
s.files = `git ls-files`.split("\n")
|
14
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
15
|
+
end
|
data/lib/data_sync.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Dir.glob(File.join(File.dirname(__FILE__), '/recipes/*.rb')).each {|recipe| load recipe}
|
@@ -0,0 +1,60 @@
|
|
1
|
+
Capistrano::Configuration.instance.load do
|
2
|
+
desc 'Dumps the production database to db/production_data.sql on the remote server'
|
3
|
+
task :remote_db_dump, :roles => :db, :only => { :primary => true } do
|
4
|
+
commands = [
|
5
|
+
"cd #{deploy_to}/#{current_dir}",
|
6
|
+
"bundle exec rake RAILS_ENV=production db:database_dump --trace"
|
7
|
+
]
|
8
|
+
|
9
|
+
run commands.join(" && ")
|
10
|
+
end
|
11
|
+
|
12
|
+
desc 'Restores the database from db/production_data.sql on the remote server'
|
13
|
+
task :remote_db_restore, :roles => :db, :only => { :primary => true } do
|
14
|
+
commands = [
|
15
|
+
"cd #{deploy_to}/#{current_dir}",
|
16
|
+
"bundle exec rake RAILS_ENV=production db:database_load --trace"
|
17
|
+
]
|
18
|
+
|
19
|
+
run commands.join(" && ")
|
20
|
+
end
|
21
|
+
|
22
|
+
desc 'Downloads db/production_data.tar.bz2 from the remote production environment to your local machine'
|
23
|
+
task :remote_db_download, :roles => :db, :only => { :primary => true } do
|
24
|
+
execute_on_servers(options) do |servers|
|
25
|
+
self.sessions[servers.first].sftp.connect do |tsftp|
|
26
|
+
tsftp.download! "#{deploy_to}/#{current_dir}/db/production_data.tar.bz2", "db/production_data.tar.bz2"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
desc 'Uploads db/production_data.tar.bz2 from local environment to your production machine'
|
32
|
+
task :local_db_upload, :roles => :db, :only => { :primary => true } do
|
33
|
+
execute_on_servers(options) do |servers|
|
34
|
+
self.sessions[servers.first].sftp.connect do |tsftp|
|
35
|
+
tsftp.upload! "db/production_data.tar.bz2", "#{deploy_to}/#{current_dir}/db/production_data.tar.bz2"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
desc 'Cleans up data dump file'
|
41
|
+
task :remote_db_cleanup, :roles => :db, :only => { :primary => true } do
|
42
|
+
run "rm #{deploy_to}/#{current_dir}/db/production_data.tar.bz2"
|
43
|
+
end
|
44
|
+
|
45
|
+
desc 'Dumps, downloads and then cleans up the production data dump'
|
46
|
+
task :remote_db_runner do
|
47
|
+
remote_db_dump
|
48
|
+
remote_db_download
|
49
|
+
remote_db_cleanup
|
50
|
+
end
|
51
|
+
|
52
|
+
desc "Downloads the files in public/system"
|
53
|
+
task :download_files, :roles => :db, :only => { :primary => true } do
|
54
|
+
`mkdir -p public/system`
|
55
|
+
execute_on_servers(options) do |servers|
|
56
|
+
logger.info "Pulling public/system via rsync"
|
57
|
+
`rsync --delete -r #{user}@#{servers.first}:#{deploy_to}/#{current_dir}/public/system/* public/system`
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
data/lib/tasks/data.rake
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
namespace :db do
|
3
|
+
desc "Refreshes your local development environment to the current production database"
|
4
|
+
task :pull do
|
5
|
+
`cap remote_db_runner`
|
6
|
+
`bundle exec rake db:database_load`
|
7
|
+
end
|
8
|
+
|
9
|
+
desc "Refreshes your production database to the current local development database"
|
10
|
+
task :push do
|
11
|
+
`bundle exec rake db:database_dump`
|
12
|
+
`cap local_db_upload`
|
13
|
+
`cap remote_db_restore`
|
14
|
+
end
|
15
|
+
|
16
|
+
desc "Dump the current database to a MySQL file"
|
17
|
+
task :database_dump => :environment do
|
18
|
+
databases = YAML::load(File.open(Rails.root.join('config', 'database.yml')))
|
19
|
+
|
20
|
+
if (databases[Rails.env]["adapter"] == 'mysql' || databases[Rails.env]["adapter"] == 'mysql2')
|
21
|
+
commands = []
|
22
|
+
|
23
|
+
mysql_dump_command = []
|
24
|
+
mysql_dump_command << "mysqldump"
|
25
|
+
mysql_dump_command << "-h #{databases[Rails.env]["host"].blank? ? 'localhost' : databases[Rails.env]["host"]}"
|
26
|
+
mysql_dump_command << "-u #{databases[Rails.env]["username"]}"
|
27
|
+
mysql_dump_command << "-p#{databases[Rails.env]["password"]}" if databases[Rails.env]["password"].present?
|
28
|
+
mysql_dump_command << "#{databases[Rails.env]["database"]}"
|
29
|
+
mysql_dump_command << " > #{Rails.root.join('db', 'production_data.sql')}"
|
30
|
+
|
31
|
+
commands << mysql_dump_command.join(' ')
|
32
|
+
commands << "cd #{Rails.root.join('db')}"
|
33
|
+
commands << "tar -cjf #{Rails.root.join('db', 'production_data.tar.bz2')} production_data.sql"
|
34
|
+
commands << "rm -fr #{Rails.root.join('db', 'production_data.sql')}"
|
35
|
+
|
36
|
+
`#{commands.join(' && ')}`
|
37
|
+
elsif databases[Rails.env]["adapter"] == 'mongodb'
|
38
|
+
port = databases[Rails.env]['port']
|
39
|
+
port ||= 27017 # default mongodb port
|
40
|
+
|
41
|
+
commands = []
|
42
|
+
commands << "rm -fr #{Rails.root.join('db', 'dump')}"
|
43
|
+
commands << "mongodump --host #{databases[Rails.env]['host']} --port #{port} --db #{databases[Rails.env]['database']} --out #{Rails.root.join('db', 'dump')}"
|
44
|
+
commands << "cd #{Rails.root.join('db')}"
|
45
|
+
commands << "tar -cjf #{Rails.root.join('db', 'production_data.tar.bz2')} dump/#{databases[Rails.env]['database']}"
|
46
|
+
commands << "rm -fr #{Rails.root.join('db', 'dump')}"
|
47
|
+
|
48
|
+
`#{commands.join(' && ')}`
|
49
|
+
else
|
50
|
+
raise "Task doesn't work with '#{databases[Rails.env]['adapter']}'"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
desc "Loads the production data downloaded into db/production_data into your local development database"
|
55
|
+
task :database_load => :environment do
|
56
|
+
databases = YAML::load(File.open(Rails.root.join('config', 'database.yml')))
|
57
|
+
|
58
|
+
database_folder = Rails.env == 'production' ? databases['development']['database'] : databases['production']['database']
|
59
|
+
|
60
|
+
unless File.exists? Rails.root.join('db', 'production_data.tar.bz2')
|
61
|
+
raise 'Unable to find database dump in db/production_data.tar.bz2'
|
62
|
+
end
|
63
|
+
|
64
|
+
if databases[Rails.env]["adapter"] == 'mysql' || databases[Rails.env]["adapter"] == 'mysql2'
|
65
|
+
commands = []
|
66
|
+
commands << "cd #{Rails.root.join('db')}"
|
67
|
+
commands << "tar -xjf #{Rails.root.join('db', 'production_data.tar.bz2')}"
|
68
|
+
|
69
|
+
mysql_dump_command = []
|
70
|
+
mysql_dump_command << "mysql"
|
71
|
+
mysql_dump_command << "-h #{databases[Rails.env]["host"]}" if databases[Rails.env]["host"].present?
|
72
|
+
mysql_dump_command << "-u #{databases[Rails.env]["username"]}"
|
73
|
+
mysql_dump_command << "-p#{databases[Rails.env]["password"]}" if databases[Rails.env]["password"].present?
|
74
|
+
mysql_dump_command << "#{databases[Rails.env]["database"]}"
|
75
|
+
mysql_dump_command << " < production_data.sql"
|
76
|
+
commands << mysql_dump_command.join(' ')
|
77
|
+
|
78
|
+
commands << "rm -fr #{Rails.root.join('db', 'production_data.tar.bz2')} #{Rails.root.join('db', 'production_data.sql')}"
|
79
|
+
|
80
|
+
`#{commands.join(' && ')}`
|
81
|
+
elsif databases[Rails.env]["adapter"] == 'mongodb'
|
82
|
+
commands = []
|
83
|
+
commands << "cd #{Rails.root.join('db')}"
|
84
|
+
commands << "tar -xjvf #{Rails.root.join('db', 'production_data.tar.bz2')}"
|
85
|
+
commands << "mongorestore --drop --db #{databases[Rails.env]['database']} #{Rails.root.join('db', 'dump', database_folder)}"
|
86
|
+
commands << "rm -fr #{Rails.root.join('db', 'dump')} #{Rails.root.join('db', 'production_data.tar.bz2')}"
|
87
|
+
|
88
|
+
`#{commands.join(' && ')}`
|
89
|
+
else
|
90
|
+
raise "Task not supported by '#{databases[Rails.env]['adapter']}'"
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: data_sync
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Tomislav Car
|
9
|
+
- Nikica Kapraljević
|
10
|
+
- Josip Bišćan
|
11
|
+
autorequire:
|
12
|
+
bindir: bin
|
13
|
+
cert_chain: []
|
14
|
+
date: 2012-12-10 00:00:00.000000000 Z
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: capistrano
|
18
|
+
requirement: !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ! '>='
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: '0'
|
24
|
+
type: :runtime
|
25
|
+
prerelease: false
|
26
|
+
version_requirements: !ruby/object:Gem::Requirement
|
27
|
+
none: false
|
28
|
+
requirements:
|
29
|
+
- - ! '>='
|
30
|
+
- !ruby/object:Gem::Version
|
31
|
+
version: '0'
|
32
|
+
description: Rake tasks to sync data between development and production environments
|
33
|
+
(servers).
|
34
|
+
email:
|
35
|
+
- tomislav@infinum.hr
|
36
|
+
- nikola@infinum.hr
|
37
|
+
- josip@infinum.hr
|
38
|
+
executables: []
|
39
|
+
extensions: []
|
40
|
+
extra_rdoc_files: []
|
41
|
+
files:
|
42
|
+
- Readme.markdown
|
43
|
+
- data_sync.gemspec
|
44
|
+
- lib/data_sync.rb
|
45
|
+
- lib/data_sync/railtie.rb
|
46
|
+
- lib/data_sync_recipes.rb
|
47
|
+
- lib/recipes/data_sync.rb
|
48
|
+
- lib/tasks/data.rake
|
49
|
+
- lib/tasks/data_sync.rake
|
50
|
+
homepage: https://github.com/infinum/data_sync
|
51
|
+
licenses: []
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options: []
|
54
|
+
require_paths:
|
55
|
+
- lib
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ! '>='
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
requirements: []
|
69
|
+
rubyforge_project:
|
70
|
+
rubygems_version: 1.8.23
|
71
|
+
signing_key:
|
72
|
+
specification_version: 3
|
73
|
+
summary: Data sync rake tasks.
|
74
|
+
test_files: []
|