Skip to content

Instantly share code, notes, and snippets.

@hdevilbiss
Last active July 17, 2021 19:54
Show Gist options
  • Save hdevilbiss/9d66ecdd443a9a5cee2c060141c0493d to your computer and use it in GitHub Desktop.
Save hdevilbiss/9d66ecdd443a9a5cee2c060141c0493d to your computer and use it in GitHub Desktop.
Linux shell script to sync WordPress assets down from production in 2 clicks.
#!/bin/bash
# A Linux shell script to remotely sync WordPress assets down from production in only 2 clicks.
# Useful when syncing sites that use third-party themes or plugins which are not available from public repositories.
# To run this script, invoke its filename using the `bash` command. `bash rsync-wp.sh`
# DEFAULT SERVER VARS
# user=""
# port=22
# ip=12.34.567.890
# serverPath="/www/"$user"/public"
# themePath=$serverPath"/wp-content/themes/theme-name"
# childThemePath=$serverPath"/wp-content/themes/child-theme-name"
# uploadsPath=$serverPath"/wp-content/uploads/"
# pluginPath=$serverPath"/wp-content/plugins"
# pluginsPath="$pluginPath/plugin-name,$pluginPath/plugin-name-2
# DEFAULT LOCAL VARS
# localPath="/mnt/c/Users/Name/path/to/web"
# localThemePath=$localPath"/app/themes/theme-name"
# localChildThemePath=$localPath"/app/themes/child-theme-name"
# localUploadsPath=$localPath"/app/uploads"
# localPluginsPath=$localPath"/app/plugins"
# Functions
sync () {
if [ "$1" = "Child Theme" ]
then
src="$childThemePath"
dest="$localChildThemePath"
elif [ "$1" = "Theme" ]
then
src="$themePath"
dest="$localThemePath"
elif [ "$1" = "Uploads" ]
then
src="$uploadsPath"
dest="$localUploadsPath"
elif [ "$1" = "Plugins" ]
then
src={"$pluginsPath"}
dest="$localPluginsPath"
fi
PS3='Do you want this be a dry-run or live run? '
dryRunOptions=("Dry Run" "Live" "Cancel")
select dryRunOption in "${dryRunOptions[@]}"; do
case $dryRunOption in
"Dry Run")
rsync -avP --info=progress2 --dry-run -e 'ssh -p '$port $user@$ip:$src $dest
exit
;;
"Live")
rsync -avP --info=progress2 -e 'ssh -p '$port $user@$ip:$src $dest
exit
;;
"Cancel")
printf "Closing the script"
exit
;;
*) printf "Invalid option $REPLY";;
esac
done;
}
# Ask themes or uploads
PS3='Pick what you want to sync, or cancel: '
options=("Child Theme" "Theme" "Uploads" "Plugins" "Cancel")
select option in "${options[@]}"; do
case $option in
"Child Theme" | "Theme" | "Uploads" | "Plugins")
printf "You want to sync the $option. \n"
sync "$option"
;;
"Cancel")
printf "Closing the script. \n"
exit
;;
*) printf "Invalid option $REPLY. \n";;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment