Created
April 4, 2015 07:43
-
-
Save daz/a30780946cbaf32ea59a to your computer and use it in GitHub Desktop.
Capistrano task to export and import Wordpress database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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