blavosync 0.3.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/Blavosync.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{blavosync}
8
- s.version = "0.3.2"
8
+ s.version = "0.4.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["jayronc"]
12
- s.date = %q{2010-01-05}
12
+ s.date = %q{2010-03-19}
13
13
  s.description = %q{Sync a remote db and rsync content to your development environment. Useful for small teams and developers who are not able to do this manually.}
14
14
  s.email = %q{jerrodblavos@mac.com}
15
15
  s.extra_rdoc_files = [
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.2
1
+ 0.4.0
data/lib/blavosync.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Blavosync
2
- VERSION = '0.1.3'
2
+ VERSION = '0.4.0'
3
3
  end
@@ -9,6 +9,9 @@ Capistrano::Configuration.instance(:must_exist).load do
9
9
  last_modified = Time.now.to_i
10
10
  generate_remote_db_backup
11
11
  system "mkdir -p #{tmp_dir}"
12
+ download(db_schema_backup_zip_file, "#{local_db_schema_backup_file(:env=>from_env, :timestamp=>last_modified)}.#{zip_ext}", :via=> :scp) do|ch, name, sent, total|
13
+ print "\r\033[1;42m #{File.basename(name)}: #{sent}/#{total} -- #{(sent.to_f * 100 / total.to_f).to_i}% \033[0m"
14
+ end
12
15
  download(db_backup_zip_file, "#{local_db_backup_file(:env=>from_env, :timestamp=>last_modified)}.#{zip_ext}", :via=> :scp) do|ch, name, sent, total|
13
16
  print "\r\033[1;42m #{File.basename(name)}: #{sent}/#{total} -- #{(sent.to_f * 100 / total.to_f).to_i}% \033[0m"
14
17
  end
@@ -19,15 +22,28 @@ Capistrano::Configuration.instance(:must_exist).load do
19
22
  variable, which defalts to RAILS_ENV), and imports (via mysql command line tool) it back into the database
20
23
  defined in the RESTORE_ENV env variable (defaults to development).
21
24
  DESC
25
+
26
+
22
27
  task :restore_db, :roles => :db do
23
28
  mysql_str = "mysql #{mysql_connection_for(to_env)}"
24
29
  mysql_dump = "mysqldump #{mysql_connection_for(from_env)}"
25
- local_db_create = "mysql #{mysql_connection_for(to_env)} -e \"create database if not exists #{mysql_db_for(to_env)}\""
30
+ local_db_create = "mysqladmin create #{mysql_connection_for(to_env)} " # "create database if not exists #{mysql_db_for(to_env)}"
31
+ remote_schema_backup_file = local_db_schema_backup_file(:env => from_env, :timestamp=>most_recent_local_backup(from_env, 'schema')).strip
26
32
  remote_backup_file = local_db_backup_file(:env => from_env, :timestamp=>most_recent_local_backup(from_env, 'db')).strip
27
33
 
28
- puts "\n\033[1;42m Restoring database backup to #{to_env} environment FROM #{remote_backup_file}--#{from_env} using #{mysql_str}\033[0m"
34
+ puts "\n\033[1;42m Attempting to create #{to_env} database \033[0m"
29
35
  system(local_db_create.strip)
30
36
  cmd = ""
37
+
38
+ puts "\n\033[1;42m Restoring database schema to #{to_env} environment FROM #{remote_schema_backup_file}--#{from_env} using #{mysql_str}\033[0m"
39
+ cmd << <<-CMD
40
+ #{unzip} -c #{remote_schema_backup_file}.#{zip_ext} > #{remote_schema_backup_file} &&
41
+ #{mysql_str} < #{remote_schema_backup_file} &&
42
+ rm -f #{remote_schema_backup_file}
43
+ CMD
44
+
45
+ puts "\n\033[1;42m Restoring database data to #{to_env} environment FROM #{remote_backup_file}--#{from_env} using #{mysql_str}\033[0m"
46
+
31
47
  cmd << <<-CMD
32
48
  #{unzip} -c #{remote_backup_file}.#{zip_ext} > #{remote_backup_file} &&
33
49
  #{mysql_str} < #{remote_backup_file} &&
@@ -14,105 +14,134 @@
14
14
  # set :tar_content_backup_file, fetch(:blavosync_tar_content_backup_file, "#{shared_path}/backup_#{from_env}_content.tar.#{zip_ext}" )
15
15
  # set :db_backup_file, fetch(:blavosync_db_backup_file, "#{shared_path}/backup_#{from_env}_db.sql" )
16
16
  # set :db_backup_zip_file, fetch(:blavosync_db_backup_zip_file, "#{db_backup_file}.#{zip_ext}" )
17
- #
17
+ #
18
18
  Capistrano::Configuration.instance(:must_exist).load do |configuration|
19
19
 
20
20
  def rails_root
21
21
  Pathname.new('.').realpath
22
22
  end
23
- def content_dir
24
- exists?(:content_directory) ? fetch(:content_directory) : "system"
23
+ def content_dir
24
+ exists?(:content_directory) ? fetch(:content_directory) : "system"
25
25
  end
26
- def content_path
27
- File.join(fetch(:shared_path), content_dir)
28
- end
29
- def public_path
30
- File.join(fetch(:latest_release), 'public')
26
+ def exlude_table_command
27
+ exists?(:exclude_tables) ? fetch(:exclude_tables).collect {|table| " --ignore-table=#{mysql_db_for(from_env).strip}.#{table} " }.join(" ") : ""
31
28
  end
32
- def remote_backup_expires
33
- 100000
29
+
30
+ def content_path
31
+ File.join(fetch(:shared_path), content_dir)
32
+ end
33
+
34
+ def content_dir
35
+ exists?(:content_directory) ? fetch(:content_directory) : "system"
36
+ end
37
+
38
+ def public_path
39
+ File.join(fetch(:latest_release), 'public')
34
40
  end
35
- def zip
36
- "gzip"
41
+ def remote_backup_expires
42
+ 100000
37
43
  end
38
- def unzip
39
- "gunzip"
44
+ def zip
45
+ "gzip"
40
46
  end
41
- def zip_ext
42
- "gz"
47
+ def unzip
48
+ "gunzip"
43
49
  end
44
- def tmp_dir
45
- "tmp"
50
+ def zip_ext
51
+ "gz"
46
52
  end
47
- def content_sync_method
48
- exists?(:sync_method) ? sync_method : 'rsync'
53
+ def tmp_dir
54
+ "tmp"
49
55
  end
50
- def from_env
51
- (ENV['FROM_ENV'].nil? ? 'production' : ENV['RAILS_ENV'])
56
+ def content_sync_method
57
+ exists?(:sync_method) ? sync_method : 'rsync'
52
58
  end
53
- def to_env
54
- (ENV['TO_ENV'].nil? ? 'development' : ENV['TO_ENV'])
59
+ def from_env
60
+ (ENV['FROM_ENV'].nil? ? 'production' : ENV['RAILS_ENV'])
55
61
  end
56
- def rsync_content_backup_file
57
- "#{shared_path}/#{content_dir}"
62
+ def to_env
63
+ (ENV['TO_ENV'].nil? ? 'development' : ENV['TO_ENV'])
64
+ end
65
+ def rsync_content_backup_file
66
+ "#{shared_path}/#{content_dir}"
67
+ end
68
+ def tar_content_backup_file
69
+ "#{shared_path}/backup_#{from_env}_content.tar.#{zip_ext}"
70
+ end
71
+
72
+ def db_schema_backup_file
73
+ "#{shared_path}/backup_#{from_env}_schema.sql"
74
+
58
75
  end
59
- def tar_content_backup_file
60
- "#{shared_path}/backup_#{from_env}_content.tar.#{zip_ext}"
76
+
77
+ def db_schema_backup_zip_file
78
+ "#{db_schema_backup_file}.#{zip_ext}"
79
+
61
80
  end
62
- def db_backup_file
63
- "#{shared_path}/backup_#{from_env}_db.sql"
81
+
82
+ def db_backup_file
83
+ "#{shared_path}/backup_#{from_env}_db.sql"
64
84
  end
65
- def db_backup_zip_file
66
- "#{db_backup_file}.#{zip_ext}"
85
+
86
+ def db_backup_zip_file
87
+ "#{db_backup_file}.#{zip_ext}"
67
88
  end
68
-
89
+
69
90
  def local_content_backup_dir(args={})
70
91
  timestamp = args[:timestamp] || current_timestamp
71
92
  "#{tmp_dir}/#{fetch(:application)}-#{from_env}-content-#{timestamp.to_s.strip}"
72
93
  end
73
-
94
+
74
95
  def generate_remote_tar_content_backup
75
96
  run "cd #{fetch(:shared_path)} && tar czf #{rsync_content_backup_file} 'system'"
76
97
  end
77
-
98
+
78
99
  def local_db_conf(env = nil)
79
100
  env ||= fetch(:to_env)
80
101
  fetch(:config_structure, :rails).to_sym == :sls ?
81
102
  File.join('config', env.to_s, 'database.yml') :
82
103
  File.join('config', 'database.yml')
83
104
  end
84
-
105
+
85
106
  def pluck_pass_str(db_config)
86
107
  db_config['password'].nil? ? '' : "-p'#{db_config['password']}'"
87
108
  end
88
-
109
+
89
110
  def current_timestamp
90
111
  @current_timestamp ||= Time.now.to_i.to_s.strip
91
112
  end
92
-
113
+
93
114
  def retrieve_local_files(env, type)
94
115
  `ls -r #{tmp_dir} | awk -F"-" '{ if ($2 ~ /#{env}/ && $3 ~ /#{type}/) { print $4; } }'`.split(' ')
95
116
  end
96
-
117
+
97
118
  def most_recent_local_backup(env, type)
98
119
  retrieve_local_files(env, type).first.to_i
99
120
  end
100
-
121
+
101
122
  def last_mod_time(path)
102
123
  capture("stat -c%Y #{path}") rescue 0
103
124
  end
104
-
125
+
105
126
  def generate_remote_db_backup
106
- run "mysqldump #{mysql_connection_for(from_env)} > #{db_backup_file}"
127
+ run "mysqldump --opt --no-data #{mysql_connection_for(from_env)} > #{db_schema_backup_file}"
128
+ run "mysqldump --no-create-info #{exlude_table_command} #{mysql_connection_for(from_env)} > #{db_backup_file}"
107
129
  run "rm -f #{db_backup_zip_file} && #{zip} #{db_backup_file} && rm -f #{db_backup_file}"
130
+ run "rm -f #{db_schema_backup_zip_file} && #{zip} #{db_schema_backup_file} && rm -f #{db_schema_backup_file}"
108
131
  end
109
-
132
+
110
133
  def local_db_backup_file(args = {})
111
134
  env = args[:env] || 'production'
112
135
  timestamp = args[:timestamp] || current_timestamp
113
136
  "#{tmp_dir}/#{fetch(:application)}-#{env}-db-#{timestamp.to_s.strip}.sql"
114
137
  end
115
-
138
+
139
+ def local_db_schema_backup_file(args = {})
140
+ env = args[:env] || 'production'
141
+ timestamp = args[:timestamp] || current_timestamp
142
+ "#{tmp_dir}/#{fetch(:application)}-#{env}-schema-#{timestamp.to_s.strip}.sql"
143
+ end
144
+
116
145
  def mysql_connection_for(environment)
117
146
  db_settings = YAML.load_file(local_db_conf(environment))[environment]
118
147
  pass = pluck_pass_str(db_settings)
@@ -122,15 +151,15 @@ Capistrano::Configuration.instance(:must_exist).load do |configuration|
122
151
  database = (db_settings['database'].nil?) ? nil : " #{db_settings['database']}"
123
152
  [user, pass, host, socket, database ].join(" ")
124
153
  end
125
-
154
+
126
155
  def mysql_db_for(environment)
127
156
  restore_from = ENV['FROM'] || 'production'
128
157
  @from_db ||= YAML.load_file(local_db_conf(restore_from))[restore_from]
129
158
  @from_database ||= (@from_db['database'].nil?) ? nil : " #{@from_db['database']}"
130
159
  end
131
-
160
+
132
161
  namespace :local do
133
-
162
+
134
163
  desc <<-DESC
135
164
  Wrapper for local:sync_db and local:sync_content
136
165
  $> cap local:sync RAILS_ENV=production RESTORE_ENV=development
@@ -143,9 +172,9 @@ Capistrano::Configuration.instance(:must_exist).load do |configuration|
143
172
  rsync_content
144
173
  end
145
174
  end
146
-
175
+
147
176
  desc <<-DESC
148
- Wrapper for local:force_backup_db, local:force_backup_content, and the local:sync to get
177
+ Wrapper for local:force_backup_db, local:force_backup_content, and the local:sync to get
149
178
  a completely fresh set of data from the server
150
179
  $> cap local:sync RAILS_ENV=production RESTORE_ENV=development
151
180
  DESC
@@ -153,8 +182,8 @@ Capistrano::Configuration.instance(:must_exist).load do |configuration|
153
182
  force_backup_db
154
183
  force_backup_content
155
184
  sync
156
- end
157
-
185
+ end
186
+
158
187
  end
159
188
 
160
189
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blavosync
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - jayronc
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-05 00:00:00 -05:00
12
+ date: 2010-03-19 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies: []
15
15