Quantcast
Channel: Bash – Jackie Chen's IT Workshop
Viewing all articles
Browse latest Browse all 22

Bash: Log File Archive Script

$
0
0

There are some logs files are in such format: 20120130-174201-130947.log. I created two bash scripts to archive them by year/month/day: The first one is based on the log file creation date, the other one is based on the time stamp within the file name.

#Backup Script1: Use modification date of the file
#!/bin/bash

#Define the Log files location
LOGS_FOLDER=~/scripts/log

#Define the Archived log files location
ARCHIVE_FOLDER=~/scripts/archive

#Only archive the files older than X Days, the format is +n
X=+90

#Create a temp folder for processing log files
mkdir -p $ARCHIVE_FOLDER/temp

#Move the logs that older than X days to a temp folder
mv -f $(find $LOGS_FOLDER -type f -mtime $X) $ARCHIVE_FOLDER/temp

#Process the archived logs to categarize them based on the Year/Month/Day
for log in $ARCHIVE_FOLDER/temp/*
do
    date=`echo $log | grep -oE ’20[0-9][0-9][0-9][0-9][0-9][0-9]‘`
    year=${date:0:4}
    month=${date:4:2}
    day=${date:6:2}
   
    mkdir -p $ARCHIVE_FOLDER/$year
    mkdir -p $ARCHIVE_FOLDER/$year/$month
    mkdir -p $ARCHIVE_FOLDER/$year/$month/$day
    cp -rf $log $ARCHIVE_FOLDER/$year/$month/$day
 
done

#Remove the temp folder
rm -rf temp

—————————————————————————–

#Backup Script2: Use time stamp within the filename
#!/bin/bash

#Define the Log files location
LOGS_FOLDER=~/scripts/log

#Define the Archived log files location
ARCHIVE_FOLDER=~/scripts/archive

#Only archive the files older than X Days
X=180

ARC_DATE=`date –date=”$X day ago” -I`
echo $X days ago, it is $ARC_DATE
echo All logs older than $ARC_DATE will be archived
ARC_DATE_S=`date –date=”$ARC_DATE” +%s`
#echo ARC_DATE_S is $ARC_DATE_S

#Archived logs and categorize them based on the Year/Month/Day
for log in $LOGS_FOLDER/*
do
    date=`echo $log | grep -oE ’20[0-9][0-9][0-9][0-9][0-9][0-9]‘`
    year=${date:0:4}
    month=${date:4:2}
    day=${date:6:2}

    LOG_DATE=$year-$month-$day
    echo This log $log is on $LOG_DATE
    LOG_DATE_S=`date –date=”$LOG_DATE” +%s`
    #echo LOG_DATE_S is $LOG_DATE_S
   
    n=$(($ARC_DATE_S – $LOG_DATE_S))
           
    if [[ $n == -* ]]; then
    echo “Don’t archive this log”
    echo “*************************************************”
    else
    echo “Archive this log”
    echo “*************************************************”
    mkdir -p $ARCHIVE_FOLDER/$year
    mkdir -p $ARCHIVE_FOLDER/$year/$month
    mkdir -p $ARCHIVE_FOLDER/$year/$month/$day
    mv $log $ARCHIVE_FOLDER/$year/$month/$day
     fi
done



Viewing all articles
Browse latest Browse all 22

Trending Articles