This article explains how we can split the log files based on size and also how we can move the backup files to another folder using shell script. When we use log4j configuration to split the file based on size, we have a limitation that there is a possibility that we may lose a particular backup file, as it works as below. To avoid that we need to create a backup script and that also provided in this article.
Consider we are setting as below:
log4j.appender.CARBON_LOGFILE.MaxFileSize=1MB
log4j.appender.CARBON_LOGFILE.MaxBackupIndex=5
It will keep only 5 backup files, when the 6th ( means the wso2carbon.log reached 1MB ), it will delete the 1st backup file created that will be wso2carbon.log.5 and reassign the order, like wise old ones will be deleted keeping the latest.
Below are the steps to follow:
1) Take a backup of the ESB_HOME/repository/conf/log4j.properties.
2) Enable the wire logs in log4j.properties
Uncomment the below lines:
#log4j.logger.org.apache.synapse.transport.http.headers=DEBUG
#log4j.logger.org.apache.synapse.transport.http.wire=DEBUG
3) Follow the instructions [1].
[1]
https://docs.wso2.com/display/ADMIN44x/Monitoring+Logs
Change the log4j.appender.CARBON_LOGFILE=org.wso2.carbon.utils.logging.appenders.CarbonDailyRollingFileAppenderappender
in the <PRODUCT_HOME>/repository/conf/ log4j.properties file as follows:
log4j.appender.CARBON_LOGFILE=org.apache.log4j.RollingFileAppender
Add the following two properties under RollingFileAppender
log4j.appender.CARBON_LOGFILE.MaxFileSize=100MB
log4j.appender.CARBON_LOGFILE.MaxBackupIndex=200
4) Restart the Server.
Once done you can see the output inside ESB_HOME/repository/logs folder as below:
5) Configure the cron job to run the backup script.
cd /etc/cron.daily/
chmod 755 wso2backuplogscript-esb.sh
#!/bin/bash
ESB_HOME="/home/ajanthan/log_split/wso2esb-5.0.0"
DIRECTORY="$ESB_HOME/repository/logs"
BACKUP_SRC_FILES="wso2carbon.log.*"
DESTINATION="$DIRECTORY/archivefolder"
TIME=`date "+%Y%m%d-%H%M%S"`
FILENAME=backup-wso2carbon.$TIME.tar.gz
echo "[`date '+%Y-%m-%d %H:%M:%S'`] Starting the backup process...\r\n" ;
echo "Source Directory : $DIRECTORY\n";
echo "Destination Directory : $DESTINATION\n";
if ls $DIRECTORY/$BACKUP_SRC_FILES 1> /dev/null 2>&1;
then
echo "[`date '+%Y-%m-%d %H:%M:%S'`] Files available for backup. \n";
cd $DIRECTORY && tar -cvpzf $DESTINATION/$FILENAME $BACKUP_SRC_FILES #create,verbose,preserve permission,zip,filename
if [ -f "$DESTINATION/$FILENAME" ]
then
#echo -e "Inside then...\n";
echo "[`date '+%Y-%m-%d %H:%M:%S'`] $DESTINATION/$FILENAME found.\n";
echo "[`date '+%Y-%m-%d %H:%M:%S'`] Starting to remove the files from Source : $BACKUP_SRC_FILES\r\n";
rm -fv $BACKUP_SRC_FILES; #Avoiding the prompt for each file and also printing what are the files got removed by using -fv.
echo "[`date '+%Y-%m-%d %H:%M:%S'`] File removal END.\r\n";
else
#echo -e "Inside elsee...\n";
echo "[`date '+%Y-%m-%d %H:%M:%S'`] $DESTINATION/$FILENAME not found.\r\n";
fi
else
echo "[`date '+%Y-%m-%d %H:%M:%S'`] Files do not available for backup.\n";
fi
echo "\r\n[`date '+%Y-%m-%d %H:%M:%S'`] Ending the backup process...\r\n";
6) Create a folder for the backup files
ESB_HOME/repository/logs/archivefolder
ESB_HOME/repository/logs/backupprocesslogs
7) Add new cron job to crontab:
crontab -e
For Testing
*/5 * * * * sh /etc/cron.daily/wso2backuplogscript-esb.sh >> ESB_HOME/repository/logs/backupprocesslogs/backup-$(date +\%Y-\%m-\%d-\%H-\%M-\%S).log 2>&1
8) Check the log at $ESB_HOME/repository/logs/backupprocesslogs-esb/
Check the backup files at $ESB_HOME/repository/logs/archive-esblogs
Also can check the process related log at ESB_HOME/repository/logs/backupprocesslogs. The sample output will be as below: