Question : create bash script that archives a file if 1 year old or 1gb is size.

Experts
 I need to rename a log file to log.YYYY_SEQ on the first day of each year or when the size reaches 1GB.   Obviously the "YYYY" means the current year. And "SEQ" equals the log count.  The SEQ should end at 2 but allow me to increas the number to more if I need to.

Answer : create bash script that archives a file if 1 year old or 1gb is size.

This script should be run by the crontab.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
#/bin/bash
 
#LOG is the desired log file
LOG="/var/log/logfile"
DATE=`date +%Y`
 
if [ ! -f $LOG ]
then
    exit 0
fi
 
SIZE=`stat -c %s $LOG`
 
FLAG=0
#check 1st day of the year
if [ `date +%d` == 1 -a `date +%m` == 1]
then
    FLAG=1
fi
 
#check size
if [ $SIZE -ge 1073741824 ]
then
    FLAG=1
fi
 
if [ -f $FLAG ]
then
    SEQ="00"
    NEWLOG=$LOG_$DATE_$SEQ
    while [ -f $NEWLOG ]
    do 
        SEQ=$(($SEQ+1))
        NEWLOG=$LOG_$DATE_$SEQ
    done
    mv $LOG $NEWLOG 
fi
Open in New Window Select All
Random Solutions  
 
programming4us programming4us