The 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 development environment from the production Magento 2 site.
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'
scriptPath=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )'/'
skip1=0
skip2=0
skip3=0
skip4=0
skip5=0
skip6=0
cd $localMagentoPath
printf 'packing the local code... '
if [ $skip1 -eq 1 ]; then
printf 'skipped'
else
printf '\n'
# 2015-07-16
# ./pub/static/[^.] pattern preserves .htaccess.
#
# 2015-07-17
# Excluding root .git subfolder because of its bug size (200 Mb, twice as the remaining files).
#
# 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='./.git' \
--exclude='./pub/static/[^.]*' \
--exclude='./var/[^.]*' \
-zcvf ../$archiveNameForFiles . >/dev/null
printf 'done'
fi
printf '\nuploading the code to the remote server... '
if [ $skip2 -eq 1 ]; then
printf 'skipped'
else
printf '\n'
scp $localMagentoPathBase$archiveNameForFiles www-data@5.9.188.84:$remoteMagentoPathBase
rm -f $localMagentoPathBase$archiveNameForFiles
printf 'done'
fi
cd $localMagentoPathBase
printf '\ndumping the DB... '
if [ $skip3 -eq 1 ]; then
printf 'skipped'
else
printf '\n'
mysqldump $dbName > $sqlDump
printf 'done'
fi
printf '\npreprocessing the DB dump... '
if [ $skip4 -eq 1 ]; then
printf 'skipped'
else
printf '\n'
urlReplacement='s/http\:\/\/'$localDomainEscaped'\:'$localPort'/'$remoteProtocol'\:\/\/'$remoteDomainEscaped'/g'
sed -i -- $urlReplacement $sqlDump
printf 'done'
fi
printf '\packing the DB dump... '
if [ $skip5 -eq 1 ]; then
printf 'skipped'
else
printf '\n'
# http://www.unix.com/53955-post2.html
gzip -f $sqlDump > $archiveNameForDB
printf 'done'
fi
printf '\nuploading the DB to the remote server... '
if [ $skip6 -eq 1 ]; then
printf 'skipped'
else
printf '\n'
scp $localMagentoPathBase$archiveNameForDB www-data@5.9.188.84:$remoteMagentoPathBase
rm -f $localMagentoPathBase$archiveNameForDB
printf 'done'
fi
printf '\nBEGIN REMOTE PART...\n'
(\
echo 'subfolder='$subfolder ; \
echo 'magentoPathBase='$remoteMagentoPathBase ; \
echo 'archiveNameForFiles='$archiveNameForFiles ; \
echo 'archiveNameForDB='$archiveNameForDB ; \
echo 'dbName='$dbName ; \
echo 'protocol='$remoteProtocol ; \
cat $scriptPath'remote.sh' \
) | ssh www-data@5.9.188.84 'bash -s' | sed 's/^/ /'
printf '\nEND REMOTE PART'
remote.sh
#!/bin/bash
skip1=0
skip2=0
printf 'base: '$magentoPathBase
printf '\nsubfolder: '$subfolder
magentoPath=$magentoPathBase$subfolder
cd $magentoPath
printf '\nupdating the code... '
if [ $skip1 -eq 1 ]; then
printf 'skipped'
else
printf '\n'
mv app/etc/env.php ..
# https://coderwall.com/p/kksf5q/delete-all-files-including-hidden-ones-with-just-one-rm
rm -rf {,.[!.],..?}*
tar -xvzf ../$archiveNameForFiles >/dev/null
sudo chmod -v -R 755 . >/dev/null
rm -f ../$archiveNameForFiles
mv ../env.php app/etc
sudo service php5-fpm restart >/dev/null
printf 'done'
fi
printf '\nupdating the DB... '
if [ $skip2 -eq 1 ]; then
printf 'skipped'
else
printf '\n'
cd ..
mysql -e "DROP DATABASE ${dbName}; CREATE DATABASE ${dbName};"
# http://superuser.com/a/402383/57200
zcat $archiveNameForDB | mysql $dbName
rm -f $archiveNameForDB
# http://stackoverflow.com/a/2237103/254475
if [ "$protocol" == 'https' ]; then
mysql -e "\
UPDATE core_config_data \
SET value = '1' \
WHERE path IN ('web/secure/use_in_frontend', 'web/secure/use_in_adminhtml'); \
" $dbName
fi
printf 'done'
fi