how to configure logrotation for 31st of month or 28/29th day of feb - logrotate

how can I configure for 31 days of month . currently I am able to do with 30 days.
/var/log/myLogs/* {
daily
missingok
rotate 30
olddir /prar/oney_logs/tomcat_m_logs/archive
compress
copytruncate
postrotate
ls /prar/oney_logs/tomcat_m_logs/GC_web_*.log |grep -v `date --date 'today' +%y%m%d`| xargs rm -f
find /prar/oney_logs/tomcat_m_logs/archive/GC_web_*.log* -mtime +30 -exec rm {} \;
endscript
}

It's not so easy with logrotate. You can set rotate 31 to keep the max number of days a month can have. Then add a monthly cron job to archive the files per month. By setting 31 as the number of logs to keep, you are sure you have at least a months of files. If the month has less the 31 days, you will have 1 or more daily logs from last month left over.
Otherwise you can:
a) Handle by postrotate script in order to rename / move files for maonths with less than 31 days;
b) Make a crontab script to change logrotate config file each month;
c) Make a config file for each month and set 12 entries in crontab as described here: Logrotate - daily log files for a month;
d) use some different utilities / program than logrotate

Related

I am using Sun OS, I want my script to read date from a file(in format %Y%m%d) and add 1 day to that date

I am working in Sun OS environment, I want to add a functionality to my existing unix ksh script where it allows to read a date(in %Y%m%d format) from a file and add 1 day and rewrite the same into that file. [please note: not adding day to current date instead i want to add 1 day to i/p date present in a file].
Eg:DateFile.dat
20200620
I want my script to change it to 20200621 at the end of run.
My code as below:
#!/bin/ksh
ip_dte</home/{file_Path}
echo $ip_dte
dte_add=`TZ=AEST-24 "$ip_dte"`
echo $dte_add
Something like that ? :
#!/bin/ksh
# Starting date, YYYYMMDD (yes I should verify the format :) )
FIRST_DATE=$1
[[ -z $FIRST_DATE ]] && FIRST_DATE=$(date +"%Y%m%d")
# 1 day in seconds
PERIOD=86400
# Transform the starting date in second and add 1 day
SECOND_DATE=$(( $(date -d "$FIRST_DATE" +"%s")+$PERIOD ))
# Transform the second date from second to human date format
print "Second date is $(date -d #"$SECOND_DATE" +"%Y%m%d")"
We can let date do the calculations and formatting for us:
$ date1='20200415'
$ date -d "${date1}+1 day" # add 1 day, use default output format
Thu Apr 16 00:00:00 UTC 2020
$date -d "${date1}+1 day" '+%Y%m%d' # add 1 day, change to YYYYMMDD format
20200416
$ date2=$(date -d "${date1}+1 day" '+%Y%m%d') # save new date in variable in YYYYMMDD format
$ echo "${date2}"
20200416
Here's a ksh fiddle of the above.
The other answers will work if you have ‎CSWcoreutils or SUNWgnu-coreutils installed. You may have to run gdate or /usr/gnu/bin/date.
But if you have a recent version of Solaris, ksh will be ksh93, and you can use the %T format in printf:
$ cat ddd
20200620
$ ip_dte=`cat ddd`
$ printf "%(%Y%m%d)T\n" "$ip_dte tomorrow"
20200621
If you have perl with the Time::Local module on the old Solaris server, try this:
#!/bin/ksh
ip_dte=20200531 # or read date from file into ip_dte
echo $ip_dte
timestamp=`perl -MTime::Local=timelocal -e '($y,$m,$d) = $ARGV[0] =~ /(\d\d\d\d)(\d\d)(\d\d)/; $m=$m-1; print timelocal(1,1,1,$d,$m,$y);' $ip_dte`
dte_add=`perl -MPOSIX=strftime -e 'print strftime("%Y%m%d", localtime($ARGV[0] +86400));' $timestamp`
echo $dte_add

Logrotate only for removing old files?

I've a cron will generate daily file with format like data.log.YYYYMMDD and I want to use logrotate only to delete those file older than 5 days.
I tried this but not work. Any idea? Thanks.
/log/data.log.* {
daily
missingok
rotate 0
maxage 5
}
It's not (easily) feasible... take a look at these posts:
logrotate-to-clean-up-date-stamped-files
logrotate-files-with-date-in-the-file-name
The most easy way to do that is just to make a cron task: see this example, basically something like:
$ /usr/bin/find /data/tier2/scripts/logs/ -mtime +7 -name "*.log" -print -exec /bin/rm {} \;

How to get files which are created one hour ago in solaris 5.9

I want to get files which are created one hour ago, i tried following command
/usr/bin/find /home/FILES/ -name '*.xml' -atime +.0417 -exec ls -l{} \;
In the above command .0417 is (1 day /24 hours ).
The find command which i am using does not have -mmin option.
Is there a way to get files created less one hour ago.
set the file time to 1 hours ago
touch -t 201410042236 /tmp/hourold.tmp;
/usr/bin/find /home/FILES/ -name '*.xml' /tmp/hourold -type f -exec ls -l {} \;

OS X terminal copy and rename one file with backward sequential day 60 times

I'm looking for a script to copy and rename one single file in a folder 60 times with backward sequential day in OS X
For example diary.list becomes
20140320_diary.list
20140319_diary.list
20140318_diary.list
20140317_diary.list
20140316_diary.list
....
20140120_diary.list
I know this simple script to copy and rename with previous date to it
cp diary.list $(date -v -1d '+%Y%m%d')_diary.list
But how do I put -1d in a loop so that it repeats 60 times?
Thanks heaps!
for i in `seq 1 60`; do cp -v diary.list $(date -v -`echo $i`d '+%Y%m%d')_diary.list ; done
Omit -v from the cp command to quiet output.

Calculating number of days given date in UNIX

I have tried to calculate number of days from January 1st to given date in same year.
Option -d for UNIX command isn't working
date -d
date: illegal option -- d
Usage: date [-u] [+format]
date [-u] [mmddhhmm[[cc]yy]]
date [-a [-]sss.fff]
I'm using this script but is too long.
Is there a simple way to calculate a nuber of days?
EDIT
Result of a script:
$ ksh datecalc -a 2013 2 5 - 2013 1 1
$ 35
OK so this may be a bit far fetched, but mysql client (or other DB clients) can come in handy for this as they have reliable and well documented date functions.
$ mysql ..... --silent -e "select datediff('2013-02-05', '2013-01-01') from dual;"
35
$
where ..... are your connection options.
If you have Perl installed, you can do:
perl -MPOSIX=strftime -le 'print strftime("%j",localtime)'
For a specific day, e.g. Feb 5:
perl -MPOSIX=strftime -le '
#d=(2013,2,5);
print strftime("%j",0,0,0,$d[2],$d[1]-1,$d[0]-1900)
'
036

Categories