简体   繁体   English

不工作脚本,为nginx的每个日志文件制作gzip

[英]Does not work script, that makes gzip for each log file of nginx

Please, don't tell me about log rotation in unix, and other tools. 请不要告诉我有关unix和其他工具中的日志轮换的信息。 I need this script. 我需要这个脚本。 When, i am trying to do this: 什么时候,我想这样做:

find /root/nginx.log* -type f -print0 -and ! -name "*.gz" -and ! -name "nginx.log.2017.05.08" -exec gzip "{}" \;

Everything is ok, i have this result, that i need: 一切都好,我有这个结果,我需要:

-rw-r--r-- 1 root root  1381791 May  8 10:33 nginx.log.2017.05.07.gz
-rw-r--r-- 1 root root 17942221 May  8 10:33 nginx.log.2017.05.08
-rw-r--r-- 1 root root  1381791 May  8 10:33 nginx.log.2017.05.09.gz
-rw-r--r-- 1 root root  1381791 May  8 10:33 nginx.log.2017.05.10.gz
-rw-r--r-- 1 root root  1381791 May  8 10:33 nginx.log.2017.05.11.gz
-rw-r--r-- 1 root root  1381791 May  8 10:33 nginx.log.2017.05.12.gz

When i am trying to write script: 当我尝试编写脚本时:

#!/bin/bash

name="nginx"
data=$(date +%Y.%m.%d)
current_nginx=$name.$data
echo $current_nginx

find /root/nginx.log* -type f -print0 -and ! -name "*.gz" -and ! -name "$current_nginx" -exec gzip "{}" \;

After executing of this script, i have: 执行此脚本后,我有:

-rw-r--r-- 1 root root 1379440 May  8 10:35 nginx.log.2017.05.07.gz
-rw-r--r-- 1 root root 1379440 May  8 10:35 nginx.log.2017.05.08.gz
-rw-r--r-- 1 root root 1379440 May  8 10:35 nginx.log.2017.05.09.gz
-rw-r--r-- 1 root root 1379440 May  8 10:35 nginx.log.2017.05.10.gz
-rw-r--r-- 1 root root 1379440 May  8 10:35 nginx.log.2017.05.11.gz
-rw-r--r-- 1 root root 1379440 May  8 10:35 nginx.log.2017.05.12.gz

Debug of script: 脚本调试:

bash -x test.sh
+ name=nginx
++ date +%Y.%m.%d
+ data=2017.05.08
+ current_nginx=nginx.2017.05.08
+ echo nginx.2017.05.08
nginx.2017.05.08
+ find /root/nginx.log.2017.05.07.gz /root/nginx.log.2017.05.08.gz /root/nginx.log.2017.05.09.gz /root/nginx.log.2017.05.10.gz /root/nginx.log.2017.05.11.gz /root/nginx.log.2017.05.12.gz /root/nginx.log.2017.05.13.gz /root/nginx.log.2017.05.14.gz -type f -print0 -and '!' -name '*.gz' -and '!' -name nginx.2017.05.08 -exec gzip '{}' ';'
/root/nginx.log.2017.05.07.gz/root/nginx.log.2017.05.08.gz/root/nginx.log.2017.05.09.gz/root/nginx.log.2017.05.10.gz/root/nginx.log.2017.05.11.gz/root/nginx.log.2017.05.12.gz/root/nginx.log.2017.05.13.gz/root/nginx.log.2017.05.14.gz

My condition -and ! -name "$current_nginx" 我的条件-and ! -name "$current_nginx" -and ! -name "$current_nginx" does not work. -and ! -name "$current_nginx"不起作用。 Where i have mistake ? 哪里有错误? Please help. 请帮忙。

你错过了current_nginx中的.log

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM