Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Taken from Joey Rivera's tutorial on Phing in the December 2013 issue of Web & PHP Magazine. For more info, see
<?xml version="1.0" encoding="UTF-8"?>
<project name="DB Post Deploy Restore" default="run">
<property file="properties" />
<!-- find out what db's need to be restored and loop through the process for each -->
<target name="run">
<input propertyname="production.user">(Required) Please enter your ssh user name:</input>
<foreach list="${dbs}" param="db" target="prepare" />
<target name="prepare">
<property file="${db}.properties" />
<property name="dump.path" value="${dump.folder}${dump.file}" />
<echo msg="Cleaning the environment" />
<delete dir="${dump.folder}" includeemptydirs="true" verbose="false" failonerror="false" />
<mkdir dir="${dump.folder}" />
<exec command="chmod 775 ${dump.folder}" checkreturn="true" />
<echo msg="Fetching DB backup" />
<echo>Getting latest db backup</echo>
<exec command="scp ${production.user}@${}:${production.db.backup} ${dump.path}" checkreturn="true" outputProperty="output.dump" />
<exec command="chmod 775 ${dump.path}" />
<!-- turn off all sites using postgres so we don't get traffic that will break the restore process -->
<foreach list="${sites}" param="site" target="" />
<!-- kick people off the server -->
<echo>Restarting db service</echo>
<exec command="ssh ${production.user}@${} 'sudo /sbin/service postgresql restart'" checkreturn="true" outputProperty="output.restart" logoutput="true" />
<foreach list="${}" param="env" target="restore" />
<!-- now lets turn the sites back on -->
<foreach list="${sites}" param="site" target="sites.on" />
<target name="">
<!-- add maintenance file -->
<echo>Adding maintenance file to ${site}/${maintenance.file}</echo>
<touch file="${site}/${maintenance.file}" />
<echo>Turning off site ${site}</echo>
<exec command="${phpctl.folder} stage php stop ${site}" checkreturn="true" outputProperty="output.sites.${site}.off" logoutput="true" />
<target name="sites.on">
<echo>Turning on site ${site}</echo>
<exec command=" ${phpctl.folder} stage php start ${site}" checkreturn="true" outputProperty="output.sites.${site}.on" logoutput="true" />
<echo>Removing maintenance file ${site}/${maintenance.file}</echo>
<delete file="${site}/${maintenance.file}" />
<target name="restore">
<property name="host" value="${}" />
<property name="name" value="${${env}}" />
<property name="user" value="${${env}.db.user}" />
<exec command="psql -h ${host} -U ${user} -d ${db.login.default} -c 'drop database ${name}'" checkreturn="true" outputProperty="output.delete" />
<echo msg="${output.delete}" />
<equals arg1="${env}" arg2="staging" />
<exec command="psql -h ${host} -U ${user} -c 'create database ${name} owner ${}' -d ${db.login.default}" checkreturn="true" outputProperty="output.create" />
<echo msg="${output.create}" />
<exec command="pg_restore -h ${host} -U ${user} -d ${name} ${dump.path}" checkreturn="true" outputProperty="output.restore" />
<echo msg="DB restored" />
<exec command="psql -h ${host} -U ${user} -c 'create database ${name} owner ${} template ${db.template}' -d ${db.login.default}" checkreturn="true" outputProperty="output.create" />
<echo msg="${output.create}" />
<!-- Now check if we need to manipulate data -->
<available file="${name}.sql" property="script.found" type="file" filepath="${script.folder}" />
<equals arg1="${script.found}" arg2="true" />
<property name="script.path" value="${script.folder}${name}.sql" />
<echo msg="Script found for ${name}, executing" />
<exec command="psql -h ${host} -U ${user} -f ${script.path} ${name}" checkreturn="true" outputProperty="output.file" />
<echo msg="${output.file}" />
// the properties file looks something like this...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.