Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

DB and file syncing to staging with media_master designation

  • Loading branch information...
commit 05afe8df1808d90b0340f206f7c8a765e1b1e936 1 parent 50afe56
@markjaquith authored
View
23 Capistrano/config/SAMPLE.config.rb
@@ -8,4 +8,27 @@
# Using Git Submodules?
set :git_enable_submodules, 1
+# Designate a media master and path. This should be a server that always has the latest production user-uploaded files
+# The path you designate here should be the same as your production :deploy_to value
+media_master '127.0.0.1', '/srv/www/example.com'
+
+# Database
+# Set the values for host, user, pass, and name for both production and staging.
+set :wpdb do
+ {
+ :production => {
+ :host => 'PRODUCTION DB HOST',
+ :user => 'PRODUCTION DB USER',
+ :password => 'PRODUCTION DB PASS',
+ :name => 'PRODUCTION DB NAME',
+ },
+ :staging => {
+ :host => 'STAGING DB HOST',
+ :user => 'STAGING DB USER',
+ :password => 'STAGING DB PASS',
+ :name => 'STAGING DB NAME',
+ }
+ }
+end
+
# You're not done! You must also configure production.rb and staging.rb
View
4 Capistrano/lib/misc.rb
@@ -7,3 +7,7 @@ def loadFile file
exit 1
end
end
+
+def media_master server, path
+ set :master, Capistrano::ServerDefinition.new( server, :path => path )
+end
View
30 Capistrano/lib/tasks.rb
@@ -43,3 +43,33 @@
end
end
+namespace :db do
+ desc "Syncs the staging database (and uploads) from production"
+ task :sync, :roles => :web do
+ if stage != :staging then
+ puts "[ERROR] You must run db:sync from staging with cap staging db:sync"
+ else
+ puts "Hang on... this might take a while."
+ random = rand( 10 ** 5 ).to_s.rjust( 5, '0' )
+ p = wpdb[ :production ]
+ s = wpdb[ :staging ]
+ puts "db:sync"
+ puts stage
+ system "mysqldump -u #{p[:user]} --result-file=/tmp/wpstack-#{random}.sql -h #{p[:host]} -p#{p[:password]} #{p[:name]}"
+ system "mysql -u #{s[:user]} -h #{s[:host]} -p#{s[:password]} #{s[:name]} < /tmp/wpstack-#{random}.sql && rm /tmp/wpstack-#{random}.sql"
+ puts "Database synced to staging"
+ # memcached.restart
+ puts "Memcached flushed"
+ # Now to copy files
+ find_servers( :roles => :web ).each do |server|
+ system "rsync -avz --delete #{master.host}:#{master.options[:path]}/files/ #{server}:#{shared_path}/files/"
+ end
+ end
+ end
+ desc "Sets the database credentials in wp-config.php"
+ task :make_config do
+ {:'%%DB_NAME%%' => wpdb[stage][:name], :'%%DB_USER%%' => wpdb[stage][:user], :'%%DB_PASSWORD%%' => wpdb[stage][:password], :'%%DB_HOST%%' => wpdb[stage][:host]}.each do |k,v|
+ run "sed -i 's/###{k}##/#{v}/' #{release_path}/wp-config.php", :roles => :web
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.