Last active
June 1, 2025 17:08
-
-
Save kagg-design/57733530a845e031ba6cb46337471a5b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# sync.params file example. | |
# REMOTE_DOMAIN="123.123.123.123" | |
# KEY_FILE="~/.ssh/site.rsa" | |
# REMOTE_PATH="/var/www/site/" | |
# LOCAL_PATH="/mnt/c/laragon/www/site" | |
# REMOTE_URL="https://www.site.org" | |
# LOCAL_URL="https://site.test" | |
# SYNC_PLUGINS="elementor-pro wordpress-seo-premium" | |
# AFTER_SYNC_COMMANDS="wp plugin deactivate backwpup wordfence wp-super-cache --skip-plugins" | |
WP_PATH=$(wp config path) | |
if [[ ! $WP_PATH ]]; then | |
echo "This is not a WordPress install" | |
exit 1 | |
fi | |
WP_PATH="$(dirname "$WP_PATH")" | |
PARAMETERS_FILE=""$WP_PATH/sync.params"" | |
if [[ ! -f "$PARAMETERS_FILE" ]]; then | |
echo "Parameters file $PARAMETERS_FILE does not exist" | |
exit 1 | |
fi | |
TYPE="$1" | |
if [[ -z $TYPE ]]; then | |
TYPE="db" | |
fi | |
if [[ "db" != "$TYPE" && "plugins" != "$TYPE" && "uploads" != "$TYPE" && "all" != "$TYPE" ]]; then | |
echo "Argument can be 'db', 'plugins', 'uploads' or 'all' only" | |
exit 1 | |
fi | |
# shellcheck disable=SC1090 | |
if ! source "$PARAMETERS_FILE"; | |
then | |
exit 1 | |
fi | |
if [[ "" == "$REMOTE_USER" ]]; then | |
REMOTE_USER=$USER | |
fi | |
SECONDS=0 | |
if [[ "db" == "$TYPE" || "all" == "$TYPE" ]]; then | |
echo -e "\nDownloading database..." | |
ssh -l "$REMOTE_USER" -i "$KEY_FILE" "$REMOTE_DOMAIN" "cd $REMOTE_PATH > /dev/null&&/usr/local/bin/wp db export /tmp/wp.sql > /dev/null" | |
scp -i "$KEY_FILE" "$REMOTE_USER"@"$REMOTE_DOMAIN":/tmp/wp.sql wp.sql | |
ssh -l "$REMOTE_USER" -i "$KEY_FILE" "$REMOTE_DOMAIN" "rm /tmp/wp.sql" | |
echo -e "Done." | |
echo -e "\nImporting database..." | |
wp --quiet db import wp.sql | |
echo -e "Done." | |
rm wp.sql | |
echo -e "\nReplacing domain in database..." | |
wp search-replace --url="$REMOTE_URL" "$REMOTE_URL" "$LOCAL_URL" --recurse-objects --report-changed-only --precise --skip-columns=guid --skip-tables=wp_users --skip-plugins --skip-themes --allow-root | |
wp cache flush | |
echo -e "Done database sync." | |
fi | |
echo -e "\nDoing after db sync commands..." | |
eval $AFTER_SYNC_COMMANDS | |
echo -e "Done after db sync commands." | |
if [[ "plugins" == "$TYPE" || "all" == "$TYPE" ]]; then | |
echo -e "\nSyncing plugins..." | |
IFS=' ' | |
read -ra PLUGINS <<< "$SYNC_PLUGINS" | |
for PLUGIN in "${PLUGINS[@]}"; do | |
rsync -ase "ssh -l $REMOTE_USER -i $KEY_FILE" "$REMOTE_USER"@"$REMOTE_DOMAIN":"$REMOTE_PATH"/wp-content/plugins/"$PLUGIN"/* "$LOCAL_PATH"/wp-content/plugins/"$PLUGIN" | |
done | |
echo -e "Done." | |
fi | |
if [[ "uploads" == "$TYPE" || "all" == "$TYPE" ]]; then | |
echo -e "\nSyncing uploads..." | |
rsync -ase "ssh -l $REMOTE_USER -i $KEY_FILE" --exclude "*backwpup*/*" "$REMOTE_USER"@"$REMOTE_DOMAIN":"$REMOTE_PATH"/wp-content/uploads/* "$LOCAL_PATH"/wp-content/uploads | |
echo -e "Done." | |
fi | |
echo -e "\nFinished. Time elapsed: $SECONDS seconds." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment