hmm

#!/bin/bash
#
# Author: jbsnake
#
# Start configure: Hopefully you’ve read this far
#
# outfile=/var/log/httpd/log_rotation
outfile=/var/tmp/log_backups/log_rotation.out
num_days=30
uncompress_days=7
bob=-1
# log_dir=/var/log/httpd
log_dir=/var/tmp/log_backups
log_dir_old=${log_dir}/old/
old_access=${log_dir_old}access/
old_error=${log_dir_old}error/

# End configure

cur_date=`date +%Y%m%d`
outfile=”${outfile}.${cur_date}.out”

function usage
{
echo “”
echo “# ‘$0 -0′ (dry run)”
if [[ $bob == 0 ]]; then echo -e “# ‘$0 -f’ (live run)\n# Note: Double-check other configs before using”; fi
echo “”
exit
}

function daysbetween
{
future=$1
past=$2
echo “$(( ( $(date -d “$future” +%s) – $(date -d “$past” +%s) ) /86400 ))”
}

if [[ $bob != 0 && "$1" != "-0" ]]
then
usage;
elif [[ "$1" == "-0" ]]
then
bob=1
fi

if [[ "$1" != "-f" && $bob == 0 ]]
then
usage;
fi

# making file structure
if [[ $bob == 0 ]]
then
mkdir -p ${log_dir_old}
mkdir -p ${old_access}
mkdir -p ${old_error}
fi

# start logging
echo “### Starting log rotation: `date` ###” >> ${outfile}
echo “# ” >> ${outfile}
if [[ $bob != 0 ]]; then echo -e “# *** Dry Run *** \n# To do live run, change bob to 0\n#” >> ${outfile}; fi
echo “# Details: ” >> ${outfile}
echo “# Number of days retention:” >> ${outfile}
echo “# ${num_days}: Total (compressed and uncompressed)” >> ${outfile}
echo “# ${uncompress_days}: Uncompressed” >> ${outfile}
echo “# Log Directory to be rotated: ${log_dir}” >> ${outfile}
echo “# Old Log Directory (where the logs go):” >> ${outfile}
echo “# ${old_access}: Old Access Log files.” >> ${outfile}
echo “# ${old_error}: Old Error Log files.” >> ${outfile}
echo “### ” >> ${outfile}

# enter log directory
cd ${log_dir}

# work on error_logs
for error_file in `ls error_log.*`
do
log_date=`echo ${error_file} | sed “s#error_log\.##g” | sed ‘s/\.//g’`
if [[ "${log_date:${#log_date}-2}" == "gz" ]]
then
iscompress_tag=true
log_date=${log_date:0:${#log_date}-2}
else
iscompress_tag=false
fi

age_file=`daysbetween $cur_date $log_date`

if [[ "$age_file" -lt ${uncompress_days} ]]
then
echo “# File: $error_file newer than ${uncompress_days} days, skipping” >> $outfile
else

if [[ "$age_file" -gt $num_days ]]
then
echo “# Removed: $log_date – $age_file days old} : $error_file ” >> $outfile
if [[ $bob == 0 ]]; then rm -f ${log_dir}/${error_file}; fi
else
echo “# Compressing and moving: $error_file to ${old_error}${error_file}.gz” >> $outfile
if [[ $iscompress_tag == false ]]
then
echo “# Compressing: $error_file” >> $outfile
if [[ $bob == 0 ]]; then gzip ${log_dir}/${error_file}; fi
error_file=${error_file}.gz
fi
echo “# Moving: $error_file” >> $outfile
if [[ $bob == 0 ]]; then mv ${log_dir}/${error_file} ${old_error}; fi
fi
fi
done

# reset age_file
age_file=”"

# work on access_logs
for access_file in `ls access_log.*`
do
log_date=`echo ${access_file} | sed “s#access_log\.##g” | sed ‘s/\.//g’`
if [[ "${log_date:${#log_date}-2}" == "gz" ]]
then
iscompress_tag=true
log_date=${log_date:0:${#log_date}-2}
else
iscompress_tag=false
fi

age_file=`daysbetween $cur_date $log_date`

if [[ "$age_file" -lt $uncompress_days ]]
then
echo “# File: $access_file newer than ${uncompress_days} days, skipping” >> $outfile
else
echo “# Compressing and moving: $access_file to ${old_access}${access_file}.gz” >> $outfile
if [[ $iscompress_tag == false ]]
then
echo “# Compressing: $access_file” >> $outfile
if [[ $bob == 0 ]]; then gzip ${log_dir}/${access_file}; fi
access_file=${access_file}.gz
fi
echo “# Moving: $access_file” >> $outfile
if [[ $bob == 0 ]]; then mv ${log_dir}/${access_file} ${old_access}; fi
fi
done

# get out of log dir
cd – >/dev/null

# get into old dir
# add a matching for loop for access logs (if you want to remove some)
if [[ `grep "### Log rotation completed:" $outfile` && `ls -R ${log_dir_old} | grep ".gz" | wc -l` -gt 0 ]]
then
cd ${old_error}

# reset age_file
age_file=”"

# work on old error logs
for error_old in `ls error_log.*.gz`
do
log_date=`echo ${error_old} | sed “s#error_log\.##g” | sed ‘s/\.//g’ | sed ‘s/gz//g’`
age_file=`daysbetween $cur_date $log_date`

if [[ "$age_file" -gt $num_days ]]
then
echo “# Removed: $log_date : $error_old : $age_file days old” >> $outfile
if [[ $bob == 0 ]]; then rm -f ${old_error}${error_old}; fi
else
echo “# Skipped: $log_date : $error_old : $age_file days old” >> $outfile
fi
done
cd – >/dev/null
fi
# add matching for loop here
echo “### Log rotation completed: `date` ###” >> ${outfile}
echo “” >> ${outfile}

echo “View status at: ${outfile}”

Leave a Reply