linux logrotate, rotate file with underscore - linux

I have a logrotate configuration which is given below.
/var/log/Test.log {
size 50k
missingok
rotate 10
notifempty
create 0640 root root
}
The file is rotating successfully once it reached the size of 50kb in the below format
Test.log.1
Test.log.2
Test.log.3
What I can do if I want to rotate files in the below format
Test_1.log
Test_2.log
Test_3.log

Try this one,it should help you.
/var/log/Test.log {
...
postrotate
/usr/bin/mv Test.log.1 Test_1.log
...
endscript
}

OK, so the problem here is that logrotate uses these filename extensions to keep track of its rotations. So if you have x.log, it inherently needs a predictable, sequential extension to keep track of what is what; i.e. x.log.1 is the first rotation, x.log.2 is the second, etc.
By renaming the file like that, you're moving the extension to the middle of the file and subsequently leaving each file with the same extension: .log. So as far as logrotate is concerned, it will think those are all unrelated files, not rotations of a previous log.
E.g., if you rename Test.log.1 to Test_1.log, and leave that file in the same folder, logrotate won't know that it's a rotation of Test.log and create a new Test.log.1 upon next rotation. And so on, and so on.
So the short answer is no, there is no logrotate directive that will rename a file like you want. You will have to create this functionality yourself using a postrotate script, and you will also have to (at some point) move these files out of the var/log/ directory once they're renamed, using either said postrotate script or the olddir directive.
The dateext directive might also be useful, as in my opinion date extensions are typically more user-friendly than numerical ones.

Related

how to logrotate(8) tell the process that inode has changed?

