Skip to content

Instantly share code, notes, and snippets.

@matthewmorek
Created October 24, 2015 12:55
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 matthewmorek/3142dff871d2932b564a to your computer and use it in GitHub Desktop.
Save matthewmorek/3142dff871d2932b564a to your computer and use it in GitHub Desktop.
CS-Cart 4.3.x Sync Script
#!/bin/sh
# ---
# This script assumes you're using SSH key-based authentication to your remote
# It should be run from the root of the local CS-Cart installation, i.e.: `$ ./bin/sync.sh`
# ---
red='\033[0;31m'
green='\033[0;32m'
normal='\033[0m'
TMP_FILE="`mktemp`"
# Root of your remote CS-Cart installation
PROD_ROOT=[remote_path]
# Root of your local CS-Cart installation (you should run this script from it)
DEV_ROOT=$(pwd)
# Valid SSH username for your remote
SSH_USR=jdoe
PROD_HOST=dev.printdesigns.com
STAGE_HOST=local.printdesigns.com
# Define local misc dirs
DB_BACKUP=$DEV_ROOT/data
IMG_DIR=$DEV_ROOT/images
# Read database credentials from a local file
source db_remote.txt
echo "${green}------ CS-CART 4.3.x Sync Script ------"
echo "${normal}[>] Connecting to $PROD_HOST via SSH and dumping $CSCART_DB to temp file..."
ssh $SSH_USR@$PROD_HOST "mysqldump -u $CSCART_USER -p$CSCART_PASSWD $CSCART_DB" > $TMP_FILE 2>/dev/null
echo "${normal}[>] Updating references to '$STAGE_HOST'..."
perl -pi -e "s/$PROD_HOST/$STAGE_HOST/g" $TMP_FILE
echo "${normal}[>] Checking for a local $CSCART_DB..."
mysql -u root $CSCART_DB -e exit 2>&1 >/dev/null
if [ $? -gt 0 ]; then
echo "${normal}[>] Database '$CSCART_DB' was not found. Creating a new one..."
mysqladmin -u root create $CSCART_DB
mysql -u root -e "CREATE USER '$CSCART_USER'@'localhost' IDENTIFIED BY '$CSCART_PASSWD'"
mysql -u root -e "GRANT alter,create,create temporary tables,delete,drop,index,insert,lock tables,select,update ON $CSCART_DB.* to '$CSCART_USER'@'localhost'"
mysqladmin -u root flush-privileges
else
echo "${normal}[>] Database '$CSCART_DB' was found. Creating a local snapshot..."
if [ ! -d "$DB_BACKUP" ]; then
mkdir $DB_BACKUP
fi
mysqldump -u root $CSCART_DB > $DB_BACKUP/$(date +%Y-%m-%d.%H.%M.%S.sql)
echo "${green}[Done] Local snapshot was saved to: '$DB_BACKUP/$(date +%Y-%m-%d.%H.%M.%S.sql)'"
fi
echo "${normal}[>] Syncing local database with production..."
mysql -u root $CSCART_DB < $TMP_FILE
rm $TMP_FILE
echo "${normal}[>] Syncing images with production..."
if [ ! -d "$IMG_DIR" ]; then
mkdir -v -m 777 $IMG_DIR
chown -R mmorek:_www $IMG_DIR
fi
cd $IMG_DIR
rsync -prqe ssh $SSH_USR@$PROD_HOST:$PROD_ROOT/images/. $IMG_DIR 2>&1 >/dev/null
echo "${green}------ Synchronisation Complete. ------${normal}\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment