0 5 * * 5 aem /opt/aem/help/compact.sh
Check out compact.sh on GitHub
So how do you run a compaction across an entire environment without causing downtime? To do this, I created a script which will sequentially stop the instances in an environment and the associated dispatchers, ensuring the load balancer will direct traffic to the alternate instance during the compaction process.
This script is set up to run in a 2-publish, single author environment, but you could adapt it to more significant buildouts. To use the script, deploy the compaction script to all of the servers, then replace the relevant server information in the ssh commands below for your particular environment.
#!/bin/bash
cat << EOF
WARNING!!!
-------
This script will run compaction on the Prod environment.
During the course of this operation, the Author and Publish AEM Instances will not be available.
Please ensure before starting appropriate backups have been taken!
EOF
read -p "Do you want to continue? (y/n) " CONT
if [ $CONT = "y" ]; then
echo "Starting compaction of Prod environment..."
echo "Stopping Author Dispatcher..."
ssh -i ~/.ssh/client-prod.pem -t user@dispatcher-author.prod.client.com 'sudo service httpd stop'
echo "Running compaction script in Author..."
ssh -i ~/.ssh/client-prod.pem -t user@aem-author.prod.client.com 'sudo su - aem /opt/aem/help/compact.sh'
echo "Stopping Publish Dispatcher 1..."
ssh -i ~/.ssh/client-prod.pem -t user@dispatcher-publish-01.client.com 'sudo service httpd stop'
echo "Running compaction script in Publish 1..."
ssh -i ~/.ssh/client-prod.pem -t user@aem-publish-01.client.com 'sudo su - aem /opt/aem/help/compact.sh'
echo "Starting Publish Dispatcher 1..."
ssh -i ~/.ssh/client-prod.pem -t user@dispatcher-publish-01.client.com 'sudo service httpd start'
echo "Waiting for ELB resumption..."
sleep 5m
echo "Stopping Apache 2..."
ssh -i ~/.ssh/client-prod.pem -t user@dispatcher-publish-02.client.com 'sudo service httpd stop'
echo "Running compaction script in Publish 2..."
ssh -i ~/.ssh/client-prod.pem -t user@aem-publish-02.client.com 'sudo su - aem /opt/aem/help/compact.sh'
echo "Starting Apache 2..."
ssh -i ~/.ssh/client-prod.pem -t user@dispatcher-publish-02.client.com 'sudo service httpd start'
echo "Starting Author Dispatcher..."
ssh -i ~/.ssh/client-prod.pem -t user@dispatcher-author.prod.client.com 'sudo service httpd start'
echo "Prod Compacted!"
fi
I also updated the aem-mgr.sh script I described in Managing Multiple AEM Instances to add a new compaction option.
This script will not stop and start AEM, but will run compaction on your local author and publish instances. In order to enable compaction, update every local instance you would want to compact with the following:
Create a folder help
as a sibling to crx-quickstart
Download the appropriate oak-run.jar into the help folder
Check out aem-mgr.sh on GitHub
With the upcoming release of AEM 6.3, we anticipate support for online compaction. This will significantly ease the maintainance of AEM by eliminating the need for offline compaction. In the meatime, I hope this article is helpful for anyone using AEM 6.0, 6.1 or 6.2.