dbsync 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +48 -0
- data/Rakefile +1 -0
- data/dbsync.gemspec +26 -0
- data/lib/dbsync.rb +9 -0
- data/lib/dbsync/version.rb +3 -0
- data/lib/tasks/dbsync.rake +117 -0
- metadata +89 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Bryan Ricker
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
= dbsync
|
2
|
+
|
3
|
+
A set of rake tasks to help you sync your production
|
4
|
+
data with your local database for development.
|
5
|
+
|
6
|
+
Currently only supports:
|
7
|
+
* Rails
|
8
|
+
* MySQL
|
9
|
+
|
10
|
+
Support for more things will happen if anybody needs it.
|
11
|
+
|
12
|
+
Add the following to your +config/environments/development.rb+
|
13
|
+
file:
|
14
|
+
config.dbsync = ActiveSupport::OrderedOptions.new
|
15
|
+
|
16
|
+
config.dbsync.filename = "yourapp_production_data.dump" # The name of the remote dumpfile
|
17
|
+
config.dbsync.local_dir = "#{Rails.root}/../dbsync" # The local directory to store the dump file. No trailing slash
|
18
|
+
config.dbsync.remote_host = "66.123.4.567" # Remote server where the dumpfile is
|
19
|
+
config.dbsync.remote_dir = "~dbsync" # The directory on the remote server where the dumpfile is
|
20
|
+
|
21
|
+
Now just make sure you have something on the remote
|
22
|
+
server updating that dumpfile. I recommend a cronjob:
|
23
|
+
0 */12 * * * /usr/bin/mysqldump yourapp_production > /home/dbsync/yourapp_production_data.dump
|
24
|
+
|
25
|
+
You will need proper SSH access into the remote server,
|
26
|
+
as the tasks use +rsync+ and +scp+ directly.
|
27
|
+
|
28
|
+
Run +rake -T dbsync+ for all of the available tasks:
|
29
|
+
rake dbsync # Alias for dbsync:pull
|
30
|
+
rake dbsync:clone # Copy the remote dump file, reset the local database, and load in the dump file
|
31
|
+
rake dbsync:clone_dump # Copy the remote dump file to a local destination
|
32
|
+
rake dbsync:config # Show the dbsync configuration
|
33
|
+
rake dbsync:fetch # Update the local dump file from the remote source
|
34
|
+
rake dbsync:merge # Merge the local dump file into the local database
|
35
|
+
rake dbsync:pull # Update the local dump file, and merge it into the local database
|
36
|
+
rake dbsync:reset # Drop & Create the database, then load the dump file.
|
37
|
+
|
38
|
+
== TODO
|
39
|
+
|
40
|
+
- Specs!
|
41
|
+
|
42
|
+
== Copyright
|
43
|
+
|
44
|
+
Copyright (c) 2012 Bryan Ricker/SCPR.
|
45
|
+
|
46
|
+
== Licence
|
47
|
+
|
48
|
+
See MIT-LICENSE for more.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/dbsync.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "dbsync/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "dbsync"
|
7
|
+
s.version = Dbsync::VERSION
|
8
|
+
s.authors = ["Bryan Ricker"]
|
9
|
+
s.email = ["bricker88@gmail.com"]
|
10
|
+
s.homepage = "http://github.com/bricker88/dbsync"
|
11
|
+
s.summary = %q{Easy syncing from remote to development database in Rails.}
|
12
|
+
s.description = %q{A set of rake tasks to help you sync your remote production data with your local database for development.}
|
13
|
+
|
14
|
+
s.rubyforge_project = "dbsync"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib", "lib/tasks"]
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
# s.add_development_dependency "rspec"
|
23
|
+
s.add_runtime_dependency "activesupport", "~> 3.2.8"
|
24
|
+
s.add_runtime_dependency "activerecord", "~> 3.2.8"
|
25
|
+
s.add_runtime_dependency "railties", "~> 3.2.8"
|
26
|
+
end
|
data/lib/dbsync.rb
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
# Easy database syncing for development/staging
|
2
|
+
|
3
|
+
desc "Alias for dbsync:pull"
|
4
|
+
task :dbsync do
|
5
|
+
Rake::Task["dbsync:pull"].invoke
|
6
|
+
end
|
7
|
+
|
8
|
+
namespace :dbsync do
|
9
|
+
task :setup => :environment do
|
10
|
+
module Dbsync
|
11
|
+
LOGGER = $stdout
|
12
|
+
CONFIG = Rails.application.config.dbsync
|
13
|
+
|
14
|
+
CONFIG['remote'] = "#{CONFIG['remote_host']}:" + File.join(CONFIG['remote_dir'], CONFIG['filename'])
|
15
|
+
CONFIG['local'] = File.join CONFIG['local_dir'], CONFIG['filename']
|
16
|
+
end
|
17
|
+
|
18
|
+
# ---------------
|
19
|
+
|
20
|
+
Dbsync::LOGGER.puts "Environment: #{Rails.env}"
|
21
|
+
|
22
|
+
if Rails.env == 'production'
|
23
|
+
raise "These tasks are destructive and shouldn't be used in the production environment."
|
24
|
+
end
|
25
|
+
|
26
|
+
#-----------------
|
27
|
+
|
28
|
+
if Dbsync::CONFIG['filename'].blank?
|
29
|
+
raise "No dump filename specified."
|
30
|
+
elsif Dbsync::CONFIG['remote'].blank?
|
31
|
+
raise "No remote dump file specified."
|
32
|
+
end
|
33
|
+
|
34
|
+
#-----------------
|
35
|
+
|
36
|
+
VERBOSE = %w{1 true}.include? ENV['VERBOSE']
|
37
|
+
DB = ActiveRecord::Base.configurations[Rails.env]
|
38
|
+
end
|
39
|
+
|
40
|
+
#-----------------------
|
41
|
+
|
42
|
+
desc "Show the dbsync configuration"
|
43
|
+
task :config => :setup do
|
44
|
+
Dbsync::LOGGER.puts "Config:"
|
45
|
+
Dbsync::LOGGER.puts Dbsync::CONFIG.to_yaml
|
46
|
+
end
|
47
|
+
|
48
|
+
#-----------------------
|
49
|
+
|
50
|
+
desc "Update the local dump file, and merge it into the local database"
|
51
|
+
task :pull => [:fetch, :merge]
|
52
|
+
|
53
|
+
desc "Copy the remote dump file, reset the local database, and load in the dump file"
|
54
|
+
task :clone => [:clone_dump, :reset]
|
55
|
+
|
56
|
+
#-----------------------
|
57
|
+
|
58
|
+
desc "Update the local dump file from the remote source"
|
59
|
+
task :fetch => :setup do
|
60
|
+
Dbsync::LOGGER.puts "Fetching #{Dbsync::CONFIG['remote']} using rsync"
|
61
|
+
output = %x{ rsync -v #{Dbsync::CONFIG['remote']} #{Dbsync::CONFIG['local']} }
|
62
|
+
|
63
|
+
if VERBOSE
|
64
|
+
Dbsync::LOGGER.puts output
|
65
|
+
end
|
66
|
+
|
67
|
+
Dbsync::LOGGER.puts "Finished."
|
68
|
+
end
|
69
|
+
|
70
|
+
#-----------------------
|
71
|
+
|
72
|
+
desc "Copy the remote dump file to a local destination"
|
73
|
+
task :clone_dump => :setup do
|
74
|
+
Dbsync::LOGGER.puts "Fetching #{Dbsync::CONFIG['remote']} using scp"
|
75
|
+
output = %x{ scp #{Dbsync::CONFIG['remote']} #{Dbsync::CONFIG['local_dir']}/ }
|
76
|
+
|
77
|
+
if VERBOSE
|
78
|
+
Dbsync::LOGGER.puts output
|
79
|
+
end
|
80
|
+
|
81
|
+
Dbsync::LOGGER.puts "Finished."
|
82
|
+
end
|
83
|
+
|
84
|
+
#-----------------------
|
85
|
+
|
86
|
+
desc "Merge the local dump file into the local database"
|
87
|
+
task :merge => :setup do
|
88
|
+
Dbsync::LOGGER.puts "Dumping data from #{Dbsync::CONFIG['local']} into #{DB['database']}"
|
89
|
+
|
90
|
+
command = "mysql "
|
91
|
+
command += "-u #{DB['username']} " if DB['username'].present?
|
92
|
+
command += "-p#{DB['password']} " if DB['password'].present?
|
93
|
+
command += "-h #{DB['host']} " if DB['host'].present?
|
94
|
+
command += "#{DB['database']} < #{Dbsync::CONFIG['local']}"
|
95
|
+
|
96
|
+
output = %x{#{command}}
|
97
|
+
|
98
|
+
if VERBOSE
|
99
|
+
Dbsync::LOGGER.puts output
|
100
|
+
end
|
101
|
+
|
102
|
+
Dbsync::LOGGER.puts "Finished."
|
103
|
+
end
|
104
|
+
|
105
|
+
#-----------------------
|
106
|
+
|
107
|
+
desc "Drop & Create the database, then load the dump file."
|
108
|
+
task :reset => :setup do
|
109
|
+
if VERBOSE
|
110
|
+
Dbsync::LOGGER.puts "Resetting database..."
|
111
|
+
end
|
112
|
+
|
113
|
+
Rake::Task["db:drop"].invoke
|
114
|
+
Rake::Task["db:create"].invoke
|
115
|
+
Rake::Task["dbsync:merge"].invoke
|
116
|
+
end
|
117
|
+
end
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dbsync
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Bryan Ricker
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-05 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: &70099915000180 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.2.8
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70099915000180
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: activerecord
|
27
|
+
requirement: &70099914999600 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 3.2.8
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70099914999600
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: railties
|
38
|
+
requirement: &70099914999120 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 3.2.8
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70099914999120
|
47
|
+
description: A set of rake tasks to help you sync your remote production data with
|
48
|
+
your local database for development.
|
49
|
+
email:
|
50
|
+
- bricker88@gmail.com
|
51
|
+
executables: []
|
52
|
+
extensions: []
|
53
|
+
extra_rdoc_files: []
|
54
|
+
files:
|
55
|
+
- .gitignore
|
56
|
+
- Gemfile
|
57
|
+
- MIT-LICENSE
|
58
|
+
- README.rdoc
|
59
|
+
- Rakefile
|
60
|
+
- dbsync.gemspec
|
61
|
+
- lib/dbsync.rb
|
62
|
+
- lib/dbsync/version.rb
|
63
|
+
- lib/tasks/dbsync.rake
|
64
|
+
homepage: http://github.com/bricker88/dbsync
|
65
|
+
licenses: []
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
- lib/tasks
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
requirements: []
|
84
|
+
rubyforge_project: dbsync
|
85
|
+
rubygems_version: 1.8.16
|
86
|
+
signing_key:
|
87
|
+
specification_version: 3
|
88
|
+
summary: Easy syncing from remote to development database in Rails.
|
89
|
+
test_files: []
|