Skip to content

Instantly share code, notes, and snippets.

@mkeneqa
Forked from lopadz/SYNC_GIST_README.md
Created March 22, 2021 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkeneqa/1f48a3b55d16c57fba348029b3f2e1fd to your computer and use it in GitHub Desktop.
Save mkeneqa/1f48a3b55d16c57fba348029b3f2e1fd to your computer and use it in GitHub Desktop.
Sync WP Code & DB Makefile Recipe
# Mark the files/dir to exclude from syncing to remote server
*.DS_Store
*.bak
*.log
.vscode/
# Specify the shell
SHELL := bash
# Site URL
OLD_URL = https://domain.test
NEW_URL = https://www.domain.com
# Commands to execute
execute:
@echo "Updating URL in Database ..."
@wp search-replace "${OLD_URL}" "${NEW_URL}" --all-tables
@wp elementor replace_urls "${OLD_URL}" "${NEW_URL}"
@wp elementor flush_css
@wp cache flush
@echo "Done!"
# Specify the shell
SHELL := bash
# Get name of this Makefile
MAKE_FILENAME := $(lastword $(MAKEFILE_LIST))
# Include utilities like colors
include _UTILITIES.mk
# ==============
# EDIT THIS
# ==============
# Environment
ENVIRONMENT = STAGING
# Where the app is located.
# Make sure this Makefile is at the project's root level or update your paths accordingly
LOCAL_APP_PATH = ${CURRENT_PATH}
# Sync codebase dir (theme, plugins, etc...)
# Leave empty if syncing all of it for the first time (ex. Bedrock install)
SYNC_CODE_PATH = public_html/app/
# Sync database dir
SYNC_DB_PATH = _db/
# SSH Port (if other than 22)
REMOTE_SSH_PORT =
# Name of Server User Account connecting to
REMOTE_USER =
# Hostname or IP address of server connecting to
REMOTE_HOSTNAME =
# Server path
REMOTE_APP_PATH = /home/${REMOTE_USER}/staging/
# List of files/folders to be excluded
EXCLUDE_LIST = ${CURRENT_PATH}/_exclude.txt
# Syncing from ...
CODEBASE_FROM = ${LOCAL_APP_PATH}/${SYNC_CODE_PATH}
DATABASE_FROM = ${LOCAL_APP_PATH}/${SYNC_DB_PATH}
# Syncing to ...
CODEBASE_TO = ${REMOTE_USER}@${REMOTE_HOSTNAME}:${REMOTE_APP_PATH}/${SYNC_CODE_PATH}
DATABASE_TO = ${REMOTE_USER}@${REMOTE_HOSTNAME}:${REMOTE_APP_PATH}/${SYNC_DB_PATH}
# DB options
DB_NAME = $(shell wp config get DB_NAME)
DB_EXPORT = ${LOCAL_APP_PATH}/${SYNC_DB_PATH}dev-${DB_NAME}-${CURRENT_DATE}.sql
#---------------------------------------------
# Save WP Database
#---------------------------------------------
start:
@echo ""
@echo "Syncing to ${RED}${ENVIRONMENT}${RESET_COLOR} server."
@read -p "${YELLOW}Do you wish to continue?${RESET_COLOR} " -n 1 -r; \
echo "";\
if [[ $$REPLY =~ ^[Yy] ]]; \
then \
echo "";\
echo "${GREEN}Starting${RESET_COLOR} ...";\
make -f ${MAKE_FILENAME} execute;\
echo "";\
fi
#---------------------------------------------
# Order of the sync steps
#---------------------------------------------
execute: rsync_code dump_db rsync_db ssh_remote
#---------------------------------------------
# Sync Codebase Directory
#---------------------------------------------
rsync_code:
@echo "${LIGHTPURPLE}${SEPARATOR}${RESET_COLOR}"
@echo " ${LIGHTPURPLE}Syncing files ...${RESET_COLOR}"
@echo "${LIGHTPURPLE}${SEPARATOR}${RESET_COLOR}"
@wp elementor flush_css
@wp cache flush
@rsync -avzhe "ssh -p ${REMOTE_SSH_PORT}" --delete --exclude-from="${EXCLUDE_LIST}" "${CODEBASE_FROM}" "${CODEBASE_TO}"
@echo "${GREEN}Success:${RESET_COLOR} Codebase has been synced."
#---------------------------------------------
# Save WP Database
#---------------------------------------------
dump_db:
@echo "${LIGHTPURPLE}${SEPARATOR}${RESET_COLOR}"
@echo " ${LIGHTPURPLE}Exporting LOCAL DB ...${RESET_COLOR}"
@echo "${LIGHTPURPLE}${SEPARATOR}${RESET_COLOR}"
@wp db repair --quiet && wp db optimize --quiet
@wp db export "${DB_EXPORT}"
#---------------------------------------------
# Sync Database Directory
#---------------------------------------------
rsync_db:
@echo "${LIGHTPURPLE}${SEPARATOR}${RESET_COLOR}"
@echo " ${LIGHTPURPLE}Syncing Database ...${RESET_COLOR}"
@echo "${LIGHTPURPLE}${SEPARATOR}${RESET_COLOR}"
@rsync -avzhe "ssh -p ${REMOTE_SSH_PORT}" --delete --exclude-from="${EXCLUDE_LIST}" "${DATABASE_FROM}" "${DATABASE_TO}"
@echo "${GREEN}Success:${RESET_COLOR} Database has been synced."
#---------------------------------------------
# SSH to Remote host
#---------------------------------------------
ssh_remote:
@echo "${LIGHTPURPLE}${SEPARATOR}${RESET_COLOR}"
@echo " ${LIGHTPURPLE}Connecting to remote server ...${RESET_COLOR}"
@echo "${LIGHTPURPLE}${SEPARATOR}${RESET_COLOR}"
@ssh -t ${REMOTE_USER}@${REMOTE_HOSTNAME} -p ${REMOTE_SSH_PORT} "cd ${REMOTE_APP_PATH}/ ; bash --login"
#---------------------------------------------
# Use this to run tests
#---------------------------------------------
test:
@echo "test"

Sync WP code/database Makefile

Because sometimes you are just trying to sync your codebase and database from a local host to a remote one--no complex deployment strategies here!

In my case, I love developing with Bedrock and Laravel Valet and I haven't found a simple deployment strategy for my current setup. So, this is my simple workflow:

  1. Make sure you have set up remote directories.

LOCAL

  1. Install Bedrock to your dir of choice (in this case to "bedrock-app"):
$ composer create-project roots/bedrock bedrock-app
  1. Configure Bedrock as needed. Pay attention to your local & remote paths.
  2. Create bedrock-app/_db directory.
  3. Copy this gist to your project's root directory for easy access. (ex. bedrock-app/_SYNC.mk)
  4. Configure the _SYNC.mk & _SR_DB.mk files to match your project needs.
  5. When you're ready to sync to remote, cd to your project's root directory and run:
$ make -f _SYNC.mk
  1. Hit 'y' to run the makefile.
  2. If everything worked, it will open a new connection to the remote host and cd you into the project's dir.

REMOTE

  1. Make sure your DB credentials in the remote .env file have been configured.
  2. Reset and/or import the synced db:
    $ wp db reset
    $ wp db import _db/DB_NAME_HERE
  3. Make sure _SR_DB.mk has been configured with old and new URLs.
  4. Run this to search & replace URLs in the remote db:
$ make -f _SR_DB.mk

WARNING: LINES 78 & 98 have a --delete flag, which means it will delete everything on the remote host and mirror the local files. Remove the flag if you don't need it.

Assumptions & other notes

  • Both local and remote hosts have Bash shell and WP-CLI installed.
  • SSH connection to remote host is set up and working.
  • If SSH key has passphrase, it will need to be entered multiple times.
  • The script exports to: _db/dev-DATABASE_NAME-YYYY-MM-DD-HHMMSS.sql. To change this, configure lines 21, 43, 44.
  • If not using Elementor, remove line 76 in _SYNC.mk and lines 13 and 14 in _SR_DB.mk.

If you only need to run a specific step in the _SYNC.mk script, run:

$ make -f _SYNC.mk step_name_here

Available steps are:

  • rsync_code: Syncs the codebase directory.
  • dump_db: Exports the local db.
  • rsync_db: Syncs the database directory.
  • ssh_remote: SSH into the remote host.
# ===================
# COLORS
# ===================
# define standard colors
ifneq (,$(findstring xterm,${TERM}))
RED := $(shell tput -Txterm setaf 1)
GREEN := $(shell tput -Txterm setaf 2)
YELLOW := $(shell tput -Txterm setaf 3)
LIGHTPURPLE := $(shell tput -Txterm setaf 4)
PURPLE := $(shell tput -Txterm setaf 5)
BLUE := $(shell tput -Txterm setaf 6)
RESET_COLOR := $(shell tput -Txterm sgr0)
else
RED := ""
GREEN := ""
YELLOW := ""
LIGHTPURPLE := ""
PURPLE := ""
BLUE := ""
RESET_COLOR := ""
endif
# ===================
# MISC
# ===================
SEPARATOR = ===============================
CHECK = ${GREEN}✔${RESET_COLOR}
CURRENT_DATE = $(shell date +"%Y-%m-%d-%k%M%S")
CURRENT_PATH = $(shell pwd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment