Skip to content

Instantly share code, notes, and snippets.

@rickycheers
Last active December 27, 2015 03:29
Show Gist options
  • Save rickycheers/7259300 to your computer and use it in GitHub Desktop.
Save rickycheers/7259300 to your computer and use it in GitHub Desktop.
Bash script to "sync" WordPress sites between environments (servers) (For Digizent use)
#!/bin/bash
####### Update all this variables #######
repo_dir="rr-huntsville-site"
site_dir="wordpress"
branch="master"
# Update with directories relative to wp-content
pull_dirs="themes uploads"
push_dirs="themes uploads plugins"
####### Stop updating variables #######
action=$1
pull(){
cd $repo_dir
echo "Updating repo..."
git pull
echo "Exporting repo..."
git checkout-index -f -a --prefix=$PWD/../exported-repo/
cd ..
for dir in $pull_dirs
do
echo "Copying exported-repo/wp-content/$dir to $site_dir/wp-content/$dir"
cp -r exported-repo/wp-content/$dir $site_dir/wp-content/$dir/../
done
echo "Done, have a nice day ;)"
}
push(){
for dir in $push_dirs
do
echo "Copying $site_dir/wp-content/$dir to $repo_dir/wp-content/$dir"
cp -r $site_dir/wp-content/$dir $repo_dir/wp-content/$dir/../
done
cd $repo_dir
if ! git diff-index --quiet HEAD --; then
echo "Changes detected. Updating repository"
echo "Be careful, I'm going to add everything I find because I'm not smart enough yet..."
git add --all
echo "This is what I'm about to commit"
git status
echo "Do you want to proceed? y/n : "
read proceed
if [[ $proceed = "y" ]]; then
echo "Making a commit"
git commit -a -m "Updating from server"
git push origin $branch
echo "Done, have a nice day :)"
else
echo "Not doing anything."
exit
fi
else
echo "The repository is clean. Nothing to push"
fi
}
if [[ $action = '' ]]; then
echo "What should I do? Pull (./update.sh pull) or Push (./update.sh pull)"
exit
fi
case "$1" in
"pull") pull ;;
"push") push ;;
esac
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment