Skip to content

Instantly share code, notes, and snippets.

@stephencroberts
Last active December 29, 2015 04:58
Show Gist options
  • Save stephencroberts/7618058 to your computer and use it in GitHub Desktop.
Save stephencroberts/7618058 to your computer and use it in GitHub Desktop.
Pushes source site to destination testing site
#!/bin/bash
#
# Usage: pushtotesting.sh <source> <destination>
# Summary: Pushes source site to destination testing site
# Description: Assuming the source site updates from the master branch, and
# the testing site the develop branch, this script will:
#
# 1. Fetch updates from the remote repo
# 2. Discard all local changes
# 3. Checkout and pull the develop branch
# 4. Checkout the master branch
# 5. Sync files from source
# 6. Apply site-specific setup SQL
# 7. Merge the develop branch into the master branch
# 8. Import the develop SQL
#
# Essentially, the testing site is copied identically from the source,
# then updated to reflect any new changes in the develop branch.
#
# Hold non-fatal errors to show at the end
err=''
# Parses out database info from Joomla! config file
jconfig ()
{
printf "Checking for Joomla! configuration..."
if [ ! -f $1 ]; then
printf "failed\n\nCan't find $1\n"
exit 1
fi
printf "done\nParsing config files..."
db[0]=$(grep "\$db " $1 | sed "s/.*\$db \= '\(.*\)'.*/\1/")
db[1]=$(grep "\$user " $1 | sed "s/.*\$user \= '\(.*\)'.*/\1/")
db[2]=$(grep "\$password " $1 | sed "s/.*\$password \= '\(.*\)'.*/\1/")
printf "done\n"
}
# Exit with usage info if no arguments supplied
printf "Checking arguments..."
if [ $# -eq 0 ]; then
printf "failed\n\nUsage: ./pushtotesting.sh <source> <destination>\n"
exit 1
fi
printf "done\n"
# Exit if source or destination aren't directories or don't exist
printf "Checking source directory..."
if [ ! -d $1 ]; then
printf "failed\n\nSource isn't a directory or doesn't exist!\n"
exit 1
fi
printf "done\nChecking destination directory..."
if [ ! -d $2 ]; then
printf "failed\n\nDestination isn't a directory or doesn't exist!\n"
exit 1
fi
printf "done\n"
# cd to the destination
cd $2
# Exit if no git repo in destination
printf "Checking for git repo..."
if [ ! -d .git ]; then
printf "failed\n\nDestination is not a git repository!\n"
exit 1
fi
printf "done\n"
src=${1%/}
dst=${2%/}
jconfig $src/httpdocs/configuration.php
sdb=("${db[@]}")
jconfig $dst/httpdocs/configuration.php
ddb=("${db[@]}")
# Checkout master branch
printf "Checking out master branch..."
git fetch --all
git reset --hard origin/master
git checkout develop
git pull
git checkout master
printf "done\n"
# Sync files
printf "Syncing files on staging with testing...\n"
/usr/bin/rsync -avPzO --stats --delete --exclude ".*" --exclude "/git" --exclude "/keys" --exclude "/logs" --exclude "/scripts" --exclude "/vmfiles" --exclude "/httpdocs/configuration.php" --exclude "/httpdocs/administrator/cache" --exclude "/httpdocs/cache" --exclude "/httpdocs/logs" --exclude "/httpdocs/tmp" $src/ $dst/
# Pull database
printf "Copying source database to destination..."
mysqldump --user=${sdb[1]} --password=${sdb[2]} ${sdb[0]} | mysql --user=${ddb[1]} --password=${ddb[2]} ${ddb[0]}
if [ $? -eq 1 ]; then
printf "failed\n\nFailed to copy database!\n"
exit 1
fi
printf "done\n"
# Set up the site for testing
if [ -f $dst/scripts/test.sql ]; then
printf "Applying settings..."
mysql --user=${ddb[1]} --password=${ddb[2]} ${ddb[0]} < $dst/scripts/test.sql
if [ $? -eq 1 ]; then
printf "failed\n"
err="${err}Failed to apply settings\n"
else
printf "done\n"
fi
fi
# Checkout latest develop branch
printf "Merging develop branch into master...\n"
git merge --no-edit develop
if [ $? -eq 1 ]; then
err="${err}Failed to merge develop branch into master\n"
fi
if [ -f $dst/database/develop.sql ]; then
printf "Importing develop.sql..."
mysql --user=${ddb[1]} --password=${ddb[2]} ${ddb[0]} < $dst/database/develop.sql
if [ $? -eq 1 ]; then
printf "failed\n"
err="${err}Failed to import develop.sql\n"
else
printf "done\n"
fi
fi
if [ -z $err ]; then
printf "Completed successfully!\n"
else
printf "Completed with errors:\n$err"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment