Skip to content

Instantly share code, notes, and snippets.

@eksiscloud
Created August 1, 2019 23:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eksiscloud/4d04d7262967de857f37551e0d85a025 to your computer and use it in GitHub Desktop.
Save eksiscloud/4d04d7262967de857f37551e0d85a025 to your computer and use it in GitHub Desktop.
Syncing two wordpress databases using WP-CLI
#!/bin/sh
DEVDIR="web/app/uploads/"
DEVSITE="https://example.dev"
PRODDIR="web@example.com:/srv/www/example.com/shared/uploads/"
PRODSITE="https://example.com"
STAGDIR="web@staging.example.com:/srv/www/example.com/shared/uploads/"
STAGSITE="https://staging.example.com"
FROM=$1
TO=$2
case "$1-$2" in
development-production) DIR="up"; FROMSITE=$DEVSITE; FROMDIR=$DEVDIR; TOSITE=$PRODSITE; TODIR=$PRODDIR; ;;
development-staging) DIR="up" FROMSITE=$DEVSITE; FROMDIR=$DEVDIR; TOSITE=$STAGSITE; TODIR=$STAGDIR; ;;
production-development) DIR="down" FROMSITE=$PRODSITE; FROMDIR=$PRODDIR; TOSITE=$DEVSITE; TODIR=$DEVDIR; ;;
staging-development) DIR="down" FROMSITE=$STAGSITE; FROMDIR=$STAGDIR; TOSITE=$DEVSITE; TODIR=$DEVDIR; ;;
*) echo "usage: $0 development production | development staging | production development | production staging" && exit 1 ;;
esac
read -r -p "Would you really like to reset the $TO database and sync $DIR from $FROM? [y/N] " response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then
cd ../ &&
wp "@$TO" db export &&
wp "@$FROM" db export - | wp "@$TO" db import - &&
wp "@$TO" search-replace "$FROMSITE" "$TOSITE" &&
rsync -az --progress "$FROMDIR" "$TODIR"
fi
Usage:
## USAGE
# ./sync.sh
# usage: ./sync.sh development production | development staging | production development | production staging
## EXAMPLES
# ./sync.sh development production
# ./sync.sh production development
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment