Skip to content

Instantly share code, notes, and snippets.

@matrunchyk
Last active October 14, 2015 21:38
Show Gist options
  • Save matrunchyk/05477e28f1d84e99bd55 to your computer and use it in GitHub Desktop.
Save matrunchyk/05477e28f1d84e99bd55 to your computer and use it in GitHub Desktop.
Use: update.sh /absolute/path/of/your/OpenCart/installation. Read this post for details: http://easywebthings.blogspot.com/2015/10/opencart-upgrade-script.html
#!/bin/bash
RED='\033[0;31m'
YELLOW='\033[0;33m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
dest=$1
keep_local=(
".git"
".gitignore"
".idea"
".htaccess"
"config.php"
"admin/config.php"
"catalog/view/theme/my_custom_theme"
"image/cache"
"image/templates/my_custom_logo.png"
"system/storage"
"image/catalog"
)
ignore_new=(
".htaccess.txt"
"config-dist.php"
"admin/config-dist.php"
)
if [ -z "${dest}" ]; then
echo -e "${RED}Please specify a destination directory.${NC}"
exit 1
fi
last=${dest:${#dest}-1:1}
if [ "${last}" != "/" ]; then
dest="${dest}/"
fi
if [ ! -d "${dest}" ]; then
echo -e "${RED}Destination directory doesn't exists. Please specify a current OC installation.${NC}"
exit 1
fi
if [ ! -f "${dest}/index.php" ]; then
echo -e "${RED}${dest}/index.php has not been found.${NC}"
echo -e "${RED}Please check if destination directory is a valid OC installation.${NC}"
exit 2
fi
cur_ver=`grep -E "define\('VERSION', '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+(_rc)?'\);" "${dest}/index.php" | sed -E 's/.*([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+(_rc)?).*/\1/g'`
echo -e "Current version: ${cur_ver}"
if [ -f source/upload/index.php ]; then
read -e -p "Found a local distribution. Update? (y/N)" YN
if [ "${YN}" != "y" ] && [ "${YN}" != "Y" ]; then
rm -rf ./source
echo -e "Checking new release..."
git clone https://github.com/opencart/opencart.git source > /dev/null
fi
fi
if [ ! -f source/upload/index.php ]; then
echo -e "${RED}Cannot download a new OC release. Update failed.${NC}"
exit 1
fi
cd source/upload
new_ver=`grep -E "define\('VERSION', '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+(_rc)?'\);" "index.php" | sed -E 's/.*([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+(_rc)?).*/\1/g'`
if [ "${cur_ver}" == "${new_ver}" ]; then
echo -e "${YELLOW}No new releases found.${NC}"
read -e -p "Force upgrade? (y/N)" YN
if [ "${YN}" != "y" ] && [ "${YN}" != "Y" ]; then
echo -e "Cancelled by user."
exit 4
fi
fi
echo -e "${GREEN}New release version: ${new_ver}${NC}"
read -e -p "Procced to upgrade? (y/N)" YN
if [ "${YN}" != "y" ] && [ "${YN}" != "Y" ]; then
echo -e "Cancelled by user."
exit 4
fi
echo -e "Starting upgrade..."
delete_filter="diff -rq ./ ${dest} | grep \"Only in ${dest}\" | sed \"s/Only in //g\" | sed 's/: /\//g' | sed 's/\/\//\//g' | grep -Ev \""
for keep in ${keep_local[@]}
do
delete_filter="${delete_filter}^${dest}$keep|"
done
delete_filter="${delete_filter}^${dest}stub\""
count=`eval ${delete_filter} | wc -l`
if [ "${count}" -gt "0" ]; then
echo -e "${RED}Files to be removed:${NC}"
eval ${delete_filter}
YN=''
read -e -p "Proceed to remove? (y/N)" YN
if [ "${YN}" != "y" ] && [ "${YN}" != "Y" ]; then
echo -e "Cancelled by user."
exit 4
fi
delete_filter="${delete_filter} | xargs rm -rf"
eval ${delete_filter}
echo -e "${count} files were removed."
fi
echo -e "Copying new files..."
while true; do
new_filter="diff -rq ./ ${dest} | grep \"Only in ./\" | sed \"s/Only in //g\" | sed 's/: /\//g' | sed 's/\/\//\//g' | grep -Ev \""
for ignore in ${ignore_new[@]}; do
new_filter="${new_filter}^./$ignore|"
done
new_filter="${new_filter}^./stub\""
count=`eval ${new_filter} | wc -l`
if [ "${count}" -gt "0" ]; then
items=`eval ${new_filter}`
for item in ${items}; do
if [ -d "${item}" ]; then
mkdir -p "${dest}${item}"
else
cp "${item}" "${dest}${item}"
fi
done
echo -e "${count} new files were copied."
else
break
fi
done
echo -e "Upgrading old files..."
upgrade_cmd="diff -rq ./ $dest | grep \" differ\" | sed \"s/ differ$//g\" | sed 's/: /\//g' | sed \"s/^Files //g\" | sed -E 's/^(.*) and (.*)$/cp \"\1\" \"\2\"; /g'"
count=`eval $upgrade_cmd | wc -l`
items=`eval ${upgrade_cmd}`
eval ${items}
echo -e "${count} files were upgraded."
echo -e "${GREEN}Upgrade finished!${NC}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment