January 4, 2021
Estimated Post Reading Time ~

AEM — Offline Compaction by LINUX

SYNOPSIS
AEM Offline TAR Compaction script, using a custom logback configuration file with custom encoder for better logging.

For Windows
If you wish to execute on windows environment use the THIS script

Guide
#!/bin/bash

##############    AEM Offline TAR Compaction  ####################################
#                                          #
# ███████╗██╗  ██╗██╗   ██╗██████╗ ███████╗██╗   ██╗ ██████╗ ██████╗ ███████╗    #   
# ██╔════╝██║ ██╔╝╚██╗ ██╔╝██╔══██╗██╔════╝██║   ██║██╔═══██╗██╔══██╗██╔════╝    # 
# ███████╗█████╔╝  ╚████╔╝ ██║  ██║█████╗  ██║   ██║██║   ██║██████╔╝███████╗    # 
# ╚════██║██╔═██╗   ╚██╔╝  ██║  ██║██╔══╝  ╚██╗ ██╔╝██║   ██║██╔═══╝ ╚════██║    #
# ███████║██║  ██╗   ██║   ██████╔╝███████╗ ╚████╔╝ ╚██████╔╝██║     ███████║    #
# ╚══════╝╚═╝  ╚═╝   ╚═╝   ╚═════╝ ╚══════╝  ╚═══╝   ╚═════╝ ╚═╝     ╚══════╝    #
#                                        #
#                                        #
# SCRIPT: AEM Offline TAR Compaction                       #
# AUTHOR: Shashi Yebbare                             #
# DATE:   FEB, 09, 2019                                #
# REV:    1.3.P                                  #
#         (For Alpha, Beta, Dev, Test and Production)                  #
#                                        #
# PLATFORM: AIX, HP-UX, Linux, Solaris                       #
#                                        #
# PURPOSE:  To perform offline AEM compaction on Segmentstore          #
#           and reduce the size of the AEM repository, Script            #
#       uses a custom log-back XML configuration to log                #
#     additional compaction information.                   #
#                                          # 
# REVISED LIST:                                  #
#        DATE: FEB, 17 2019                            #
#        BY: Shashi Yebbare                            #
#        MODIFICATION: Added custom log back XML configuration           #
#                                        #
#                                        #
# set -n   # Uncomment to check your syntax, without execution.          #
#          # NOTE: Do not forget to put the comment back in or           #
#          #       the shell script will not execute!              #
#                                        #
##################################################################################

# ####################### BEGIN -- Parameter declaration ##########################

Date=$(date +%m-%d-%Y) # Get Current Date
Time=$(date +%T) # Get Current Time date +"%T"
Type="Author"
Port="4502"
Base="/opt/aem/${Type}/crx-quickstart"
Segmentstore="${Base}/repository/segmentstore"
OAK_JAR="/opt/aem/${Type}/support/oak-run.jar"
SUPPORTDIR="/opt/aem/${Type}/support"
CRXLOGS="${Base}/logs"
LOGFILE="TarComapction_${Date}_${Time}.log"
COUNT=0
DUMPSPATH="${SUPPORTDIR}/compactiondumps"
COMPACTARGS="-Dtar.memoryMapped=true  -Doak.compaction.eagerFlush=true -Dcompaction-progress-log=5000000 -Dcompress-interval=150000000 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${DUMPSPATH}"
LOGBACKCONF="${SUPPORTDIR}/logback-compaction.xml"
SDMEMORY="8g"

# ####################### END -- Parameter declaration ############################

# ############# ############# #############
# ##       TIME TO RUN THE SCRIPT        ##
# ##                                     ##
# ## You shouldn't need to edit anything ##
# ## beneath this line                   ##
# ##                                     ##
# ############# ############# #############

