§logrotate
- See if it installed
alexlai@n2Bookworm:/etc$ /sbin/logrotate --version <-- Debian
$ logrotate <-- else
- Create a logrotate configuration file specifically for MongoDB. You can create a new configuration file, for example, /etc/logrotate.d/mongodb:
# logrotate /var/log/mongoDB-*.log
# /var/log/mongoDB-ys20220317.log {
/var/log/mongoDB-*.log {
daily
rotate 7
compress
delaycompress
missingok
notifempty
create 0640 syslog sudo
sharedscripts
# su: This stands for "switch user.`root:sudo`
su root syslog
# postrotate
# e.g. /usr/bin/killall -SIGUSR1 mongod
#
# after rotation, it seems I have to
# systemctl restart syslog-ng #
# endscript
}
or in n2Bookworm.yushei.com.tw
# logrotate /var/log/mongodb/mongod.log
/var/log/mongodb/mongod.log {
daily
rotate 7
compress
delaycompress
missingok
notifempty
create 0640 syslog sudo
sharedscripts
# su: This stands for "switch user.`root:sudo`
su root syslog
# postrotate
# e.g. /usr/bin/killall -SIGUSR1 mongod
#
}
- daily: Rotates the log file daily.
- rotate 7: Keeps up to 7 rotated log files.
- compress: Compresses rotated log files.
- delaycompress: Postpones compression of the previous log file until the next rotation cycle.
- missingok: Ignores missing log files.
- notifempty: Doesn't rotate the log file if it's empty.
- create: Specifies the permissions and ownership for the newly created log files.
- sharedscripts: Runs the postrotate script only once for all matching log files.
- ?? postrotate: Executes the specified command after rotation. In this case, it sends the SIGUSR1 signal to the mongod process to reopen log files.
- Adjust permissions: Ensure that the user/group specified in the create directive (in this example, mongodb) has write permissions to the log directory.
3. testing `sudo logrotate -d`
alexlai@n2Bookworm:/etc$ sudo logrotate -d /etc/logrotate.d/mongodb warning: logrotate in debug mode does nothing except printing debug messages! Consider using verbose mode (-v) instead if this is not what you want.
reading config file /etc/logrotate.d/mongodb error: /etc/logrotate.d/mongodb:9 unknown user 'syslog' error: found error in /var/log/mongodb/mongod.log , skipping removing last 1 log configs Reading state from file: /var/lib/logrotate/status Allocating hash table for state file, size 64 entries Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state
Handling 0 logs alexlai@n2Bookworm:/etc$ ls -l /var/log/mongodb/ total 880 -rw------- 1 mongodb mongodb 894025 May 29 09:24 mongod.log