Let's take a look at the mysql backup script.
The code is as follows: |
Copy code |
Mysql_back.sh #! /Bin/sh # Mysql database is backed up into SQL files and compressed # Delete the backup created seven days ago. Backupdir =/opt/cpfxs/mysqlbak Mysqlpath =/usr/local/mysql/bin/ Time = 'date + % Y % m % d' $ Mysqlpath/mysqldump-h127.0.0.1-uroot-pluobo mydb | gzip> $ backupdir/mydb1_time. SQL .gz # Delete a backup that exceeds 7 days Find $ backupdir-name "mydb *. SQL .gz"-type f-mtime + 7-exec rm {};>/dev/null 2> & 1 |
The above steps and methods are very simple. The following logs are separated
The code is as follows: |
Copy code |
Cut_nginx_log _. sh #! /Bin/bash # Cut nginx log files, # Name it from yesterday's date Logs_path = "/var/www/log" Date = $ (date-d "yesterday" + "% Y-% m-% d ") # Rename a log file Mv $ {logs_path}/access. log $ {logs_path}/access _ $ date. log # Notify nginx to re-write the log file # Without this step, nginx will continue to write content to the new file name Kill-USR1 'cat/var/nginx. Pi' |
The above is just a simple log operation. Let's take a look at a more detailed nginx log segmentation function.
1. Log cutting
The shell script is as follows:
The code is as follows: |
Copy code |
#! /Bin/bash Logs_path = "/nginx/logs /" Mv $ {logs_path} access. log $ {logs_path} access _ $ (date-d "yesterday" + "% Y % m % d"). log Kill-USR1 'cat/nginx/logs/nginx. Pi' |
"/Nginx/logs" indicates the directory where the nginx log file is located. The generated log file is named after the date of yesterday.
To achieve daily automatic segmentation, add the following parts to the crontab:
1 0 *** sh/home/zyf/sh/cut_nginx_log.sh
In this way, the nginx log is renamed to the date format at 00:01 every day, and the new log file of today is regenerated.
II. Awstats configuration
After the log files are split, the next step is the analysis, that is, the use of the tool software Awstats.
The Awstats configuration file is stored in the/etc/awstats/directory by default, including the domain name you set during installation, such as awstats.www.xxxxke.com. conf.
Modify this location in this configuration file:
The code is as follows: |
Copy code |
LogFile = "/nginx/logs/access _ % YYYY-24 % MM-24 % DD-24.log"
|
This means to read nginx yesterday's log file, about the following % YYYY-24 % MM-24 % DD-24 settings, the rules are as follows:
# You can also use tags in this filename if you need a dynamic file name
# Depending on date or time (Replacement is made by AWStats at the beginning
# Of its execution). This is available tags:
# % YYYY-n is replaced with 4 digits year we were n hours ago
# % YY-n is replaced with 2 digits year we were n hours ago
# % MM-n is replaced with 2 digits month we were n hours ago
# % MO-n is replaced with 3 letters month we were n hours ago
# % DD-n is replaced with day we were n hours ago
# % HH-n is replaced with hour we were n hours ago
# % NS-n is replaced with number of seconds at since 1970
# % WM-n is replaced with the week number in month (1-5)
# % Wm-n is replaced with the week number in month (0-4)
# % WY-n is replaced with the week number in year (01-52)
# % Wy-n is replaced with the week number in year (00-51)
# % DW-n is replaced with the day number in week (1-7, 1 = Sunday)
# Use n = 24 if you need (1-7, 1 = Monday)
# % Dw-n is replaced with the day number in week (0-6, 0 = Sunday)
# Use n = 24 if you need (0-6, 0 = Monday)
# Use 0 for n if you need current year, month, day, hour
3. Start analysis and generate results
Finally, you can perform the analysis.
Run the following command:
The code is as follows: |
Copy code |
/Usr/local/awstats/wwwroot/cgi-bin/awstats. pl-update-config = www.111cn.net |
This command generates the result to the awstatsXXXX.www.XXXX.com.txt file in the/var/lib/awstats directory.
Of course this looks inconvenient. You can use the following command to generate an html page, which is more intuitive.
The code is as follows: |
Copy code |
Perl/usr/local/awstats/tools/awstats_buildstaticpages.pl-update -Config = www.xxxxoke.com-lang = cn -Dir =/html/awstats -Awstatsprog =/usr/local/awstats/wwwroot/cgi-bin/awstats. pl |
This will generate a very intuitive analysis result page under the/html/awstats directory.
IV. Automation
It would be annoying to run several commands on the server every day. Thanks to crontab in the Linux world,
The following is a crontab from a netizen.
The code is as follows: |
Copy code |
1 0 *** sh/home/zyf/sh/cut_nginx_log.sh 0 1 ***/usr/local/awstats/wwwroot/cgi-bin/awstats. pl-update-config = www.xxxxke.com 0 2 *** perl/usr/local/awstats/tools/awstats_buildstaticpages.pl-update-config = www.xxxxke.com-lang = cn-dir =/html/awstats-awstatsprog =/usr/local /awstats/wwwroot/cgi-bin/awstats. pl LogFile = "/usr/local/nginx/logs/% YYYY-24/% MM-24/expotia2.access _ % YYYY-24 % MM-24 % DD-24.log" Log_format main '$ remote_addr-$ remote_user [$ time_local] "$ request "' '$ Status $ body_bytes_sent "$ http_referer "' '"$ Http_user_agent" "$ http_x_forwarded_for "'; |