# BEGIN -- Function checks the storagae size of segmentstore
function SDRepositorySize {
  echo ">>>> Repository Size: "
  echo " " | tee -a "$CRXLOGS/$LOGFILE"
  du -h $Segmentstore  | tee -a "$CRXLOGS/$LOGFILE"
  echo " " | tee -a "$CRXLOGS/$LOGFILE"
} # END -- Function checks the storagae size of segmentstore

# BEGIN -- Function to bring AEM instance offline
function SDStopAEM {
  echo " " | tee -a "$CRXLOGS/$LOGFILE"
  echo ">>>> Stopping AEM ${Type} Instance"  | tee -a "$CRXLOGS/$LOGFILE"
  Command="bash $Base/bin/stop"
  eval $Command | tee -a "$CRXLOGS/$LOGFILE"
  echo " " | tee -a "$CRXLOGS/$LOGFILE"
  echo "Waiting 10 seconds" | tee -a "$CRXLOGS/$LOGFILE"
  echo " " | tee -a "$CRXLOGS/$LOGFILE"
  sleep 10s
  PID=$(cat $Base/conf/cq.pid 2>/dev/null)
  if [ "$PID" ]; then
     echo " " | tee -a "$CRXLOGS/$LOGFILE"
     echo "AEM PID $PID" | tee -a "$CRXLOGS/$LOGFILE"
     echo " " | tee -a "$CRXLOGS/$LOGFILE"
     if ps -p $PID > /dev/null 2>&1; then
      eval $Command | tee -a "$CRXLOGS/$LOGFILE"
      sleep 30s
      while ps -p $PID > /dev/null 2>&1 && (($COUNT < $MAXCOUNT)); do
       sleep 10s
       let COUNT=COUNT+1
      done
     else
      echo "AEM was already stopped" | tee -a "$CRXLOGS/$LOGFILE"
      echo " " | tee -a "$CRXLOGS/$LOGFILE"
     fi
  else
      echo "cq.pid was not found" | tee -a "$CRXLOGS/$LOGFILE"
      echo "AEM ${Type} is already stopped" | tee -a "$CRXLOGS/$LOGFILE"
  fi
  if ps -p $PID > /dev/null 2>&1; then
      echo "The AEM application is still running after maxcount" | tee -a "$CRXLOGS/$LOGFILE"
      WAIT_SECONDS=600
    echo "Waiting additional $WAIT_SECONDS for kill process to complete" | tee -a "$CRXLOGS/$LOGFILE"
    count=0

    while kill $PID > /dev/null
    do
      sleep 1
      ((count++))

      if ! ps -p $PID > /dev/null ; then
        break
      fi

      if [ $count -gt $WAIT_SECONDS ]; then
        kill -9 $PID
        break
      fi
    done
    echo "Process has been killed after $count seconds" | tee -a "$CRXLOGS/$LOGFILE"
    echo " " | tee -a "$CRXLOGS/$LOGFILE"
  fi
  echo "AEM ${Type} Successfully stopped" | tee -a "$CRXLOGS/$LOGFILE"
} # END -- Function to bring AEM instance offline

# BEGIN -- Offline Tar Comapction
function SDOfflineCompaction {
  echo ">>>> Starting Offline Tar Compaction Process" | tee -a "$CRXLOGS/$LOGFILE"
  echo " " | tee -a "$CRXLOGS/$LOGFILE"
  
  echo ">>>> Verifying Checkpoints"  | tee -a "$CRXLOGS/$LOGFILE"
  Command="java -server -Xmx${SDMEMORY} ${COMPACTARGS} -Dlogback.configurationFile=${LOGBACKCONF} -jar ${OAK_JAR} checkpoints $Segmentstore"
  eval $Command  | tee -a "$CRXLOGS/$LOGFILE"
  echo " " | tee -a "$CRXLOGS/$LOGFILE"
  
  echo ">>>> Removing UnReferenced Checpoints"  | tee -a "$CRXLOGS/$LOGFILE"
  Command="java -server -Xmx${SDMEMORY} ${COMPACTARGS} -Dlogback.configurationFile=${LOGBACKCONF} -jar ${OAK_JAR} checkpoints $Segmentstore rm-unreferenced"
  eval $Command | tee -a "$CRXLOGS/$LOGFILE"
  echo " " | tee -a "$CRXLOGS/$LOGFILE"
  
  echo ">>>> Compacting Segmentstore" | tee -a "$CRXLOGS/$LOGFILE"
  Command="java -server -Xmx${SDMEMORY} ${COMPACTARGS} -Dlogback.configurationFile=${LOGBACKCONF} -jar ${OAK_JAR} compact $Segmentstore"
  eval $Command | tee -a "$CRXLOGS/$LOGFILE"
  echo " " | tee -a "$CRXLOGS/$LOGFILE"
  
  echo "Waiting 10 seconds"  | tee -a "$CRXLOGS/$LOGFILE"
  echo " " | tee -a "$CRXLOGS/$LOGFILE"
  sleep 10
  echo ">>>> Compaction Process Successfully Completed" | tee -a "$CRXLOGS/$LOGFILE"
} # END -- Offline Tar Comapction

# BEGIN -- Start AEM Instance
function SDStartAEM {
  echo "Starting AEM ${Type} Instance"  | tee -a "$CRXLOGS/$LOGFILE"
  Command="cd $Base/bin && ./start"
  eval $Command | tee -a "$CRXLOGS/$LOGFILE"
  sleep 10
  #Get the process id from AEM
  PID=$(cat $Base/conf/cq.pid 2>/dev/null)
  ps -p $PID
} # END -- Start AEM Instance

# ############################## Actual Script Execution ##############################
# Executing the function accordingly to get repository size before compaction       # 
# and execute comapction and check size of the repository after comapction        #
#######################################################################################

echo " "
echo "
##############    AEM Offline TAR Compaction  ####################################
#                                          #
# ███████╗██╗  ██╗██╗   ██╗██████╗ ███████╗██╗   ██╗ ██████╗ ██████╗ ███████╗    #   
# ██╔════╝██║ ██╔╝╚██╗ ██╔╝██╔══██╗██╔════╝██║   ██║██╔═══██╗██╔══██╗██╔════╝    # 
# ███████╗█████╔╝  ╚████╔╝ ██║  ██║█████╗  ██║   ██║██║   ██║██████╔╝███████╗    # 
# ╚════██║██╔═██╗   ╚██╔╝  ██║  ██║██╔══╝  ╚██╗ ██╔╝██║   ██║██╔═══╝ ╚════██║    #
# ███████║██║  ██╗   ██║   ██████╔╝███████╗ ╚████╔╝ ╚██████╔╝██║     ███████║    #
# ╚══════╝╚═╝  ╚═╝   ╚═╝   ╚═════╝ ╚══════╝  ╚═══╝   ╚═════╝ ╚═╝     ╚══════╝    #
#                                        #
#                                        #
# SCRIPT: AEM Offline TAR Compaction                       #
# AUTHOR: Shashi Yebbare                             #
# DATE:   FEB, 09, 2019                                #
# REV:    1.3.P                                  #
#         (For Alpha, Beta, Dev, Test and Production)                  #
#                                        #
# PLATFORM: AIX, HP-UX, Linux, Solaris                       #
#                                        #
# PURPOSE:  To perform offline AEM compaction on Segmentstore          #
#           and reduce the size of the AEM repository, Script            #
#       uses a custom log-back XML configuration to log                #
#     additional compaction information.                   #
#                                          # 
# REVISED LIST:                                  #
#        DATE: FEB, 17 2019                            #
#        BY: Shashi Yebbare                            #
#        MODIFICATION: Added custom log back XML configuration           #
#                                        #
#                                        #
# set -n   # Uncomment to check your syntax, without execution.          #
#          # NOTE: Do not forget to put the comment back in or           #
#          #       the shell script will not execute!              #
#                                        #
##################################################################################

" | tee -a "$CRXLOGS/$LOGFILE"

