A bash script to automatic update a development environment from a production Magento 2 site

The update program consist of 2 Bash scripts: main.sh and remote.sh.
They need reside in the same folder.
The program is started by executing main.sh under the Cygwin.

See also the reverse solution: Automatic update the production Magento 2 site from the development environment.

main.sh:

#!/bin/bash
# 2015-07-16
subfolder='store'
localDomainEscaped='localhost\.com'
remoteDomainEscaped='mage2\.pro'
localPort='900'
remoteProtocol='https'
localMagentoPathBase='/cygdrive/c/work/mage2.pro/'
localMagentoPath=$localMagentoPathBase$subfolder
remoteMagentoPathBase='/var/www/mage2.pro/'
archiveNameForFiles='code.tar.gz'
sqlDump='db.sql'
archiveNameForDB=$sqlDump'.gz'
dbName='mage2_pro_store'
remoteAccount='www-data@5.9.188.84'
scp="scp $remoteAccount:$remoteMagentoPathBase"
scriptPath=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )'/'
skip1=0
skip2=0
skip3=0
skip4=0
skip5=0
printf 'BEGIN REMOTE PART...\n'
(\
	echo 'subfolder='$subfolder ; \
	echo 'magentoPathBase='$remoteMagentoPathBase ; \
	echo 'archiveNameForFiles='$archiveNameForFiles ; \
	echo 'sqlDump='$sqlDump ; \
	echo 'archiveNameForDB='$archiveNameForDB ; \
	echo 'dbName='$dbName ; \
	echo 'protocol='$remoteProtocol ; \
	cat $scriptPath'remote.sh' \
) | ssh $remoteAccount 'bash -s' | sed 's/^/    /'
printf '\nEND REMOTE PART'
cd $localMagentoPathBase
printf '\ndownloading and unpacking the DB... '
if [ $skip1 -eq 1 ]; then
	printf 'skipped'
else
	printf '\n'
	$scp$archiveNameForDB ./
	ssh $remoteAccount rm -f $remoteMagentoPathBase$archiveNameForDB
	rm -f $sqlDump
	gunzip $archiveNameForDB
	printf 'done'
fi
printf '\npreprocessing the DB... '
if [ $skip2 -eq 1 ]; then
	printf 'skipped'
else
	printf '\n'
	urlReplacement='s/'$remoteProtocol'\:\/\/'$remoteDomainEscaped'/http\:\/\/'$localDomainEscaped'\:'$localPort'/g'
	sed -i -- $urlReplacement $sqlDump
	printf 'done'
fi
printf '\nupdating the local DB... '
if [ $skip3 -eq 1 ]; then
	printf 'skipped'
else
	printf '\n'
	mysql -e "DROP DATABASE ${dbName}; CREATE DATABASE ${dbName};"
	mysql $dbName < $sqlDump
	rm -f $sqlDump
	mysql -e "\
		UPDATE core_config_data \
		SET value = '0' \
		WHERE path IN ('web/secure/use_in_frontend', 'web/secure/use_in_adminhtml'); \
	" $dbName
	printf 'done'
fi
printf '\ndownloading the remote files... '
if [ $skip4 -eq 1 ]; then
	printf 'skipped'
else
	printf '\n'
	$scp$archiveNameForFiles ./
	ssh $remoteAccount rm -f $remoteMagentoPathBase$archiveNameForFiles
	printf 'done'
fi
printf '\nupdating the code... '
if [ $skip5 -eq 1 ]; then
	printf 'skipped'
else
	printf '\n'
	cd $localMagentoPath
	rm -rf ../env.php
	mv app/etc/env.php ..
	rm -rf ../.git
	mv ./.git ..
	# https://coderwall.com/p/kksf5q/delete-all-files-including-hidden-ones-with-just-one-rm
	rm -rf {,.[!.],..?}*
	tar -xvzf ../$archiveNameForFiles >/dev/null
	mv ../env.php app/etc
	mv ../.git .
	chmod -v -R 777 pub/static >/dev/null
	chmod -v -R 777 pub/media >/dev/null
	Icacls . /t /grant "everyone"\:\(OI\)\(CI\)F >/dev/null
	rm -f ../$archiveNameForFiles
	printf 'done'
fi

remote.sh

#!/bin/bash
skip1=0
skip2=0
skip3=0
printf 'base: '$magentoPathBase
printf '\nsubfolder: '$subfolder
magentoPath=$magentoPathBase$subfolder
cd $magentoPath
printf '\npacking the remote files... '
if [ $skip1 -eq 1 ]; then
	printf 'skipped'
else
	printf '\n'
	# 2015-07-16
	# ./pub/static/[^.] pattern preserves .htaccess.
	#
	# 2015-07-17
	# Please note that:
	# 1) to exclude root subfolder only an expression must be started with «./»:
	# http://stackoverflow.com/questions/984204
	#
	# 2) an exclude expression will not work with trailing slash «/»:
	# http://stackoverflow.com/questions/984204#comment13410086_984204
	tar \
		--exclude='./pub/static/[^.]*' \
		--exclude='./var/[^.]*' \
		-zcvf ../$archiveNameForFiles . >/dev/null
	printf 'done'
fi
printf '\ncleaning the remote DB... '
if [ $skip2 -eq 1 ]; then
	printf 'skipped'
else
	printf '\n'
	mysql -e "\
		SET FOREIGN_KEY_CHECKS=0; \
		TRUNCATE log_url; \
		TRUNCATE log_url_info; \
		TRUNCATE log_visitor; \
		TRUNCATE log_visitor_info; \
		TRUNCATE report_event; \
		TRUNCATE report_viewed_product_index; \
		TRUNCATE report_compared_product_index; \
		TRUNCATE report_viewed_product_aggregated_daily; \
		SET FOREIGN_KEY_CHECKS=1; \
	" $dbName
	printf 'done'
fi
cd $magentoPathBase
printf '\ndumping and packing the remote DB... '
if [ $skip3 -eq 1 ]; then
	printf 'skipped'
else
	printf '\n'
	mysqldump $dbName | gzip > $archiveNameForDB
	printf 'done'
fi

See also: