Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Bash script for automatization of Drupal 7 within Apache2 in Ubuntu Server. Tested upon Trusty Tahr and forth.
#!/bin/bash
# Author: Juane Chomon
# Author URL: https://www.juane.cl
# Created: September 01, 2018
# Version: 1.0
# License: Free to use it whatever you like. But don't be that ass to say you made it if you don't add it your own value to it
# Usage: sh update_drupal7_core.sh {version_number} or ./update_drupal7_core {version_number}
# eg. sh update_drupal7_core.sh 7.59 or ./update_drupal7_core 7.59
TIMESTAMP=`date '+%Y%m%d%H%M%S'` # Datetime string to log and backup as a distinctive attempt
USER="_www" # Set the application server username _www www-data apache
GROUP="_www" # Set the application server groupname _www www-data apache
FILE_PERM=644 # Add the numerical permission value for files, usually is 644
DIR_PERM=755 # Add the numerical permission value for folders, usually is 755
VERSION=$1 # This value store the parameter entered on the script execution
DIR=drupal-$VERSION # This is the folder name created after update file download and extraction
FILE=$DIR.tar.gz # This is the filename of the update to retrieve from Drupal official FTP site
URL="https://ftp.drupal.org/files/projects/$FILE" # This is the full URL of the update file
# FLAGS
INSTALLATION_FLAG=TRUE # Enables/Disables the whole update process
CACHE_FLAG=FALSE # Enables/Disables cache cleaning after updating everything
REPLACE_FLAG=TRUE # Enables/Disables the actual file and dir replacement on your project
PERMISSIONS_FLAG=TRUE # Enables/Disables the permissions hardening
CONFIG_FLAG=TRUE # Enables/Disables folders and files creation and verification needed to run this script
DELETE_INSTALLATION_FILES_FLAG=TRUE # Enables/Disables the installation files deletion
BACKUP_FLAG=FALSE # Enables/Disables a previous backup of the project
CUSTOM_DOMAINS_FLAG=FALSE # Enables/Disables the custom domain settings folder permissions hardening
# Main paths, folder and filenames assignment
BASE_PATH="/var/www" # Add your own path to the parent folder of your drupal installation
DESTINATION_PATH="$BASE_PATH/html" # Add your own path to the folder of your drupal installation
DOWNLOADS_PATH="~/downloads" # Add your own path to a temporary folder for the drupal installation files
LOGS_PATH="~/logs/$TIMESTAMP" # Add your own path to a logging folder to store every step of the process
BACKUP_PATH="~/backups/$TIMESTAMP" # Add your own path to a backup folder to store your drupal project before the update process begins
LOG_INSTALL=$LOGS_PATH/upgrade_drupal.log
# Special folder names assignment
SITES_PATH=$DESTINATION_PATH/sites # Drupal sites folder
CUSTOM_SITES_PATH=$SITES_PATH/domain # Assign your own domain name
DOMAINS=("com" "org" "net" "edu") # Array to assign multiple domain site
DEFAULT_SITE_PATH=$SITES_PATH/default # Drupal default domain configuration folder
FILES_PATH=$DEFAULT_SITE_PATH/files # Drupal assets folder
FOLDERS=("profiles" "modules" "themes" "includes" "misc" "scripts") # Array of folder names to replace on update
SETTINGS_FILE=settings.php # Drupal main settings file
# Checking some of the previous variable assignments
echo "\nUPDATE DRUPAL 7 CORE STARTED" >> $LOG_INSTALL
echo "**************************************************" >> $LOG_INSTALL
echo "The version selected for upgrade $VERSION" >> $LOG_INSTALL
echo "URL for download the update: $URL" >> $LOG_INSTALL
echo "The BASE_PATH: $BASE_PATH" >> $LOG_INSTALL
echo "The DESTINATION_PATH: $DESTINATION_PATH" >> $LOG_INSTALL
echo "The DOWNLOADS_PATH: $DOWNLOADS_PATH" >> $LOG_INSTALL
echo "The LOGS_PATH: $LOGS_PATH" >> $LOG_INSTALL
echo "The BACKUP_PATH: $BACKUP_PATH" >> $LOG_INSTALL
echo "**************************************************" >> $LOG_INSTALL
echo "Is the update process enabled? $INSTALLATION_FLAG" >> $LOG_INSTALL
echo "Is the previous configuration enabled? $CONFIG_FLAG" >> $LOG_INSTALL
echo "Is the backup process enabled? $BACKUP_FLAG" >> $LOG_INSTALL
echo "Is the file and folder replacement enabled? $REPLACE_FLAG" >> $LOG_INSTALL
echo "Is the permissions hardening enabled? $PERMISSIONS_FLAG" >> $LOG_INSTALL
echo "Is the installation files deletion enabled? $INSTALL_FILES_FLAG" >> $LOG_INSTALL
echo "**************************************************\n" >> $LOG_INSTALL
# Starts previous configuration steps
if [ $CONFIG_FLAG = TRUE ]
then
echo "\nStarts previous configuration steps" >> $LOG_INSTALL
echo "**************************************************" >> $LOG_INSTALL
if [ ! -d "$BASE_PATH" ] # Evaluates $BASE_PATH existance
then
sudo mkdir -p $BASE_PATH # Creates the $BASE_PATH path recursively
sudo chown -R $(whoami) $BASE_PATH # Assigns the $BASE_PATH the current user as owner
echo "$BASE_PATH created and $(whoami) assigned as owner." >> $LOG_INSTALL
ORIGIN_DIR=$(pwd)
cd $BASE_PATH
echo "Entering $BASE_PATH." >> $LOG_INSTALL
sudo find $(pwd) -type d -exec chmod -R $DIR_PERM {} \;
echo "$DIR_PERM folder permission assigned to $BASE_PATH." >> $LOG_INSTALL
cd $ORIGIN_DIR
echo "Returning back to $ORIGIN_DIR." >> $LOG_INSTALL
if [ -d "$BASE_PATH" ]
then
echo "$BASE_PATH exists: OK!" >> $LOG_INSTALL
else
echo "Can't create $BASE_PATH. Script end." >> $LOG_INSTALL
exit 1
fi
else
echo "$BASE_PATH exists: OK!" >> $LOG_INSTALL
fi
if [ ! -d "$LOGS_PATH" ] # Evaluates $LOGS_PATH existance
then
sudo mkdir -p $LOGS_PATH # Creates the $LOGS_PATH recursively
sudo chown -R $(whoami) $LOGS_PATH # Assigns the $LOGS_PATH the current user as owner
echo "$LOGS_PATH created and $(whoami) assigned as owner." >> $LOG_INSTALL
touch $LOG_INSTALL
echo "Log file $LOG_INSTALL created." >> $LOG_INSTALL
ORIGIN_DIR=$(pwd)
cd $LOGS_PATH
echo "Entering $LOGS_PATH." >> $LOG_INSTALL
sudo find . -type d -exec chmod -R $FILE_PERM {} \; # 777 if the logs aren't writting anything
echo "$FILE_PERM file permission assigned to $LOGS_PATH." >> $LOG_INSTALL
sudo find . -type d -exec chmod -R $DIR_PERM {} \;
echo "$DIR_PERM folder permission assigned to $LOGS_PATH." >> $LOG_INSTALL
cd $ORIGIN_DIR
echo "Returning back to $ORIGIN_DIR." >> $LOG_INSTALL
if [ -d "$LOGS_PATH" ]
then
echo "$LOGS_PATH exists: OK!" >> $LOG_INSTALL
else
echo "Can't create $LOGS_PATH. Script end." >> $LOG_INSTALL
exit 1
fi
else
echo "$LOGS_PATH exists: OK!" >> $LOG_INSTALL
fi
if [ ! -d "$DOWNLOADS_PATH" ] # Evaluates $DOWNLOADS_PATH existance
then
sudo mkdir -p $DOWNLOADS_PATH # Creates the $DOWNLOADS_PATH recursively
sudo chown -R $(whoami) $DOWNLOADS_PATH # Assigns the $DOWNLOADS_PATH the current user as owner
echo "$DOWNLOADS_PATH created and $(whoami) assigned as owner." >> $LOG_INSTALL
ORIGIN_DIR=$(pwd)
cd $DOWNLOADS_PATH
echo "Entering $DOWNLOADS_PATH." >> $LOG_INSTALL
sudo find $(pwd) -type d -exec chmod -R $DIR_PERM {} \;
echo "$DIR_PERM folder permission assigned to $DOWNLOADS_PATH." >> $LOG_INSTALL
cd $ORIGIN_DIR
echo "Returning back to $ORIGIN_DIR." >> $LOG_INSTALL
if [ -d "$DOWNLOADS_PATH" ]
then
echo "$DOWNLOADS_PATH exists: OK!" >> $LOG_INSTALL
else
echo "Can't create $DOWNLOADS_PATH. Script end." >> $LOG_INSTALL
exit 1
fi
else
echo "$DOWNLOADS_PATH exists: OK!" >> $LOG_INSTALL
fi
if [ ! -d "$BACKUP_PATH" ] # Evaluates $BACKUP_PATH existance
then
sudo mkdir -p $BACKUP_PATH # Creates the $BACKUP_PATH recursively
sudo chown -R $(whoami) $BACKUP_PATH
echo "$BACKUP_PATH created and $(whoami) assigned as owner." >> $LOG_INSTALL
ORIGIN_DIR=$(pwd)
cd $BACKUP_PATH
echo "Entering $BACKUP_PATH." >> $LOG_INSTALL
sudo find $(pwd) -type d -exec chmod -R $DIR_PERM {} \;
echo "$BACKUP_PATH folder permission assigned to $BACKUP_PATH." >> $LOG_INSTALL
cd $ORIGIN_DIR
echo "Returning back to $ORIGIN_DIR." >> $LOG_INSTALL
if [ -d "$BACKUP_PATH" ]
then
echo "$BACKUP_PATH exists: OK!" >> $LOG_INSTALL
else
echo "Can't create $BACKUP_PATH. Script end." >> $LOG_INSTALL
exit 1
fi
else
echo "$BACKUP_PATH exists: OK!" >> $LOG_INSTALL
fi
if [ ! -d "$DESTINATION_PATH" ]
then
echo "Your drupal project folder selected to update can't be found. Script end." >> $LOG_INSTALL
exit 1
else
echo "$DESTINATION_PATH exists: OK!" >> $LOG_INSTALL
fi
else
echo "Continue script without files and folders previous configuration. This might produce unexpected behaviour." >> $LOG_INSTALL
fi
if [ $BACKUP_FLAG = TRUE ]
then
sudo tar -zcf $BACKUP_PATH/$TIMESTAMP.tar.gz $DESTINATION_PATH >> $LOG_INSTALL
echo "\nStarts the backup of the project before the update." >> $LOG_INSTALL
echo "The file will be stored at $BACKUP_PATH/$TIMESTAMP.tar.gz." >> $LOG_INSTALL
echo "**************************************************" >> $LOG_INSTALL
else
echo "Continue script without backuping the project files." >> $LOG_INSTALL
fi
if [ ! -z "$VERSION" ] # Evaluates if the version is set
then
echo "\nStarts the update files retrieval and extraction." >> $LOG_INSTALL
echo "**************************************************" >> $LOG_INSTALL
cd $DOWNLOADS_PATH
echo "Entering $DOWNLOADS_PATH" >> $LOG_INSTALL
if [ ! -f "$FILE" ] # Evaluates if file was downloaded before
then
sudo wget $URL &> /dev/null # Procedes to download the file
if [[ "$?" != 0 ]]
then
echo "Can't download the $URL. Script end." >> $LOG_INSTALL
exit 1
else
echo "The $FILE was retrieved sucessfully via wget." >> $LOG_INSTALL
fi
else
echo "$FILE already downloaded." >> $LOG_INSTALL
fi
if [ $INSTALLATION_FLAG = TRUE ] # Evaluates if the update process is enabled
then
echo "\nStarts the extraction of the update files and the installation." >> $LOG_INSTALL
echo "**************************************************" >> $LOG_INSTALL
if [ ! -d "$DOWNLOADS_PATH/$DIR" ]
then
sudo tar xf $FILE >> $LOG_INSTALL # Extracting update files
echo "$FILE extracted successfully." >> $LOG_INSTALL
else
echo "The $DIR already exists." >> $LOG_INSTALL #TODO: Check if the folder is complete
fi
if [ -d "$DOWNLOADS_PATH/$DIR" ]
then
cd $DIR
echo "Entering $DIR." >> $LOG_INSTALL
if [ $REPLACE_FLAG = TRUE ]
then
echo "\nStarts the files and folders replacement with the new version $VERSION." >> $LOG_INSTALL
echo "**************************************************" >> $LOG_INSTALL
ORIGIN_DIR=$(pwd)
cd $DESTINATION_PATH
sudo find $DESTINATION_PATH -maxdepth 1 -type f | xargs cp -t $BACKUP_PATH >> $LOG_INSTALL
echo "All drupal actual files in root at $DESTINATION_PATH backed up."
sudo find . -maxdepth 1 -type f -name '*' -exec mv -n {} "$DESTINATION_PATH" \; >> $LOG_INSTALL
echo "All drupal installation files in root at $DESTINATION_PATH replaced."
for i in "${FOLDERS[@]}"
do
:
if [ -d "$i" ] # Evaluates the existance
then
sudo cp -r "$DESTINATION_PATH/$i" "$BACKUP_PATH" # Backup the folder to be replaced
echo "Backed up folder $DESTINATION_PATH/$i"
sudo rm -rf "$DESTINATION_PATH/$i"
echo "Deleted folder $DESTINATION_PATH/$i"
sudo mv "$i" "$DESTINATION_PATH"
echo "Replaced folder $DESTINATION_PATH/$i"
else
echo "The installation folder is not complete. The folder $i is missing. The update can't continue."
echo "\nStarts the rollback" >> $LOG_INSTALL
echo "**************************************************" >> $LOG_INSTALL
for j in "${FOLDERS[@]}"
do
:
# Restore replaced folders
sudo rm -rf "$DESTINATION_PATH/$j"
echo "$DESTINATION_PATH/$j deleted."
sudo mv "$BACKUP_PATH/$j" "$DESTINATION_PATH"
echo "$DESTINATION_PATH/$j restored."
if [$j = $i]
then
break #Stops on the folder missing
fi
done
#Restore replaced files
sudo find "$BACKUP_PATH" -maxdepth 1 -type f -name '*' -exec mv -n {} "$DESTINATION_PATH" \; >> $LOG_INSTALL
echo "Files in root at $DESTINATION_PATH are restored."
exit 1
fi
done
fi
cd $DOWNLOADS_PATH
echo "Returning back to $DOWNLOADS_PATH.\n"
if [ $DELETE_INSTALLATION_FILES_FLAG = TRUE ] # Evaluates if installation files are going to be deleted
then
echo "\nStarts installation files deletion." >> $LOG_INSTALL
echo "**************************************************" >> $LOG_INSTALL
sudo rm -rf $DIR > $LOG_INSTALL
sudo rm $FILE > $LOG_INSTALL
echo "\n"
fi
if [ $PERMISSIONS_FLAG = TRUE ] # Evaluates if permissions hardening is enabled
then
cd $DESTINATION_PATH
echo "\nStarts the permissions hardening." >> $LOG_INSTALL
echo "**************************************************" >> $LOG_INSTALL
sudo chown -R $USER:$GROUP $(pwd)
echo "Switched to $USER:$GROUP user:group names." >> $LOG_INSTALL
sudo find . -type f -exec chmod -v $FILE_PERM {} \;
echo "Switched all file permissions to $FILE_PERM." >> $LOG_INSTALL
sudo find . -type d -exec chmod -v $DIR_PERM {} \;
echo "Switched all folder permissions to $DIR_PERM." >> $LOG_INSTALL
sudo chmod -v 444 .htaccess
echo "Switched the file .htaccess permission to 444." >> $LOG_INSTALL
sudo chmod -v 555 $DEFAULT_SITE_PATH
echo "Switched the folder $DEFAULT_SITE_PATH permissions to 555." >> $LOG_INSTALL
sudo chmod -v 400 $DEFAULT_SITE_PATH/$SETTINGS_FILE
echo "Switched the file $DEFAULT_SITE_PATH/$SETTINGS_FILE permissions to 400.\n" >> $LOG_INSTALL
if [ $CUSTOM_DOMAINS_FLAG = TRUE ] # Evaluates custom domains folders and files permissions hardening is enabled
echo "\nStarts the permissions hardening of custom domains." >> $LOG_INSTALL
echo "**************************************************" >> $LOG_INSTALL
for i in "${DOMAINS[@]}"
do
:
sudo chmod -v 555 $CUSTOM_SITES_PATH.$i/
echo "Switched the folder $CUSTOM_SITES_PATH.$i permissions to 555." >> $LOG_INSTALL
sudo chmod -v 400 $CUSTOM_SITES_PATH.$i/$SETTINGS_FILE
echo "Switched the file $CUSTOM_SITES_PATH.$i/$SETTINGS_FILE permissions to 400." >> $LOG_INSTALL
done
else
echo "Continue without custom domains permissions hardening." >> $LOG_INSTALL
fi
sudo find $FILES_PATH/ -type f -exec chmod -R 664 {} \;
echo "Switched the file $FILES_PATH/ permissions to 664." >> $LOG_INSTALL
sudo find $FILES_PATH/ -type d -exec chmod -R 775 {} \;
echo "Switch the folder permissions into the $FILES_PATH/ to 775.\n" >> $LOG_INSTALL
fi
if [ $CACHE_FLAG = TRUE ] # Evaluates if the cache cleaning is enabled
then
# This scripts assumes mod_pagespeed, redis and drush are installed in the target server
# Feel free to uncomment or add what you need
echo "\nStarts the cache cleaning process." >> $LOG_INSTALL
echo "**************************************************" >> $LOG_INSTALL
# Cleans the PageSpeed module cache
#sudo touch /var/cache/mod_pagespeed/cache.flush
# Cleans the Redis cache
#sudo redis-cli flushall
# Cleans the Memcached PHP module cache
#sudo echo 'flush_all' | nc localhost 11211
ORIGIN_DIR=$(pwd)
cd $DESTINATION_PATH
# Cleans Drupal cache with Drush
#sudo drush cc all
cd $ORIGIN_DIR
echo "The cache has been cleaned successfully.\n" >> $LOG_INSTALL
fi
else
echo "\nUnable to extract installation file $VERSION.\n" >> $LOG_INSTALL
exit 1
fi
else
echo "\nThe update process is not enabled\n" >> $LOG_INSTALL
exit 1
fi
else
echo "\nThe parameter with the version to update was not entered. Script end.\n" >> $LOG_INSTALL
exit 1
fi
echo "\nAll the detailed information is stored in the file $LOG_INSTALL\n" >> $LOG_INSTALL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.