Nginx log segmentation

Source: Internet
Author: User
The core of this script is: compress the log file every morning and move it to the Directory of the corresponding year and month, then regenerate the log file, and record the log #! /Bin/bashlogs_path & quot;/home/wwwlog/& quot; # log storage location year 'date + % Y' # year month 'Dat...

The core of the following script is:

Compress the log file every morning and move it to the corresponding year/month Directory. then, regenerate the log file and record the log.

 

#! /Bin/bash

Logs_path = "/home/wwwlog/" # log storage location
Year = 'date + % Y' # year
Month = 'date + % M' # month
Day = 'date + % Y % m % d' # date
Time = 'date + % Y % m % d-% H: % M: % s' # time
Weblog = "www.pcstars.com _ access" # web access log
CutNginxLog = "/home/sysadmin/zhaoyj/CutNginx. Log" # Split nginx Log records

If [$ day-eq 1]; then # determines if it is the first day of the month. if the condition is true, create the directory/home/wwwlog/year/month.
Mkdir-p $ {logs_path} $ year/$ month/
Fi

Cd $ logs_path # enter the current directory
Tar jcf access_day.log.tar.bz2 access. log # compress the log file
Echo-n> access. log # clear the file and record the log
DuAccess = 'Du-sh access_day.log.tar.bz2 | awk '{print $1} ''# count the tar size to record logs.
Mv access_1_day.log.tar.bz2 $ {logs_path} $ year/$ month/# Move the log file to the corresponding directory
Echo "access_$day.log.tar.bz2: $ DuAccess $ time" >>$ CutNginxLog # Record logs

Tar jcf ${weblog1__day.tar.bz2 $ weblog. log
Echo-n> $ weblog. log
DuPcstars = 'Du-sh zookeeper weblog=_day.tar.bz2 | awk '{print $1 }''
Mv invalid weblog1__day.tar.bz2 $ {logs_path} $ year/$ month/
Echo "commandid weblog1__day.tar.bz2: $ DuPcstars $ time" >>$ CutNginxLog
DuPcstars = 'Du-sh zookeeper weblog=_day.tar.bz2 | awk '{print $1 }''
Mv invalid weblog1__day.tar.bz2 $ {logs_path} $ year/$ month/
Echo "commandid weblog1__day.tar.bz2: $ DuPcstars $ time" >>$ CutNginxLog
Echo ""> $ CutNginxLog
Echo ""> $ CutNginxLog

Author's "ANLJF column"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.