say biz.log:
I read the logrotate(8) defaults to (1) rename the old log file with the date, biz.log.20220129 and then (2) create a new file with the origin name, and finally
(3) change the process's log output to the new file inode.
My question is the fact that the biz-process uses the inode instead of the file makes step1, and step2 reasonable. But how does the (3) work? I am not sure how logrotate can tell the process the log file should be reopened, and do I have to add some code to handle this in my project?
It requires cooperation between logrotate and application. Logrotate must be configured through postrotate scripts, to emit signal (i.e. SIGUSR), which application must handle - by reopening log files by their name. Actually one may also use different interprocess communication method, but i believe signals are common. See for example discussion on logrotate for httpd configuration (https://serverfault.com/questions/262868/logrotate-configuration-for-httpd-centos).
If there is no option to modify application, to work with signals for file rotation, one may configure logrotate to use copytruncate option - where logrotate copy current file to backup file, and then truncates current file to 0 bytes. If file is opened by application in append mode, it should be seamless. See https://man7.org/linux/man-pages/man8/logrotate.8.html copytruncate section.

Log rotation in logstash

I am using file as input for logs in logstash . My log files are rotated daily so , I wanted to ask how can we configure file plugin of logstash so that it work with the files that are rotated daily. And adding to this, is log rotation available with file beat as well.
I am trying to answer your questions in part.
First - log rotation.
From the docs:
Note that the rotated filename will be treated as a new file so if
start_position is set to beginning the rotated file will be
reprocessed.
https://www.elastic.co/guide/en/logstash/current/plugins-inputs-file.html
That means, that if you have a rename in your file rotation, you will likely double your file (unless the path excludes the renamed file I believe).
If your path excludes your renamed file, then it should be fine.
I fixed this in a different way (in java and python accordingly).
I disable renaming of files and instead name the log file with the date prefix. So for me, in my java app, the file name is:
my-server-log-%h-%d.log
Since I am working in a distributed environment, I incorporate the hostname into my logfile name.
%h = hostname
%d = date
This ends up in my file being named:
my-server-log-pandaadb-2016-06-20.log
This file is never renamed. I modified my rotation algorithm to simply not rename and instead at midnight create a new file and leave the previous file untouched.
This has the effect that logstash (correctly) knows that it has read all lines in the previous file. It picks up the new file since I am using wildcards in my input. No logs are duplicated.
This also works quite well in combination with rsync by the way.
I hope that helps,
Artur
Edit: I have not worked with filebeat so far, so I can't comment on that part.

logrotate - backup any file

I have one file. It's not a log file. Every day I need to copy it somewhere else adding timestamp to its name. I need to keep last ten (10) days copies.
I am considering using logrotate service (server is running CentOS).
My question is if there is some limit on logrotate to rotate only log files or if I can use any other file and location. That is may I specify some other location where to put copies with timestamps and to hold only last 10 (days) copies?
Thanks in advance for your hints.
You can rotate any file in any location e.g. take a look at /etc/logrotate.d/samba (if installed) which rotates log.smbd. Just create a new file in /etc/logrotate.d/ and configure for your file to be rotated. A nice description is here for centOS not different (here).
The problem is the timestamp in the name. If you want this it is no longer rotating. With logrotate the latest archived file is always number 1 and thus all older once are getting renamed. But they keep their time of last modification stored in the file system untouched. But this time is not in the name.
As an alternative you can just create a new cron job by adding a file in one of the \etc\cron.*\ directories. This cron job can move you file adding a timestamp e.g. by using date +%y%m%d_%H:%M and create a new file using touch. No need for logrotate.

shell script to create backup file when creating new file in particular directory

Recently I was asked the following question in an interview.
Suppose I try to create a new file named myfile.txt in the /home/pavan directory.
It should automatically create myfileCopy.txt in the same directory.
A.txt then it automatically creates ACopy.txt,
B.txt then BCopy.txt in the same directory.
How can this be done using a script? I may know that this script should run in crontab.
Please don't use inotify-tools.
Can you explain why you want to do?
Tools like VIM can create a backup copy of a file you're working on automatically. Other tools like Dropbox (which works on Linux, Windows, and Mac) can version files, so it backs up all the copies of the file for the last 30 days.
You could do something by creating aliases to the tools you use for creating these file. You edit a file with the tools you tend to use, and the alias could create a copy before invoking a tool.
Otherwise, your choice is to use crontab to occasionally make backups.
Addendum
let me explain suppose i have directory /home/pavan now i create the file myfile.txt in that directory , immediately now i should automatically generate myfileCopy.txt file in the same folder
paven
There's no easy user tool that could do that. In fact, the way you stated it, it's not clear exactly what you want to do and why. Backups are done for two reasons:
To save an older version of the file in case I need to undo recent changes. In your scenario, I'm simply saving a new unchanged file.
To save a file in case of disaster. I want that file to be located elsewhere: On a different computer, maybe in a different physical location, or at least not on the same disk drive as my current file. In your case, you're making the backup in the same directory.
Tools like VIM can be set to automatically backup a file you're editing. This satisfy reason #1 stated above: To get back an older revision of the file. EMACs could create an infinite series of backups.
Tools like Dropbox create a backup of your file in a different location across the aether. This satisfies reason #2 which will keep the file incase of a disaster. Dropbox also versions files you save which also is reason #1.
Version control tools can also do both, if I remember to commit my changes. They store all changes in my file (reason #1) and can store this on a server in a remote location (reason #2).
I was thinking of crontab, but what would I backup? Backup any file that had been modified (reason #1), but that doesn't make too much sense if I'm storing it in the same directory. All I would have are duplicate copies of files. It would make sense to backup the previous version, but how would I get a simple crontab to know this? Do you want to keep the older version of a file, or only the original copy?
The only real way to do this is at the system level with tools that layer over the disk IO calls. For example, at one location, we used Netapps to create a $HOME/.snapshot directory that contained the way your directory looked every minute for an hour, every hour for a day, and every day for a month. If someone deleted a file or messed it up, there was a good chance that the version of the file exists somewhere in the $HOME/.snapshot directory.
On my Mac, I use a combination of Time Machine - which backs up the entire drive every hour, and gives me a snapshot of my drive that stretches back over a year and a half) and Dropbox which keeps my files stored in the main Dropbox server somewhere. I've been saved many times by that combination.
I now understand that this was an interview question. I'm not sure what was the position. Did the questioner want you to come up with a system wide way of implementing this, like a network tech position, or was this one of those brain leaks that someone comes up with at the spur of the moment when they interview someone, but were too drunk the night before to go over what they should really ask the applicant?
Did they want a whole discussion on what backups are for, and why backing up a file immediately upon creation in the same directory is a stupid idea non-optimal solution, or were they attempting to solve an issue that came up, but aren't technical enough to understand the real issue?

log4j fileappender doesn't switch to the new file when logrotate rotates the log file

Context:
I want to use log4j to write audit-related logs to a specific log file, let's say audit.log. I don't want to use syslogappender(udp based) because I don't want to be tolerant to data loss. Plus, I am using logrotate to rotate the audit.log when the file gets to certain size.
Problem:
I am encountering is that, when logrotate rotates the file audit.log to audit.log.1, log4j keeps writing to audit.log.1 other than writing to the audit.log.
Possible approaches:
I know I can use rollingfileappender to do the log rotation other than use logrotate, so when rollingfileappender rolls the file, it switch to the new file without hassles. But the reason I can't use rollingfileappender is that I want to use logrotate's post rotate feature to trigger some scripts after the rotation happens which can't be provided by rollingfileappender.
Another desperate way I can think of is to write a log4j customized appender myself to close the log file(audit.log.1) and open the new one(audit.log) when it detects the file is rotated.
I never used ExternallyRolledFileAppender, but if it's possible to use logrotate post rotate to send the signal to ExternallyRolledFileAppender and make log4j aware the file is rotated, and start writing to the new file?
Question:
Just wondering is there some appender like that already been invented/written? or do I have other options to solve this?
Check out logrotate's copytruncate option, it might help your case:
copytruncate
Truncate the original log file to zero size in place
after creating a copy, instead of moving the old log
file and optionally creating a new one. It can be
used when some program cannot be told to close its
logfile and thus might continue writing (appending) to
the previous log file forever. Note that there is a
very small time slice between copying the file and
truncating it, so some logging data might be lost.
When this option is used, the create option will have
no effect, as the old log file stays in place

Resources