You are here

/etc/ag/scripts/update-drupal

Submitted by Alan Mels on Fri, 10/12/2018 - 03:46

0) file=/etc/ag/scripts/update-drupal

cat > /etc/ag/scripts/update-drupal << 'EOT'
#!/bin/sh
 
export PATH="/usr/local/bin:$PATH"
 
if [ -f /opt/remi/php74/enable ]; then
  source /opt/remi/php74/enable
elif [ -f /opt/remi/php73/enable ]; then
  source /opt/remi/php73/enable
elif [ -f /opt/remi/php72/enable ]; then
  source /opt/remi/php72/enable
fi
 
doms=`virtualmin list-domains --name-only`
for dom in $doms; do
 
# Check if domain is disabled
partial="$(echo $dom | cut -c1-30)"
disabled="$(virtualmin list-domains --domain $dom --disabled | grep $partial)"
# Leave disabled domains out of process
if [ -z "$disabled" ]; then
 
  path=`virtualmin list-domains --domain $dom --home-only`
  echo "***********************************************************************************************"
  echo " Working in $path..."
  # Let's check if domain is set to Yes and skip otherwise
  drupal_auto_update="$(virtualmin list-custom --domain $dom | grep Autoupdate)"
  if [[ $drupal_auto_update == *"Yes"* ]]; then
    drupal=''
    if [ -d $path/public_html ]; then
      cd $path/public_html
      version=$(drush status | grep 'Drupal version')
      if [[ $version = *8.* ]]; then
        drupal='8'
      elif [[ $version = *7.* ]]; then
        drupal='7'
      elif [[ $version = *6.* ]]; then
        drupal='6'
      fi
      if [ -z "$drupal" ]; then
        echo "***********************************************************************************************"
        echo "It is not bootstrapping, so probably this is not a Drupal site. Skipping..."
      else
        user=`virtualmin list-domains --domain $dom --user-only`
        drush cc all >> /dev/null 2>&1
        drush cr >> /dev/null 2>&1
        status=$(drush ups drupal 2> /dev/null | grep 'Update available\|SECURITY UPDATE available')
        if [ -z "$status" ]; then
          echo "The latest version for $dom is already installed."
          echo "***********************************************************************************************"
        else
 #         exec 3>&1 1>>/tmp/log.txt 2>&1
          echo "*** This is an automatically generated email, please do not reply ***" >> /tmp/log.txt
          echo " " >> /tmp/log.txt
          echo "The $dom has been updated to the latest secure Drupal core." >> /tmp/log.txt
          echo " " >> /tmp/log.txt
          echo "Here is the log:" >> /tmp/log.txt
          echo " " >> /tmp/log.txt
          echo "***********************************************************************************************" >> /tmp/log.txt
          echo "Working in $path/public_html ..." >> /tmp/log.txt
          echo "Drupal "$drupal" is detected." >> /tmp/log.txt
          echo "Update is available for $dom. Backing up the website's files and database ..." >> /tmp/log.txt
          drush -q sql-query "DELETE FROM cache_form"
          systemctl restart mariadb
          drush ard default --tar-options="--exclude=.git"
          echo "Securing the .htaccess and robots.txt files ..." >> /tmp/log.txt
          mkdir -p ../keep
          mv .htaccess robots.txt -t ../keep
          echo "Upgrading the website ..." >> /tmp/log.txt
          drush -y en update 2>&1 >> /tmp/log.txt
          drush -y -q up drupal --no-backup
          find . -type f -name .htaccess -exec sed -i "s/FollowSymLinks/SymLinksIfOwnerMatch/g" {} \; && find . -type f -name file.inc -exec sed -i "s/FollowSymLinks/SymLinksIfOwnerMatch/g" {} \;
          mv -f ../keep/.htaccess .
          mv -f ../keep/robots.txt .
          rm -rf ../keep
          chown -R $user:$user ../public_html
          drush -y updb 2>&1 >> /tmp/log.txt
          echo "$dom has been updated to the latest Drupal core." >> /tmp/log.txt
          echo "***********************************************************************************************" >> /tmp/log.txt
          echo " " >> /tmp/log.txt
          echo "*** This is an automatically generated email, please do not reply ***" >> /tmp/log.txt
          # Now let's find parent domain until https://www.virtualmin.com/node/53104 is resolved
          parent=${dom#*.}
          virtualmin notify-domains --domain $parent --body-file /tmp/log.txt --subject "Your website $dom has been updated" --from noreply@altagrade.com
          rm -f /tmp/log.txt
        fi
      fi
    fi
  else
    echo "Autoupdate for $dom is set to No, so skipping..."
    echo "***********************************************************************************************"
  fi
else
  echo "$dom is disabled, so skipping"
  echo "***********************************************************************************************"
fi
done
echo "***********************************************************************************************"
echo "Drupal core updates are complete."
echo "***********************************************************************************************"
exit
EOT
1)
chmod 755 /etc/ag/scripts/update-drupal
ln -s /etc/ag/scripts/update-drupal /usr/local/sbin/update-drupal