wslave 0.2.2

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.
@@ -0,0 +1,288 @@
1
+ require 'yaml'
2
+ require 'date'
3
+ require 'wslave_sage'
4
+
5
+ opts = YAML.load_file('config/definitions.yml')
6
+ db_info = YAML.load_file('config/database.yml')
7
+
8
+ deploy_user = opts['deployer']['user']
9
+ deploy_group = opts['deployer']['www_data_group']
10
+ host_addr = opts['deployer']['host']['production']
11
+ multisite_root = opts['deployer']['root']
12
+ site_fqdn = opts['deployer']['fqdn']['production']
13
+
14
+ disable_rsync = (opts.include?('options') && opts['options'].include?('rsync_enabled') &&
15
+ opts['options']['rsync_enabled'] == false)
16
+
17
+ if (opts['deployer'].include?('branch') && opts['deployer']['branch'].include?('production'))
18
+ set :branch, opts['deployer']['branch']['production']
19
+ end
20
+
21
+ role :web, "#{deploy_user}@#{host_addr}"
22
+
23
+ set :tmp_dir, "#{multisite_root}/tmp"
24
+ deploy_path = "#{multisite_root}/#{site_fqdn}"
25
+
26
+ set :linked_dirs, %w{public/wp-content/uploads public/wordpress public/wp-content/upgrade public/wp-content/plugins tmp}
27
+ set :linked_files, %w{public/wp-config.php}
28
+
29
+ set :deploy_to, deploy_path
30
+
31
+ namespace :deploy do
32
+ desc "Generate wp-config.php for profile"
33
+ task :wp_config do
34
+ on roles(:web) do
35
+ invoke 'deploy:check:make_linked_dirs'
36
+ require_relative '../deploy-tools/gen-wp-config'
37
+ FileUtils.mkdir('./tmp') unless Dir.exist?('./tmp')
38
+ GenerateWPConfig('production', './tmp')
39
+ upload! './tmp/wp-config.php', "#{deploy_path}/shared/public/wp-config.php"
40
+ end
41
+ end
42
+
43
+ desc 'Syncs the wordpress directory with rsync'
44
+ task :sync_wp do
45
+ on roles(:web) do
46
+ `rsync -avzPhu --delete ./public/wordpress/ #{deploy_user}@#{host_addr}:#{deploy_path}/shared/public/wordpress/`
47
+ end
48
+ end
49
+
50
+ desc 'Uploads the uploads directory'
51
+ task :upload_wp do
52
+ on roles(:web) do
53
+ upload! './public/wordpress', "#{deploy_path}/shared/public/", recursive: true
54
+ end
55
+ end
56
+
57
+ desc 'Syncs the uploads directory with rsync'
58
+ task :sync_uploads do
59
+ on roles(:web) do
60
+ `rsync -avzPhu --delete ./public/wp-content/uploads/ #{deploy_user}@#{host_addr}:#{deploy_path}/shared/public/wp-content/uploads/`
61
+ end
62
+ end
63
+
64
+ desc 'Uploads the uploads directory'
65
+ task :upload_uploads do
66
+ on roles(:web) do
67
+ upload! './public/wp-content/uploads', "#{deploy_path}/shared/public/wp-content/", recursive: true
68
+ end
69
+ end
70
+
71
+ desc 'Syncs the plugins directory with rsync'
72
+ task :sync_plugins do
73
+ on roles(:web) do
74
+ `rsync -avzPhu --delete ./public/wp-content/plugins/ #{deploy_user}@#{host_addr}:#{deploy_path}/shared/public/wp-content/plugins/`
75
+ end
76
+ end
77
+
78
+ desc 'Uploads the plugins directory'
79
+ task :upload_plugins do
80
+ on roles(:web) do
81
+ upload! './public/wp-content/plugins', "#{deploy_path}/shared/public/wp-content/", recursive: true
82
+ end
83
+ end
84
+
85
+ desc 'Builds and Syncs the project Sage theme'
86
+ task :sync_sage_theme do
87
+ on roles(:web) do
88
+ wss = WSlaveSage.new()
89
+ sage_theme_name = wss.theme_name?
90
+ if (sage_theme_name == '')
91
+ puts "Couldn't find a Sage theme for this project."
92
+ else
93
+ wss.production()
94
+ `rsync -avzPhu --delete ./public/wp-content/themes/#{sage_theme_name}/vendor/ #{deploy_user}@#{host_addr}:#{deploy_path}/current/public/wp-content/themes/#{sage_theme_name}/vendor/`
95
+ `rsync -avzPhu --delete ./public/wp-content/themes/#{sage_theme_name}/dist/ #{deploy_user}@#{host_addr}:#{deploy_path}/current/public/wp-content/themes/#{sage_theme_name}/dist/`
96
+ end
97
+ end
98
+ end
99
+
100
+ desc 'Builds and Uploads the project Sage theme'
101
+ task :upload_sage_theme do
102
+ on roles(:web) do
103
+ wss = WSlaveSage.new()
104
+ sage_theme_name = wss.theme_name?
105
+ if (sage_theme_name == '')
106
+ puts "Couldn't find a Sage theme for this project."
107
+ else
108
+ wss.production()
109
+ upload! "./public/wp-content/themes/#{sage_theme_name}/vendor/", "#{deploy_path}/current/public/wp-content/themes/#{sage_theme_name}/", recursive: true
110
+ upload! "./public/wp-content/themes/#{sage_theme_name}/dist/", "#{deploy_path}/current/public/wp-content/themes/#{sage_theme_name}/", recursive: true
111
+ end
112
+ end
113
+ end
114
+
115
+ desc 'Builds and Deploys the project Sage theme'
116
+ task :sage do
117
+ on roles(:web) do
118
+ if disable_rsync
119
+ invoke('deploy:upload_sage_theme')
120
+ else
121
+ invoke('deploy:sync_sage_theme')
122
+ end
123
+ end
124
+ end
125
+
126
+ desc 'Finds and replaces localhost:8000 and your Staging address with the Production address'
127
+ task :chikan do
128
+ on roles(:web) do
129
+ puts 'Replacing localhost:8000 and Production URLs with Staging URLs...'
130
+
131
+ # Set an anchor to first homogonize instances of URL's, then replace all the anchors
132
+ anchor = "URL_REPLACEMENT_ANCHOR_00000"
133
+
134
+ # Create a backup, download it, and remove remote copy
135
+ execute "mkdir -p #{deploy_path}/db/tmp"
136
+ execute "mysqldump --opt --user=#{db_info['production']['username']} --password=#{db_info['production']['password']} --host=#{db_info['production']['host']} #{db_info['production']['database']} > #{deploy_path}/db/tmp/wordpress.sql"
137
+ FileUtils.mkdir_p('./db/tmp') unless Dir.exist?('./db/tmp')
138
+ download! "#{deploy_path}/db/tmp/wordpress.sql", "db/tmp/wordpress.sql"
139
+ execute "rm #{deploy_path}/db/tmp/*.sql"
140
+
141
+ # Regex replace in file
142
+ db_data = File.read('db/tmp/wordpress.sql')
143
+
144
+ # This may seem roundabout, but in order to avoid mangling the target URL we need to first
145
+ # replace instances of it with something that won't match
146
+ db_data = db_data.gsub(/#{opts['deployer']['fqdn']['production']}/, anchor)
147
+
148
+ # Set staging URL's to the anchor
149
+ if opts['deployer']['fqdn']['staging'] != ''
150
+ db_data = db_data.gsub(/#{opts['deployer']['fqdn']['staging']}/, anchor)
151
+ end
152
+
153
+ # Set localhost entries to the anchor
154
+ db_data = db_data.gsub(/localhost\%3A8000/, anchor)
155
+ db_data = db_data.gsub(/localhost:8000/, anchor)
156
+
157
+ # Replace anchors with the correct target URL
158
+ db_data = db_data.gsub(anchor, "#{opts['deployer']['fqdn']['production']}")
159
+
160
+ # Save results
161
+ File.open('db/tmp/wordpress.sql', "w") {|file| file.puts db_data }
162
+
163
+ # Upload file and seed
164
+ upload! 'db/tmp/wordpress.sql', "#{deploy_path}/db/tmp/wordpress.sql"
165
+ execute "mysql -h#{db_info['production']['host']} -u#{db_info['production']['username']} -p#{db_info['production']['password']} #{db_info['production']['database']} < #{deploy_path}/db/tmp/wordpress.sql"
166
+ execute "rm #{deploy_path}/db/tmp/*.sql"
167
+
168
+ # Remove work file
169
+ # `rm db/tmp/wordpress.sql`
170
+ end
171
+ end
172
+
173
+ desc 'Sets ownership permissions'
174
+ task :set_permissions do
175
+ on roles(:web) do
176
+ puts 'Setting permissions'
177
+ if deploy_group != nil
178
+ puts "Recrusively setting group to #{deploy_group}..."
179
+ execute "chown -R :#{deploy_group} #{deploy_path}"
180
+ puts 'Allowing group level Write permission...'
181
+ execute "chmod -R g+w #{deploy_path}"
182
+ end
183
+ end
184
+ end
185
+
186
+ desc 'Perform special seed tasks required on intial seed'
187
+ task :initial do
188
+ on roles(:web) do
189
+ invoke!('deploy:check:directories')
190
+ invoke!('deploy:check:linked_dirs')
191
+ invoke!('deploy:check:make_linked_dirs')
192
+ invoke('deploy:wp_config')
193
+ if disable_rsync
194
+ invoke('deploy:upload_wp')
195
+ invoke('deploy:upload_plugins')
196
+ invoke('deploy:upload_uploads')
197
+ else
198
+ invoke('deploy:sync_wp')
199
+ invoke('deploy:sync_plugins')
200
+ invoke('deploy:sync_uploads')
201
+ end
202
+ invoke('deploy')
203
+ invoke('db:seed')
204
+ invoke('deploy:chikan')
205
+ invoke('deploy:sage')
206
+ invoke('deploy:set_permissions')
207
+ end
208
+ end
209
+
210
+ desc 'Clear out remote DB tables and delete all remote files in deploy target directory'
211
+ task :destruct do
212
+ on roles(:web) do
213
+ execute "mysql --user=#{db_info['production']['username']} " \
214
+ "--password=#{db_info['production']['password']} --host=#{db_info['production']['host']} " \
215
+ "-Nse 'show tables' #{db_info['production']['database']} | " \
216
+ "while read table; do echo \"drop table $table;\"; done | " \
217
+ "mysql --user=#{db_info['production']['username']} " \
218
+ "--password=#{db_info['production']['password']} --host=#{db_info['production']['host']} " \
219
+ "#{db_info['production']['database']}"
220
+
221
+ execute "rm -rf #{deploy_path}/*"
222
+ end
223
+ end
224
+ end
225
+
226
+ namespace :db do
227
+ desc "Backup DB"
228
+ task :backup do
229
+ on roles(:web) do
230
+ timestamp = DateTime.now
231
+ execute "mkdir -p #{deploy_path}/db/backups/production/"
232
+ execute "mysqldump --opt --user=#{db_info['production']['username']} --password=#{db_info['production']['password']} --host=#{db_info['production']['host']} #{db_info['production']['database']} > #{deploy_path}/db/backups/production/#{timestamp}.sql"
233
+ FileUtils.mkdir_p('./db/production') unless Dir.exist?('./db/production')
234
+ download! "#{deploy_path}/db/backups/production/#{timestamp}.sql", "db/production/wordpress.sql"
235
+ end
236
+ end
237
+
238
+ desc "Clear remote backup records"
239
+ task :clear_remote_backups do
240
+ on roles(:web) do
241
+ execute "rm #{deploy_path}/db/backups/production/*.sql"
242
+ end
243
+ end
244
+
245
+ desc 'Seed the "active" database'
246
+ task :seed do
247
+ on roles(:web) do
248
+ if File.exist? 'db/active/wordpress.sql'
249
+ execute "mkdir -p #{deploy_path}/db/"
250
+ upload! 'db/active/wordpress.sql', "#{deploy_path}/db/wordpress.sql"
251
+ execute "mysql -h#{db_info['production']['host']} -u#{db_info['production']['username']} -p#{db_info['production']['password']} #{db_info['production']['database']} < #{deploy_path}/db/wordpress.sql"
252
+ end
253
+ end
254
+ end
255
+ end
256
+
257
+ namespace :data do
258
+ desc "Backup data"
259
+ task :backup do
260
+ on roles(:web) do
261
+ download! "#{deploy_path}/shared/public/wp-content/uploads", "./public/wp-content/", recursive: true
262
+ download! "#{deploy_path}/shared/public/wp-content/plugins", "./public/wp-content/", recursive: true
263
+ download! "#{deploy_path}/shared/public/wp-content/upgrade", "./public/wp-content/", recursive: true
264
+ download! "#{deploy_path}/current/public/wp-content/themes", "./public/wp-content/", recursive: true
265
+ end
266
+ end
267
+
268
+ desc "Backup data with rsync"
269
+ task :sync_backup do
270
+ on roles(:web) do
271
+ puts "Syncing Backup..."
272
+ `rsync -avzPhu --delete #{deploy_user}@#{host_addr}:#{deploy_path}/shared/public/wp-content/uploads/ ./public/wp-content/uploads/`
273
+ `rsync -avzPhu --delete #{deploy_user}@#{host_addr}:#{deploy_path}/shared/public/wp-content/plugins/ ./public/wp-content/plugins/`
274
+ `rsync -avzPhu --delete #{deploy_user}@#{host_addr}:#{deploy_path}/shared/public/wp-content/upgrade/ ./public/wp-content/upgrade/`
275
+ `rsync -avzPhu --delete #{deploy_user}@#{host_addr}:#{deploy_path}/current/public/wp-content/themes/ ./public/wp-content/themes/`
276
+ end
277
+ end
278
+ end
279
+
280
+ desc 'Backup DB and remote uploads/content'
281
+ task :backup do
282
+ invoke('db:backup')
283
+ if disable_rsync
284
+ invoke('data:backup')
285
+ else
286
+ invoke('data:sync_backup')
287
+ end
288
+ end
@@ -0,0 +1,288 @@
1
+ require 'yaml'
2
+ require 'date'
3
+ require 'wslave_sage'
4
+
5
+ opts = YAML.load_file('config/definitions.yml')
6
+ db_info = YAML.load_file('config/database.yml')
7
+
8
+ deploy_user = opts['deployer']['user']
9
+ deploy_group = opts['deployer']['www_data_group']
10
+ host_addr = opts['deployer']['host']['staging']
11
+ multisite_root = opts['deployer']['root']
12
+ site_fqdn = opts['deployer']['fqdn']['staging']
13
+
14
+ disable_rsync = (opts.include?('options') && opts['options'].include?('rsync_enabled') &&
15
+ opts['options']['rsync_enabled'] == false)
16
+
17
+ if (opts['deployer'].include?('branch') && opts['deployer']['branch'].include?('staging'))
18
+ set :branch, opts['deployer']['branch']['staging']
19
+ end
20
+
21
+ role :web, "#{deploy_user}@#{host_addr}"
22
+
23
+ set :tmp_dir, "#{multisite_root}/tmp"
24
+ deploy_path = "#{multisite_root}/#{site_fqdn}"
25
+
26
+ set :linked_dirs, %w{public/wp-content/uploads public/wordpress public/wp-content/upgrade public/wp-content/plugins tmp}
27
+ set :linked_files, %w{public/wp-config.php}
28
+
29
+ set :deploy_to, deploy_path
30
+
31
+ namespace :deploy do
32
+ desc "Generate wp-config.php for profile"
33
+ task :wp_config do
34
+ on roles(:web) do
35
+ invoke 'deploy:check:make_linked_dirs'
36
+ require_relative '../deploy-tools/gen-wp-config'
37
+ FileUtils.mkdir('./tmp') unless Dir.exist?('./tmp')
38
+ GenerateWPConfig('staging', './tmp')
39
+ upload! './tmp/wp-config.php', "#{deploy_path}/shared/public/wp-config.php"
40
+ end
41
+ end
42
+
43
+ desc 'Syncs the wordpress directory with rsync'
44
+ task :sync_wp do
45
+ on roles(:web) do
46
+ `rsync -avzPhu --delete ./public/wordpress/ #{deploy_user}@#{host_addr}:#{deploy_path}/shared/public/wordpress/`
47
+ end
48
+ end
49
+
50
+ desc 'Uploads the uploads directory'
51
+ task :upload_wp do
52
+ on roles(:web) do
53
+ upload! './public/wordpress', "#{deploy_path}/shared/public/", recursive: true
54
+ end
55
+ end
56
+
57
+ desc 'Syncs the uploads directory with rsync'
58
+ task :sync_uploads do
59
+ on roles(:web) do
60
+ `rsync -avzPhu --delete ./public/wp-content/uploads/ #{deploy_user}@#{host_addr}:#{deploy_path}/shared/public/wp-content/uploads/`
61
+ end
62
+ end
63
+
64
+ desc 'Uploads the uploads directory'
65
+ task :upload_uploads do
66
+ on roles(:web) do
67
+ upload! './public/wp-content/uploads', "#{deploy_path}/shared/public/wp-content/", recursive: true
68
+ end
69
+ end
70
+
71
+ desc 'Syncs the plugins directory with rsync'
72
+ task :sync_plugins do
73
+ on roles(:web) do
74
+ `rsync -avzPhu --delete ./public/wp-content/plugins/ #{deploy_user}@#{host_addr}:#{deploy_path}/shared/public/wp-content/plugins/`
75
+ end
76
+ end
77
+
78
+ desc 'Uploads the plugins directory'
79
+ task :upload_plugins do
80
+ on roles(:web) do
81
+ upload! './public/wp-content/plugins', "#{deploy_path}/shared/public/wp-content/", recursive: true
82
+ end
83
+ end
84
+
85
+ desc 'Builds and Syncs the project Sage theme'
86
+ task :sync_sage_theme do
87
+ on roles(:web) do
88
+ wss = WSlaveSage.new()
89
+ sage_theme_name = wss.theme_name?
90
+ if (sage_theme_name == '')
91
+ puts "Couldn't find a Sage theme for this project."
92
+ else
93
+ wss.production()
94
+ `rsync -avzPhu --delete ./public/wp-content/themes/#{sage_theme_name}/vendor/ #{deploy_user}@#{host_addr}:#{deploy_path}/current/public/wp-content/themes/#{sage_theme_name}/vendor/`
95
+ `rsync -avzPhu --delete ./public/wp-content/themes/#{sage_theme_name}/dist/ #{deploy_user}@#{host_addr}:#{deploy_path}/current/public/wp-content/themes/#{sage_theme_name}/dist/`
96
+ end
97
+ end
98
+ end
99
+
100
+ desc 'Builds and Uploads the project Sage theme'
101
+ task :upload_sage_theme do
102
+ on roles(:web) do
103
+ wss = WSlaveSage.new()
104
+ sage_theme_name = wss.theme_name?
105
+ if (sage_theme_name == '')
106
+ puts "Couldn't find a Sage theme for this project."
107
+ else
108
+ wss.production()
109
+ upload! "./public/wp-content/themes/#{sage_theme_name}/vendor/", "#{deploy_path}/current/public/wp-content/themes/#{sage_theme_name}/", recursive: true
110
+ upload! "./public/wp-content/themes/#{sage_theme_name}/dist/", "#{deploy_path}/current/public/wp-content/themes/#{sage_theme_name}/", recursive: true
111
+ end
112
+ end
113
+ end
114
+
115
+ desc 'Builds and Deploys the project Sage theme'
116
+ task :sage do
117
+ on roles(:web) do
118
+ if disable_rsync
119
+ invoke('deploy:upload_sage_theme')
120
+ else
121
+ invoke('deploy:sync_sage_theme')
122
+ end
123
+ end
124
+ end
125
+
126
+ desc 'Finds and replaces localhost:8000 and your Production address with the Staging address'
127
+ task :chikan do
128
+ on roles(:web) do
129
+ puts 'Replacing localhost:8000 and Production URLs with Staging URLs...'
130
+
131
+ # Set an anchor to first homogonize instances of URL's, then replace all the anchors
132
+ anchor = "URL_REPLACEMENT_ANCHOR_00000"
133
+
134
+ # Create a backup, download it, and remove remote copy
135
+ execute "mkdir -p #{deploy_path}/db/tmp"
136
+ execute "mysqldump --opt --user=#{db_info['staging']['username']} --password=#{db_info['staging']['password']} --host=#{db_info['staging']['host']} #{db_info['staging']['database']} > #{deploy_path}/db/tmp/wordpress.sql"
137
+ FileUtils.mkdir_p('./db/tmp') unless Dir.exist?('./db/tmp')
138
+ download! "#{deploy_path}/db/tmp/wordpress.sql", "db/tmp/wordpress.sql"
139
+ execute "rm #{deploy_path}/db/tmp/*.sql"
140
+
141
+ # Regex replace in file
142
+ db_data = File.read('db/tmp/wordpress.sql')
143
+
144
+ # This may seem roundabout, but in order to avoid mangling the target URL we need to first
145
+ # replace instances of it with something that won't match
146
+ db_data = db_data.gsub(/#{opts['deployer']['fqdn']['staging']}/, anchor)
147
+
148
+ # Set production URL's to the anchor
149
+ if opts['deployer']['fqdn']['production'] != ''
150
+ db_data = db_data.gsub(/#{opts['deployer']['fqdn']['production']}/, anchor)
151
+ end
152
+
153
+ # Set localhost entries to the anchor
154
+ db_data = db_data.gsub(/localhost\%3A8000/, anchor)
155
+ db_data = db_data.gsub(/localhost:8000/, anchor)
156
+
157
+ # Replace anchors with the correct target URL
158
+ db_data = db_data.gsub(anchor, "#{opts['deployer']['fqdn']['staging']}")
159
+
160
+ # Save results
161
+ File.open('db/tmp/wordpress.sql', "w") {|file| file.puts db_data }
162
+
163
+ # Upload file and seed
164
+ upload! 'db/tmp/wordpress.sql', "#{deploy_path}/db/tmp/wordpress.sql"
165
+ execute "mysql -h#{db_info['staging']['host']} -u#{db_info['staging']['username']} -p#{db_info['staging']['password']} #{db_info['staging']['database']} < #{deploy_path}/db/tmp/wordpress.sql"
166
+ execute "rm #{deploy_path}/db/tmp/*.sql"
167
+
168
+ # Remove work file
169
+ # `rm db/tmp/wordpress.sql`
170
+ end
171
+ end
172
+
173
+ desc 'Sets ownership permissions'
174
+ task :set_permissions do
175
+ on roles(:web) do
176
+ puts 'Setting permissions'
177
+ if deploy_group != nil
178
+ puts "Recrusively setting group to #{deploy_group}..."
179
+ execute "chown -R :#{deploy_group} #{deploy_path}"
180
+ puts 'Allowing group level Write permission...'
181
+ execute "chmod -R g+w #{deploy_path}"
182
+ end
183
+ end
184
+ end
185
+
186
+ desc 'Perform special seed tasks required on intial seed'
187
+ task :initial do
188
+ on roles(:web) do
189
+ invoke!('deploy:check:directories')
190
+ invoke!('deploy:check:linked_dirs')
191
+ invoke!('deploy:check:make_linked_dirs')
192
+ invoke('deploy:wp_config')
193
+ if disable_rsync
194
+ invoke('deploy:upload_wp')
195
+ invoke('deploy:upload_plugins')
196
+ invoke('deploy:upload_uploads')
197
+ else
198
+ invoke('deploy:sync_wp')
199
+ invoke('deploy:sync_plugins')
200
+ invoke('deploy:sync_uploads')
201
+ end
202
+ invoke('deploy')
203
+ invoke('db:seed')
204
+ invoke('deploy:chikan')
205
+ invoke('deploy:sage')
206
+ invoke('deploy:set_permissions')
207
+ end
208
+ end
209
+
210
+ desc 'Clear out remote DB tables and delete all remote files in deploy target directory'
211
+ task :destruct do
212
+ on roles(:web) do
213
+ execute "mysql --user=#{db_info['staging']['username']} " \
214
+ "--password=#{db_info['staging']['password']} --host=#{db_info['staging']['host']} " \
215
+ "-Nse 'show tables' #{db_info['staging']['database']} | " \
216
+ "while read table; do echo \"drop table $table;\"; done | " \
217
+ "mysql --user=#{db_info['staging']['username']} " \
218
+ "--password=#{db_info['staging']['password']} --host=#{db_info['staging']['host']} " \
219
+ "#{db_info['staging']['database']}"
220
+
221
+ execute "rm -rf #{deploy_path}/*"
222
+ end
223
+ end
224
+ end
225
+
226
+ namespace :db do
227
+ desc "Backup DB"
228
+ task :backup do
229
+ on roles(:web) do
230
+ timestamp = DateTime.now
231
+ execute "mkdir -p #{deploy_path}/db/backups/staging/"
232
+ execute "mysqldump --opt --user=#{db_info['staging']['username']} --password=#{db_info['staging']['password']} --host=#{db_info['staging']['host']} #{db_info['staging']['database']} > #{deploy_path}/db/backups/staging/#{timestamp}.sql"
233
+ FileUtils.mkdir_p('./db/staging') unless Dir.exist?('./db/staging')
234
+ download! "#{deploy_path}/db/backups/staging/#{timestamp}.sql", "db/staging/wordpress.sql"
235
+ end
236
+ end
237
+
238
+ desc "Clear remote backup records"
239
+ task :clear_remote_backups do
240
+ on roles(:web) do
241
+ execute "rm #{deploy_path}/db/backups/staging/*.sql"
242
+ end
243
+ end
244
+
245
+ desc 'Seed the "active" database'
246
+ task :seed do
247
+ on roles(:web) do
248
+ if File.exist? 'db/active/wordpress.sql'
249
+ execute "mkdir -p #{deploy_path}/db/"
250
+ upload! 'db/active/wordpress.sql', "#{deploy_path}/db/wordpress.sql"
251
+ execute "mysql -h#{db_info['staging']['host']} -u#{db_info['staging']['username']} -p#{db_info['staging']['password']} #{db_info['staging']['database']} < #{deploy_path}/db/wordpress.sql"
252
+ end
253
+ end
254
+ end
255
+ end
256
+
257
+ namespace :data do
258
+ desc "Backup data"
259
+ task :backup do
260
+ on roles(:web) do
261
+ download! "#{deploy_path}/shared/public/wp-content/uploads", "./public/wp-content/", recursive: true
262
+ download! "#{deploy_path}/shared/public/wp-content/plugins", "./public/wp-content/", recursive: true
263
+ download! "#{deploy_path}/shared/public/wp-content/upgrade", "./public/wp-content/", recursive: true
264
+ download! "#{deploy_path}/current/public/wp-content/themes", "./public/wp-content/", recursive: true
265
+ end
266
+ end
267
+
268
+ desc "Backup data with rsync"
269
+ task :sync_backup do
270
+ on roles(:web) do
271
+ puts "Syncing Backup..."
272
+ `rsync -avzPhu --delete #{deploy_user}@#{host_addr}:#{deploy_path}/shared/public/wp-content/uploads/ ./public/wp-content/uploads/`
273
+ `rsync -avzPhu --delete #{deploy_user}@#{host_addr}:#{deploy_path}/shared/public/wp-content/plugins/ ./public/wp-content/plugins/`
274
+ `rsync -avzPhu --delete #{deploy_user}@#{host_addr}:#{deploy_path}/shared/public/wp-content/upgrade/ ./public/wp-content/upgrade/`
275
+ `rsync -avzPhu --delete #{deploy_user}@#{host_addr}:#{deploy_path}/current/public/wp-content/themes/ ./public/wp-content/themes/`
276
+ end
277
+ end
278
+ end
279
+
280
+ desc 'Backup DB and remote uploads/content'
281
+ task :backup do
282
+ invoke('db:backup')
283
+ if disable_rsync
284
+ invoke('data:backup')
285
+ else
286
+ invoke('data:sync_backup')
287
+ end
288
+ end