Skip to content

Instantly share code, notes, and snippets.

@daz
Created April 4, 2015 07:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daz/a30780946cbaf32ea59a to your computer and use it in GitHub Desktop.
Save daz/a30780946cbaf32ea59a to your computer and use it in GitHub Desktop.
Capistrano task to export and import Wordpress database
# Requires WP-CLI on the client and server
namespace :db do
desc 'Backup Wordpress database to shared/backup/timestamp.sql.gz and create a latest.sql.gz symlink'
task :backup do
stamp = Time.now.utc.strftime('%Y%m%d%H%M%S')
filename = "#{fetch(:application)}_#{stamp}.sql.gz"
backup_path = "#{shared_path}/backup/#{filename}"
on roles(:app) do
execute "mkdir -p #{shared_path}/backup"
execute "cd #{current_path}; wp db export - | gzip > #{backup_path}"
execute "ln -sf #{backup_path} #{shared_path}/backup/latest.sql.gz"
end
end
task :pull do
desc 'Import backup database and overwrite local Wordpress'
ask :answer, 'Are you sure you want to overwrite your local database? [y/n] '
if fetch(:answer) == 'y'
on roles(:app) do |server|
run_locally do
execute "ssh #{server.user}@#{server.hostname} 'cat #{shared_path}/backup/latest.sql.gz' | gunzip | wp db import -"
end
end
else
puts 'Cancelled'
end
end
before 'db:pull', 'db:backup'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment