Shell script to Backup/Maintain and Purge Log Files in Linux



In this post, I am going to share a script to the daily backup log file and keep old ones for 7 days and then purge them. You can change based on requirements.

Script:


#!/bin/bash
##############################################################
#Script to Manage, Backup and Purge Log Files
#Author          Version                  Date
#======== ========= ==========
#Himanshu 1.0 9-SEP-2021
###############################################################

v_date=`date +%F`
cd /u01/oracle/appldev/10.1.2/webcache/logs
cp -p access_log  access_log_${v_date}
cat /dev/null > access_log

cp -p event_log  event_log_${v_date}
cat /dev/null > event_log


find /u01/oracle/appldev/10.1.2/webcache/logs -type f -name "access_log_*" -mtime +7 -exec rm {} \;
find /u01/oracle/appldev/10.1.2/webcache/logs -type f -name "event_log_*" -mtime +7 -exec rm {} \;


cd /u01/oracle/appldev/10.1.2/j2ee/home/log/home_default_island_1

cp -p server.log  server.log_${v_date}
cat /dev/null > server.log

find /u01/oracle/appldev/10.1.2/j2ee/home/log/home_default_island_1 -type f -name "server.log_*" -mtime +7 -exec rm {} \;



Now I will schedule this in cron to run daily at 12:05 AM.

05 00 * * * /home/appldev/scripts/logs_purge_scripts.sh > /tmp/logs_purge_scripts.log > 2>&1


If you like please follow and comment