cap_sync 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in cap_sync.gemspec
4
+ gemspec
@@ -0,0 +1,55 @@
1
+ = Capistrano Sync Recipes
2
+
3
+ Recipes to clone database & public data from production server to developement machine.
4
+
5
+ = Installation
6
+
7
+ Install the gem:
8
+
9
+ > gem install cap_sync
10
+
11
+ Add following line to your Capfile:
12
+
13
+ gem 'cap_sync'; require 'cap_sync'
14
+
15
+ = Syncing database
16
+
17
+ > cap sync:db
18
+
19
+ Does following:
20
+ 1. Accesses production server.
21
+ 2. Dumps database to app's tmp.
22
+ 3. Downloads dump to local app's tmp.
23
+ 4. Imports dump to local app's development database.
24
+
25
+ Remote database credentials are loaded from remote application config/database.yml.
26
+
27
+ Available variables and defaults:
28
+ :sync_local_env => 'development', # Local environment key in database.yml
29
+ :sync_remote_env => 'production', # Remote environment key in database.yml
30
+ :sync_remote_dump_cmd => 'mysqldump', # Remote mysqldump command
31
+ :sync_local_load_cmd => 'mysql', # Local mysqldump command
32
+ :sync_keep_dumps => false, # Keep downloaded dump in app's tmp after syncing
33
+
34
+ You can modify it in your deploy.rb as follows:
35
+
36
+ set :sync_local_cmd, '/usr/local/bin/mysql/bin'
37
+
38
+ = Syncing data
39
+
40
+ > cap sync:data
41
+
42
+ Rsync's local and remote folders. By default, does incremental implicit synchronization.
43
+
44
+ Available variables and defaults:
45
+ :sync_folders => {"#{shared_path}/system" => "public/system"}, # Folders to sync remote => local
46
+ :rsync_cmd => "rsync", # rsync command
47
+ :rsync_flags => "-rv --stats --delete" # rsync flags
48
+
49
+ = Todo
50
+
51
+ 1. Correctly handle multiple servers.
52
+ 2. Tests.
53
+ 3. Get gem to autoload, without require.
54
+ 4. Sync multiple databases.
55
+ 5. Sync up and down.
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "cap_sync"
6
+ s.version = '0.0.2'
7
+ s.platform = Gem::Platform::RUBY
8
+ s.author = "Victor Sokolov"
9
+ s.email = "gzigzigzeo@gmail.com"
10
+ s.homepage = "http://github.com/gzigzigzeo/cap_sync"
11
+ s.summary = %q{Recipes to clone database & public data from production server to developement machine}
12
+ s.description = %q{Recipes to clone database & public data from production server to developement machine}
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_path = 'lib'
18
+ end
@@ -0,0 +1,59 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'fileutils'
4
+
5
+ Capistrano::Configuration.instance(:must_exist).load do
6
+ {
7
+ :sync_local_env => 'development', # Local environment key in database.yml
8
+ :sync_remote_env => 'production', # Remote environment key in database.yml
9
+ :sync_remote_dump_cmd => 'mysqldump', # Remote mysqldump command
10
+ :sync_local_load_cmd => 'mysql', # Local mysqldump command
11
+ :sync_keep_dumps => false, # Keep downloaded dump in app's tmp after syncing
12
+
13
+ :sync_folders => {"#{shared_path}/system" => "public/system"}, # Folders to sync remote => local
14
+ :rsync_cmd => "rsync", # rsync command
15
+ :rsync_flags => "-rv --stats --delete" # rsync flags
16
+ }.each do |var, value|
17
+ self.set(var, value) unless exists?(var)
18
+ end
19
+
20
+ namespace :sync do
21
+ desc "Sync remote production database with local development machine"
22
+ task :db do
23
+ username, password, database, host, port = remote_database_config(sync_remote_env)
24
+
25
+ temp = "sync-#{Time.now.to_i}.sql"
26
+ run "#{sync_remote_dump_cmd} -u #{username} --password=\"#{password}\" -h #{host} --port #{port} #{database} > #{shared_path}/#{temp}"
27
+ get "#{shared_path}/#{temp}", "tmp/#{temp}"
28
+
29
+ username, password, database, host, port = local_database_config(sync_local_env)
30
+
31
+ system("#{sync_local_load_cmd} #{database} -u#{username} --password=\"#{password}\" < tmp/#{temp}")
32
+ FileUtils.rm("tmp/#{temp}") unless sync_keep_dumps
33
+ run "rm #{shared_path}/#{temp}"
34
+ end
35
+
36
+ desc "Sync remote production data with local development machine"
37
+ task :data do
38
+ sync_folders.each do |remote, local|
39
+ host = find_servers(:roles => :web).first.host
40
+ system("#{rsync_cmd} #{rsync_flags} #{user}@#{host}:#{remote}/. #{local}")
41
+ end
42
+ end
43
+ end
44
+
45
+ def local_database_config(env)
46
+ db = File.open("config/database.yml") { |yf| YAML::load(yf) }
47
+ defaults_config(db[env.to_s])
48
+ end
49
+
50
+ def remote_database_config(env)
51
+ remote_config = capture("cat #{current_path}/config/database.yml")
52
+ db = YAML::load(remote_config)
53
+ defaults_config(db[env.to_s])
54
+ end
55
+
56
+ def defaults_config(db)
57
+ return db['username'], db['password'], db['database'], db['host'], (db['port'] || 3306)
58
+ end
59
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cap_sync
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 2
9
+ version: 0.0.2
10
+ platform: ruby
11
+ authors:
12
+ - Victor Sokolov
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-03-12 00:00:00 +03:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Recipes to clone database & public data from production server to developement machine
22
+ email: gzigzigzeo@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - .gitignore
31
+ - Gemfile
32
+ - README.rdoc
33
+ - Rakefile
34
+ - cap_sync.gemspec
35
+ - lib/cap_sync.rb
36
+ has_rdoc: true
37
+ homepage: http://github.com/gzigzigzeo/cap_sync
38
+ licenses: []
39
+
40
+ post_install_message:
41
+ rdoc_options: []
42
+
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ segments:
50
+ - 0
51
+ version: "0"
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ requirements: []
60
+
61
+ rubyforge_project:
62
+ rubygems_version: 1.3.6
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: Recipes to clone database & public data from production server to developement machine
66
+ test_files: []
67
+