Skip to content Skip to sidebar Skip to footer

How to Manage Logs Linux Daily Weekly Monthly so Neat on Web Server

I make logs that are stored on the server so that the location is neat. in the future when there is an issue, you can easily retrieve the log.

Case

  1. Application = /apps/logs/*.log
  2. Nginx Log =  /var/logs/nginx/*.log
  3. Zend Log = /usr/local/zend/var/log/*.log

Preparation

Backup logrotate.conf

# cp /etc/logrotate.conf /etc/logrotate.conf.ori

Make Directoy Location Log

# mkdir /apps/data/KitsakeLog/daily/apps
# mkdir /apps/data/KitsakeLog/daily/nginx
# mkdir /apps/data/KitsakeLog/daily/zend
# mkdir -p /apps/data/KitsakeLog/weekly/
# mkdir -p /apps/data/KitsakeLog/monthly/

Change Permission & Owner

# chmod -R 744 /apps/data/KitsakeLog/
# chown -R kitsake.kitsake /apps/data/KitsakeLog/

Configure

Configure in logrotate.conf

# vi /etc/logrotate.conf

.................
include /etc/logrotate.d/daily
.................

save & exit

Make Directory Configure of Custom Log

# cd /etc/logrotate.d/
# mkdir -p daily

Move Existing Configure

# cd /etc/logrotate.d/
# mv nginx daily/

Bashscript Ownership

# mkdir /etc/logrotate.d/scripts
# vi  /etc/logrotate.d/scripts/apps-ownership.sh

#!/bin/bash
chown kitsake:kitsake /apps/data/KitsakeLog/daily/nginx/*
chmod 644 /apps/data/KitsakeLog/daily/nginx/*

save & exit

# vi /etc/logrotate.d/scripts/zend-ownership.sh

#!/bin/bash
chown kitsake:kitsake /apps/data/KitsakeLog/daily/zend/*
chmod 644 /apps/data/KitsakeLog/daily/zend/*
# chmod 755 /etc/logrotate.d/scripts/apps-ownership.sh
# chmod 755 /etc/logrotate.d/scripts/zend-ownership.sh

Daily Logrotate

# vi /etc/logrotate.d/daily/apps

/apps/logs/*.log {
    olddir /apps/data/KitsakeLog/daily/apps
        daily
        rotate 7
    missingok
    create 640 kitsake kitsake
}

save & exit

# vi /etc/logrotate.d/daily/nginx

/apps/logs/nginx/*.log {
    su root root
    olddir /apps/data/KitsakeLog/daily/nginx
        daily
        rotate 7
    missingok
    create 640 nginx zend
    lastaction
        /etc/logrotate.d/scripts/apps-ownership.sh
    endscript
    postrotate
        if [ -f /var/run/nginx.pid ]; then
            kill -USR1 `cat /var/run/nginx.pid`
        fi
    endscript
}

save & exit

# vi /etc/logrotate.d/daily/zend

/usr/local/zend/var/log/*.log {
    su root root
    olddir /apps/data/KitsakeLog/daily/zend
        daily
        rotate 7
    missingok
    lastaction
        /etc/logrotate.d/scripts/zend-ownership.sh
    endscript
}

save & exit

Weekly Logrotate Save

# mkdir -p /home/scripts
# chown -R kitsake.kitsake /home/scripts
# vi /home/scripts/weekly.sh

#!/bin/bash
tgl=`/bin/date +%Y%m%d`
cd /apps/data/KitsakeLog/daily/
tar -czvf weekly_$tgl.tgz *
mv weekly_$tgl.tgz /apps/data/KitsakeLog/weekly/

find /apps/data/KitsakeLog/daily/apps/ -maxdepth 1 -type f -mtime +7 -exec rm -rf {} \;
find /apps/data/KitsakeLog/daily/nginx/ -maxdepth 1 -type f -mtime +7 -exec rm -rf {} \;
find /apps/data/KitsakeLog/daily/supervisor/ -maxdepth 1 -type f -mtime +7 -exec rm -rf {} \;
find /apps/data/KitsakeLog/daily/zend/ -maxdepth 1 -type f -mtime +7 -exec rm -rf {} \;

save & exit

# chmod 755 /home/scripts/weekly.sh

Monthly Logrotate Save

# vi /home/scripts/monthly.sh

#!/bin/bash
tgl=`/bin/date +%Y%m%d`
cd /apps/data/KitsakeLog/weekly/
tar -czvf monthly_$tgl.tgz *
mv monthly_$tgl.tgz /apps/data/KitsakeLog/monthly/

find /apps/data/KitsakeLog/weekly/ -maxdepth 1 -type d -mtime +4 -exec rm -rf {} \;

save & exit

# chmod 755 /home/scripts/monthly.sh

Make Crontab

# vi /etc/crontab

00 00 */7 * * kitsake /home/scripts/weekly.sh
00 00 */30 * * kitsake /home/scripts/monthly.sh 

Manage Logs Linux Daily Weekly Monthly
Manage Logs Linux Daily Weekly Monthly

Post a Comment for " How to Manage Logs Linux Daily Weekly Monthly so Neat on Web Server"