Date=$(date +%m-%d-%Y) # Get Current Date
OutTime=$(date +%r) # Get Current Time
echo " " | tee -a "$CRXLOGS/$LOGFILE"
echo "Compaction Script -- Start Time: $Date @ $OutTime" | tee -a "$CRXLOGS/$LOGFILE"

echo " " | tee -a "$CRXLOGS/$LOGFILE"
SDStopAEM;
echo " " | tee -a "$CRXLOGS/$LOGFILE"
echo ">>>> Checking Repository size BEFORE Compaction" | tee -a "$CRXLOGS/$LOGFILE"
SDRepositorySize;
echo " " | tee -a "$CRXLOGS/$LOGFILE"
SDOfflineCompaction;
echo " " | tee -a "$CRXLOGS/$LOGFILE"
echo ">>>> Checking Repository size AFTER Compaction" | tee -a "$CRXLOGS/$LOGFILE"
SDRepositorySize;
echo " " | tee -a "$CRXLOGS/$LOGFILE"
SDStartAEM;

# Concluding the Compaction Process
Date=$(date +%m-%d-%Y) # Get Current Date
OutTime=$(date +%r) # Get Current Time
echo " " | tee -a "$CRXLOGS/$LOGFILE"
echo "Compaction Script -- End Time: $Date @ $OutTime" | tee -a "$CRXLOGS/$LOGFILE"

LOGBACK File
<?xml version="1.0" encoding="UTF-8"?>
<!-- 
##############    AEM Offline TAR Compaction  ####################################
#                                          #
# ███████╗██╗  ██╗██╗   ██╗██████╗ ███████╗██╗   ██╗ ██████╗ ██████╗ ███████╗    #   
# ██╔════╝██║ ██╔╝╚██╗ ██╔╝██╔══██╗██╔════╝██║   ██║██╔═══██╗██╔══██╗██╔════╝    # 
# ███████╗█████╔╝  ╚████╔╝ ██║  ██║█████╗  ██║   ██║██║   ██║██████╔╝███████╗    # 
# ╚════██║██╔═██╗   ╚██╔╝  ██║  ██║██╔══╝  ╚██╗ ██╔╝██║   ██║██╔═══╝ ╚════██║    #
# ███████║██║  ██╗   ██║   ██████╔╝███████╗ ╚████╔╝ ╚██████╔╝██║     ███████║    #
# ╚══════╝╚═╝  ╚═╝   ╚═╝   ╚═════╝ ╚══════╝  ╚═══╝   ╚═════╝ ╚═╝     ╚══════╝    #
#                                                                                #
#                                                                                #
# SCRIPT: AEM Offline TAR Compaction - logback-compaction configuration          #
# AUTHOR: Shashi Yebbare                                                         #
# DATE:   FEB, 09, 2019                                                          #
# REV:    1.3.P                                                                  #
#         (For Alpha, Beta, Dev, Test and Production)                            #
#                                                                                #
# PLATFORM: AIX, HP-UX, Linux, Solaris                                           #
#                                                                                #
# PURPOSE:  Script to custom log additional compaction information.              #
#                                                                                # 
# REVISED LIST:                                                                  #
#        DATE: FEB, 17 2019                                                      #
#        BY: Shashi Yebbare                                                      #
#        MODIFICATION: Added custom encoder pattern                              #
#                                                                                #
#                                                                                #
# set -n   # Uncomment to check your syntax, without execution.                  #
#          # NOTE: Do not forget to put the comment back in or                   #
#          #       the shell script will not execute!                            #
#                                                                                #
##################################################################################
 -->
<configuration debug="true">
  <appender name="STDERR" class="ch.qos.logback.core.FileAppender">
    <file>"/Users/shashi/Sites/author/support/SD_Compaction.log"</file>
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>
  <logger name="org.apache.jackrabbit.oak.plugins.segment.Compactor" level="DEBUG"/>
  <root level="warn">
    <appender-ref ref="STDERR" />
  </root>
</configuration>


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.