Skip to content

Instantly share code, notes, and snippets.

@herooutoftime
Created March 31, 2014 14:23
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 herooutoftime/9893457 to your computer and use it in GitHub Desktop.
Save herooutoftime/9893457 to your computer and use it in GitHub Desktop.
MODx sync script
#!/bin/bash
declare -A db_tables_exclude_content
enabled=true
mail=true
rsync=true
dbsync=true
modxsetup=true
ssh_user=[[SSH_USER]]
srv=[[SYNC_ORIGIN_DOMAIN_OR_IP]]
exclude=[[EXCLUDE_TXT]]
src_path=[[SYNC_ORIGIN_PATH]]
src_db_host=[[SYNC_ORIGIN_HOST]]
src_db_user=[[SYNC_ORIGIN_DB_USER]]
src_db_pass=[[SYNC_ORIGIN_DB_PW]]
src_db_db=[[SYNC_ORIGIN_DB_DB]]
dest_path=[[SYNC_DEST_PATH]]
dest_db_host=[[SYNC_DEST_DB]]
dest_db_user=[[SYNC_DEST_DB_USER]]
dest_db_pass=[[SYNC_DEST_DB_PW]]
dest_db_db=[[SYNC_DEST_DB_DB]]
sql_dir=[[SYNC_SQL_STORE_SQL_FILE]]
sql_file=[[SYNC_SQL_STORE_SQL_FILENAME]]
db_tables_exclude_content[0]="[[EXCLUDE_TBL_1]]"
db_tables_exclude_content[1]="[[EXCLUDE_TBL_2]]"
modx_setup=setup/index.php
#!/bin/bash
#CONFIGURATION
declare -A config
declare -A status
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source $DIR/inc
source $DIR/conf/$1
config[enabled]=$enabled
config[mail]=$mail
config[rsync]=$rsync
config[dbsync]=$dbsync
config[modxsetup]=$modxsetup
if [ -z "$srv" ] ; then
echo "You need to set a server in the config file: $1"
exit
fi
#MAP THE TABLES TO EXCLUDE CONTENT
# mapfile -t db_tables_exclude_content < conf/$1; echo "${db_tables_exclude_content[@]}"
sql_file=$sql_dir$src_db_db"/"$sql_file
modx_setup=$dest_path$modx_setup
#CHECK IF THIS SHOULD BE DONE
if ! ${config[enabled]} ; then
echo 'Please enabled me!'
exit
fi
if ${config[enabled]} ; then
echo "Let's do this!"
fi
#CREATE SQL-FOLDER IF NOT EXISTS
if [ ! -d "$sql_dir$src_db_db" ]; then
mkdir $sql_dir$src_db_db
fi
#SYNC PRODUCTION FILES WITH DEV SRV
if ${config[rsync]} ; then
echo "SYNC THE FILESYSTEM"
rsync -az -e ssh --exclude-from="$DIR/$exclude" $ssh_user@$srv:$src_path $dest_path
status[rsync]="File-System was synced"
fi
# if ! [ $? = 24 -o $? = 0 ] ; then
# echo "Fatal: rsync finished $SERVER with errors!"
# logger "Fatal: rsync finished $SERVER with errors!"
# fi
if ${config[dbsync]} ; then
#GENERATE TABLE EXCUSION VARS
tbl_regex="^"$( IFS=$'|'; echo "${db_tables_exclude_content[*]}" )"$"
tbl_string=$( IFS=$' '; echo "${db_tables_exclude_content[*]}" )
#DUMP PRODUCTION DATABASE
#DUMP ALL EXCEPT CERTAIN TABLES
echo "DUMP PRODUCTION DATABASE"
echo "DUMP ALL EXCEPT CERTAIN TABLES: $tbl_string"
tables=$(ssh -C $ssh_user@$srv mysql -h $src_db_host -u $src_db_user -p$src_db_pass -N <<< "show tables from $src_db_db" | grep -Ev $tbl_regex | xargs);
ssh -C $ssh_user@$srv mysqldump -h $src_db_host -u $src_db_user -p$src_db_pass $src_db_db $tables > $sql_file
#DUMP STRUCTURE FOR TABLES WHICH CONTENT WAS EXCLUDED - '-d'
echo "DUMP STRUCTURE FOR TABLES WHICH CONTENT WAS EXCLUDED: $tbl_string"
ssh -C $ssh_user@$srv mysqldump -d -h $src_db_host -u $src_db_user -p$src_db_pass $src_db_db $tbl_string >> $sql_file
#PUSH PRODUCTION DATABSSE INTO TESTING DATABASE
echo "PUSH PRODUCTION DATABASE INTO TESTING DATABASE"
mysql -h $dest_db_host -u $dest_db_user -p$dest_db_pass $dest_db_db < $sql_file
status[dbsync]="Database was synced!"
fi
if ${config[modxsetup]} ; then
# CLEAR CACHE FOLDER
echo "CLEAR CACHE: "$dest_path"core/cache/"
sudo rm -rf $dest_path"core/cache/*"
sudo chmod -R 0775 $dest_path"core/cache/"
#SETUP MODX ON TESTING
echo "SETUP MODX ON TESTING"
out=$(php $modx_setup --installmode=upgrade)
status[modxsetup]="MODx was reinstalled"
fi
if ! ${config[mail]} ; then
exit
fi
status[run]="Sync-Process successful"
status[message]=$( IFS=$'; '; echo "${status[*]}" )
#MAIL US
SUBJECT="Synchronisation-Process"
EMAIL="anti@herooutoftime.com"
# Email text/message
EMAILMESSAGE="/tmp/emailmessage.txt"
echo $(date) > $EMAILMESSAGE
echo $out >> $EMAILMESSAGE
echo -e ${status[message]} >> $EMAILMESSAGE
echo "PLEASE CHECK ASIDES SETTINGS" >> $EMAILMESSAGE
echo "PLEASE CHECK CAMPAIGNER PATHS" >> $EMAILMESSAGE
mail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE
echo "MAIL WAS SENT TO $EMAIL"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment