Skip to content

Instantly share code, notes, and snippets.

@andrezrv
Last active Dec 21, 2015
Embed
What would you like to do?
Custom configuration file for WP-Stack, loading a new set of tasks from db-tasks.rb and shared-tasks.rb.
# Customize according to your needed configuration
set :application, "Application Name"
set :repository, "git@github.com:user/repo.git"
set :db_repository, 'git@github.com:user/repo-db.git'
set :scm, :git
# Or: `accurev`, `bzr`, `cvs`, `darcs`, `git`, `mercurial`, `perforce`, `subversion` or `none`
# This assumes you're using Git. Needed to run tasks in db-tasks.rb
set :git_user_name, "user"
set :git_user_email, "user@mail.com"
# Using Git Submodules?
set :git_enable_submodules, 1
# This should be the same as :deploy_to in production.rb
# This folder should be owned by user "deploy"
set :production_deploy_to, '/path/to/deploy'
# The domain name or IP address used for your staging environment
set :staging_domain, '127.0.0.1'
# Remote database credentials. Change to your username and database name.
set :db_remote_host, 'remote_host' # Generally "localhost"
set :db_remote_user, 'remote_user'
set :db_remote_password, 'remote_password'
set :db_remote_name, 'remote_name'
# Local database credentials. Change to your username and database name.
set :db_local_host, 'local_host' # Generally "localhost"
set :db_local_user, 'local_user'
set :db_local_password, 'local_password'
set :db_local_name, 'local_name'
# Places where you'd like to save your database dumps
# These folders should be owned by user "deploy"
set :local_dump_dir, '/local/path/to/dumps'
set :remote_dump_dir, '/remote/path/to/dumps'
# Path to your uploads folder
set :local_uploads_folder, '/local/path/to/uploads'
set :remote_uploads_folder, '/remote/path/to/uploads'
# Avoid tty fatal error
default_run_options[:pty] = true
# Environment names
# Needed to run tasks in db-tasks.rb
set :local_env_name, 'local_environment'
set :remote_env_name, 'remote_environment'
# Database
# Set the values for host, user, pass, and name for both production and staging.
set :wpdb do
{
:production => {
:host => "production_localhost",
:user => "production_user",
:password => "production_password",
:name => "production_name",
},
:staging => {
:host => "local_host",
:user => "local_user",
:password => "local_password",
:name => "local_name",
}
}
end
# Backup remote database before make symbolic link to current release
before "deploy:symlink", "remotedb:mysqlbackup"
# Load database specific tasks
loadFile 'lib/db-tasks.rb'
# Load tasks for shared files
loadFile 'lib/shared-tasks.rb'
namespace :localdb do
desc "Backup MySQL Local Database"
task :mysqlbackup, :roles => :web do
puts "Backing up MySQL local database..."
filename = "#{release_name}-#{local_env_name}.sql"
# Create folder for dumps, in case that it doesn't exist
system "mkdir -p #{local_dump_dir}"
if system "mysqldump -u#{db_local_user} -p#{db_local_password} #{db_local_name} > #{local_dump_dir}/#{filename}" then
puts "MySQL local database saved to #{local_dump_dir}/#{filename}"
else
puts "MySQL local database could not be saved."
end
end
desc "Restore Local MySQL Database"
task :mysqlrestore, :roles => :web do
puts "Searching for available local backups..."
# List contents from dumps folder
backups = `ls -1 #{local_dump_dir}/`.split("\n")
# Define default backup
default_backup = backups.last
puts "Available backups: "
puts backups
backup = Capistrano::CLI.ui.ask "Which backup would you like to restore? [#{default_backup}] "
backup_file = default_backup if backup.empty?
if system "mysql -u#{db_local_user} -p#{db_local_password} #{db_local_name} < #{local_dump_dir}/#{backup_file}" then
puts "Local database restored to backup saved in #{local_dump_dir}/#{backup_file}."
else
puts "Local database could not be restored from backup."
end
end
desc "Push local MySQL database backup to remote"
task :mysqlpushdump, :roles => :web do
puts "Searching for available local backups..."
# List contents from dumps folder
backups = `ls -1 #{local_dump_dir}/`.split("\n")
# Define default backup
default_backup = backups.last
puts "Available backups: "
puts backups
backup = Capistrano::CLI.ui.ask "Which backup would you like to push? [#{default_backup}] "
backup_file = default_backup if backup.empty?
current_host = capture("echo $CAPISTRANO:HOST$").strip
if system "scp #{local_dump_dir}/#{backup_file} #{user}@#{current_host}:#{remote_dump_dir}" then
puts "Local database uploaded to remote host at #{remote_dump_dir}/#{backup_file}."
else
puts "Local database could not be pushed from backup."
end
end
desc "Pull remote MySQL database backup to local"
task :mysqlpulldump, :roles => :web do
puts "Searching for available remote backups..."
# List contents from dumps folder
backups = capture("ls -1 #{remote_dump_dir}").split("\n")
# Define default backup
default_backup = backups.last
puts "Available backups: "
puts backups
backup = Capistrano::CLI.ui.ask "Which backup would you like to pull? [#{default_backup}] "
backup_file = default_backup if backup.empty?
current_host = capture("echo $CAPISTRANO:HOST$").strip
if system "scp #{user}@#{current_host}:#{remote_dump_dir}/#{backup_file} #{local_dump_dir}" then
puts "Remote database saved to local host at #{local_dump_dir}/#{backup_file}."
else
puts "Remote database could not be pulled from backup."
end
end
end
namespace :remotedb do
desc "Initialize database repository"
task :repoinit, :roles => :web do
begin
run "cd #{remote_dump_dir}; git init; git remote add origin #{db_repository}; git config --global user.name \"#{git_user_name}\"; git config --global user.email \"#{git_user_email}\""
rescue Exception => error
end
end
desc "Backup remote MySQL database"
task :mysqlbackup, :roles => :web do
puts "Backing up remote MySQL database..."
filename = "#{release_name}-#{remote_env_name}.sql"
# Create folder for dumps, in case that it doesn't exist
run "mkdir -p #{remote_dump_dir}"
begin
run "mysqldump -u#{db_remote_user} -p#{db_remote_password} #{db_remote_name} > #{remote_dump_dir}/#{filename}"
puts "Remote MySQL database saved to #{remote_dump_dir}/#{filename}"
puts "Cleaning #{remote_dump_dir} to show only the last 5 files."
run "cd #{remote_dump_dir}; (ls -t|head -n 5;ls)|sort|uniq -u|xargs rm;"
begin
run "cd #{remote_dump_dir}; git pull origin master; git add --all; git commit -m \"Cleaned dir and added #{filename}\"; git push -u origin master"
puts "#{remote_dump_dir}/#{filename} was added to Git repository."
rescue Exception => error
puts "No Git repository was found. You should run cap remotedb:repoinit before this command."
end
rescue Exception => error
puts "Remote MySQL database could not be saved."
end
end
desc "Restore MySQL Production Database"
task :mysqlrestore, :roles => :web do
puts "Searching for available remote backups..."
# List contents from dumps folder
backups = capture("ls -1 #{remote_dump_dir}").split("\n")
# Define default backup
default_backup = backups.last
puts "Available backups: "
puts backups
backup = Capistrano::CLI.ui.ask "Which backup would you like to restore? [#{default_backup}] "
backup_file = default_backup if backup.empty?
begin
run "mysql -u#{db_remote_user} -p#{db_remote_password} #{db_remote_name} < #{remote_dump_dir}/#{backup_file}"
puts "Remote database restored to backup saved in #{local_dump_dir}/#{backup_file}."
rescue Exception => error
puts "Remote database could not be restored from backup."
end
end
end
# These tasks assume that your local and remote deploy users are both sudoers.
namespace :shared do
desc "Pull uploaded files from remote location."
task :pull do
current_host = capture("echo $CAPISTRANO:HOST$").strip
run "cd #{remote_uploads_folder}; sudo zip -r uploads .;"
system "cd #{local_uploads_folder}; sudo mkdir #{release_name}; sudo mkdir ../#{release_name}; sudo mv * ../#{release_name}/; sudo scp #{user}@#{current_host}:#{remote_uploads_folder}/uploads.zip #{local_uploads_folder}; sudo unzip uploads.zip; sudo rm -rf uploads.zip ../#{release_name};"
run "cd #{remote_uploads_folder}; sudo rm -f uploads.zip"
end
desc "Push uploaded files to remote location."
task :push do
current_host = capture("echo $CAPISTRANO:HOST$").strip
puts "Current user is #{user}"
system "cd #{local_uploads_folder}; sudo zip -r uploads .;"
run "cd #{remote_uploads_folder}; sudo mkdir #{release_name}; sudo mkdir ../#{release_name}; sudo mv * ../#{release_name}/"
system "sudo scp #{local_uploads_folder}/uploads.zip #{user}@#{current_host}:#{remote_uploads_folder}"
run "cd #{remote_uploads_folder}; sudo unzip uploads.zip; sudo rm -rf uploads.zip ../#{release_name}; sudo chown -R #{user}:#{user} #{remote_uploads_folder}"
system "cd #{local_uploads_folder}; sudo rm -f uploads.zip"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment