Skip to content

Instantly share code, notes, and snippets.

@alhoseany
Forked from santerref/sync.sh
Created May 6, 2019 21:54
Show Gist options
  • Save alhoseany/079391c40d900d517c05fcb834f73910 to your computer and use it in GitHub Desktop.
Save alhoseany/079391c40d900d517c05fcb834f73910 to your computer and use it in GitHub Desktop.
Simple bash script to synchronize your WordPress dev (local) with your staging or production.
#/bin/bash
REMOTE_URL="https://staging.example.com"
LOCAL_URL="http://example.dev"
REMOTE_PATH="/home/staging_example/public_html"
LOCAL_PATH="/var/www/example.dev"
SSH_HOST="[email protected]"
GREEN="\033[0;32m"
RESET="\033[0m"
BOLD="$(tput bold)"
echo -e "$GREEN$BOLD(1/6)$RESET Exporting WordPress database."
wp --quiet --path=$LOCAL_PATH db export ~/wordpress.sql
echo -e "$GREEN$BOLD(2/6)$RESET Rsyncing uploads and plugins folders."
rsync -qavz -e "ssh -q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" "$LOCAL_PATH/wp-content/uploads/" $SSH_HOST:$REMOTE_PATH/wp-content/uploads/
rsync -qavz -e "ssh -q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" "$LOCAL_PATH/wp-content/plugins/" $SSH_HOST:$REMOTE_PATH/wp-content/plugins/
echo -e "$GREEN$BOLD(3/6)$RESET Uploading wordpress.sql database export."
scp -q ~/wordpress.sql $SSH_HOST:/tmp
echo -e "$GREEN$BOLD(4/6)$RESET Importing wordpress.sql database into remote host."
wp --quiet --ssh=$SSH_HOST$REMOTE_PATH db import /tmp/wordpress.sql
echo -e "$GREEN$BOLD(5/6)$RESET Running wp search-replace on remote host."
wp --quiet --ssh=$SSH_HOST$REMOTE_PATH search-replace "$LOCAL_URL" "$REMOTE_URL"
echo -e "$GREEN$BOLD(6/6)$RESET Cleaning files."
rm -f ~/wordpress.sql
ssh -q $SSH_HOST /bin/bash << EOF
rm -f /tmp/wordpress.sql
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment