Drupal 8 has integrated other projects and modules into the Drupal Core. For example: Views, Symfony, and JQuery. Asz a result, whenever any of the included projects have an architecture change, or security issue, the Drupal Core must be updated, and there is not an automatic way of doing this. As a result, there have been a half-dozen Core updates in the past month, and it wastes 2–3 hours of my time whenever I must update my 6 sites. And this is if there are no bugs in the provided update, and all goes smoothly.
It occurred to me that I essentially do the same things to each site each time I update them, so why not create a shell script? So I did, and I offer it to others with the following provisos:
- There are no guarantees of correctness or application to your site. Editing the script may be needed. Although my script makes a backup of your files and database, I suggest you make your own until you have confidence in the script.
- I assume that there are links to composer.phar and vendor/drush/drush/drush in the installation directory (where vendor and this shell script are located).
- I inserted pause statements in the script to enable you to make sure things are working correctly as the script is executed. When you are confident of the script, comment them out.
- I have used this script to successfully update major (5.5 -> 5.6) updates, and minor updates (5.6.14 -> 5.6.15).
Save the script in your main Drupal directory. I called it drupal8_update.sh
Once you have confidence in the script, you can remove the diagnostics and pauses.
#!/bin/bash -x
# A script to upgrade a Drupal installation to the next minor version
# This script uses drush to initiate database updates; Assumes there is a drush link to vendor/drush/drush/drush
# and a link to composer.phar in the install directory
#init
function pause(){
read -p "$*"
}TMP="~/tmp"
# check for user input
if [ ! $@ ]; then
echo
echo " Usage:"
echo " $0 MAJOR MINOR SITE [DIRECTORY]"
echo
echo " MAJOR: The new MAJOR version to install - e.g., 8.6"
echo " MINOR: The new MINOR version to install - e.g., 14 (for 8.6.14)"
echo " SITE: a nick for your site (fixes multiple sites)"
echo " DIRECTORY: The relative path to your Drupal installation; if blank the current directory is assumed"
echo
echo " Example usage:"
echo " $0 6. 9 xisam public_html/"
echo
exit 0
fi
MAJOR=$1
MINOR=$2
VERSION="${MAJOR}.${MINOR}"
OLD_VERSION="${MAJOR}.$(( ${MINOR} - 1 ))"
DRUPAL_OLD="drupal-${OLD_VERSION}"
echo "OLD_VERSION = $OLD_VERSION DRUPAL_OLD =$DRUPAL_OLD"
pause 'Press [Enter] key to continue...'
if [ ! $4 ]; then
SITEPATH="."
else
SITEPATH=$4
fiSITE=$3
DRUPAL_SITE="${SITE}-"
DRUPAL_VERSION="drupal-${VERSION}"
DRUPAL_PACKAGE="${DRUPAL_VERSION}.tar.gz"
TMP_FILE="${TMP}/${DRUPAL_PACKAGE}"echo "Drupal_site=${DRUPAL_SITE}"
echo "DRUPAL_PACKAGE=${DRUPAL_PACKAGE}"cd $SITEPATH
# Put the site offline
echo "Putting site in offline mode..."
./drush sset system.maintenance_mode 1pause 'Site is offline. Press [Enter] key to continue...'
# backup core files
now=$(date +"%m_%d_%Y")echo "Backing up current Drupal non-core files... "
tar --exclude=./.htaccess --exclude=./*.php --exclude=./libraries --exclude=./modules --exclude=./profiles --exclude=./themes --exclude=./core --exclude=./vendor --exclude=./*.tgz -zcvf "${DRUPAL_SITE}drupal-${now}.backup.tgz" ./
echo "Done."
# (ls) | xargs echo#backup database; use result-file option to prevent charset corruption
echo -n "Backing up current Drupal database... "
./drush sql-dump --result-file=../drupal-upgrade.backup.sql.tgz
echo "Done."# example download link http://ftp.drupal.org/files/projects/drupal-6.9.tar.gz
DOWNLOAD="http://ftp.drupal.org/files/projects/${DRUPAL_PACKAGE}"
echo "Start downloading $DRUPAL_PACKAGE... "
wget -q $DOWNLOAD
echo "Done."
echo "Untarring new distribution"
tar -zxf $DRUPAL_PACKAGE
pause 'Untarred. Press [Enter] key to continue...'echo "Removing .old directories and old distro files if any"
rm -rf *.old
rm -rf ${DRUPAL_OLD}*
mv core core.old
mv vendor vendor.old
# (ls) | xargs echo
echo "Old versions removed. Core & vendor moved to .old Upgrading Drupal core files... "
echo $(ls)cd $DRUPAL_VERSION
cp -R vendor ../
cp -R core ../
cp -f *.php ../
cd ../
# (ls) | xargs echo
pause 'New directories copied. Press [Enter] key to continue...'./composer update --no-dev --with-dependencies
pause 'Composer did the update. Press [Enter] key to continue...'
# The .htaccess file is purposely not copiedecho "Done."
echo -n "Upgrading Drupal database... "
yes | ./drush updb
./drush cr
echo "Done."# Put the site back online
echo -n "Putting site back online... "./drush sset system.maintenance_mode 0
echo "Done."echo
echo -n "If something went horribly wrong type \"restore\" to restore from backup: "
read RESTOREif [ "$RESTORE" = "restore" ]; then
echo -n "Restoring database... "
MYSQL_CONNECT=$(drush sql-connect)
$MYSQL_CONNECT < ../drupal-upgrade.backup.sql.tgz
echo "Done."echo -n "Restoring files... "
# tar -xzf ../drupal-core.backup.tgz -C ./
rm -rf core
mv core.old core
rm -rf vendor
mv vendor.old vendor
echo "Done."echo "Your upgrade failed, but the backup was restored"
else
echo "removing .old directories"
echo "Your upgrade is complete"
fiBACKUP_DIR=$(readlink -f ../)
echo "Backup files before the upgrade where saved in the following locations:"
echo " ${BACKUP_DIR}/${DRUPAL_SITE}drupal-core.backup.tgz"
echo " ${BACKUP_DIR}/drupal-upgrade.backup.sql.tgz"
Add new comment