#!/bin/sh

#-----------------------------------------------------
#
# httpd log backup
#   - 30  days ago files archive
#   - 35  days ago files move backup directory
#   - 500 days ago files remove
#
#-----------------------------------------------------

#######################################################
# Define Constants
#######################################################
today=`date +%d`
echo today=$today

# - Date
archiveLogYear=`date --date '30 days ago' +%Y`          #로그압축 년
archiveLogMonth=`date --date '30 days ago' +%m`         #로그압축 월
archiveLogDay=`date --date '30 days ago' +%d`           #로그압추 일

backupLogYear=`date --date '60 days ago' +%Y`           #백업로그 년
backupLogMonth=`date --date '60 days ago' +%m`          #백업로그 월
backupLogDay=`date --date '60 days ago' +%d`            #백업로그 일

makeLogYearMonth=`date +%Y%m`                                           #로그파일 년월 디렉토리

removeLogYearMonth=`date --date '500 days ago' +%Y%m`   #로그파일 삭제 년 월

# - Dir & File
srcLogDir=/box/httpd_logs                              #로그 현재 경로
logBackupDir=/box/log_backup/http              #로그 백업 경로

findArchiveFileExtYM=${archiveLogYear}${archiveLogMonth}                                        #아카이브 년 월 확인
findArchiveFileExtYMD=${archiveLogYear}${archiveLogMonth}${archiveLogDay}       #아카이브 년 월 일 확인

findBackupFileExt=${backupLogYear}${backupLogMonth}${backupLogDay}                      #백업파일 년월일
findBackupFileExtYM=${backupLogYear}${backupLogMonth}

echo "today : ${today}"
echo "srcLogDir     : ${srcLogDir}"
echo "logBackupDir          : ${logBackupDir}"
echo "makeLogYearMonth      : ${makeLogYearMonth}"
echo "removeLogYearMonth    : ${removeLogYearMonth}"
echo "findArchiveFileExtYM  : ${findArchiveFileExtYM}"
echo "findArchiveFileExtYMD : ${findArchiveFileExtYMD}"
echo "findBackupFileExt     : ${findBackupFileExt}"
echo "findBackupFileExtYM   : ${findBackupFileExtYM}"

# 백업 디렉토리가 없으면 생성
       ###########################################
       # Check Backup Logging Directory
       ###########################################
       if [ -d ${logBackupDir}/${makeLogYearMonth} ]; then

         echo """"
         echo "Make Directory ${logBackupDir}/${makeLogYearMonth} ${logBackupDir}/${findArchiveFileExtYM} ${logBackupDir}/${findBackupFileExtYM}"
         echo """"

       else
         mkdir -p ${logBackupDir}/${makeLogYearMonth} ${logBackupDir}/${findArchiveFileExtYM} ${logBackupDir}/${findBackupFileExtYM};
       fi

echo ""
echo "#######################################################"
echo "# ${archiveLogDate} HTTPD log Archive Process START ###"
echo "find ${srcLogDir} -name \"*${findArchiveFileExtYMD}*\""
echo "#######################################################"

   if [ x0 == x`find ${srcLogDir} -name "*${findArchiveFileExtYMD}*" | wc -l` ]; then
       echo " not found"
   else
       echo "gzip start"
       find ${srcLogDir} -name "*${findArchiveFileExtYMD}*" -type f | sort | while read FNAME
       do
           echo "gzip -f ${FNAME}"
                 gzip -f ${FNAME}
       done

   fi

sleep 1;

echo "#######################################################"
echo "# ${archiveLogDate} HTTPD log Archive Process DONE ####"
echo "#######################################################"

echo "################################################################"
echo "# ${backupLogDate} HTTPD log backup Directory Move Process START"
echo "################################################################"

   echo "find ${srcLogDir} -name *${findBackupFileExtYM}*.gz"
   find ${srcLogDir} -name "*${findBackupFileExtYM}*.gz" -type f | sort | while read FNAME

   do
       echo "${FNAME} ${logBackupDir}/${backupLogYear}${backupLogMonth}"
       mv ${FNAME} ${logBackupDir}/${backupLogYear}${backupLogMonth}/
   done
sleep 1;

echo "################################################################"
echo "# ${backupLogDate} HTTPD log backup Directory Move Process DONE#"
echo "################################################################"


echo "#######################################################"
echo "# ${removeLogYearMonth} HTTPD log REMOVE Process START#"
echo "#######################################################"

    if [ -d ${logBackupDir}/${removeLogYearMonth} ]; then
        echo "remove"
        rm -rf  ${logBackupDir}/${removeLogYearMonth}
    fi
sleep 1;

echo "#######################################################"
echo "# ${removeLogYearMonth} HTTPD log REMOVE Process DONE #"
echo "#######################################################"
