简体   繁体   English

用于归档日志文件的脚本

[英]Script for archiving log files

i have created a Bash script for archiving log files: 我创建了一个用于归档日志文件的Bash脚本:

#!/bin/bash

# Pour chaque dossiers "log" trouvé.
for folder in $(find . -name log -type d )
do :
    # Pour chaque dossier log contenant des fichiers ".log" vieux de +30jours.
    for file in $(find $folder -name "*.log" -mtime +30)
    do :
            # Archiver les fichiers ".log".
        tar czf archive-log.tar.gz $folder/*.log
    done

    # Si une archive existe.
    if [ -e $folder/*.tar.gz ]
         # Déplacer l'archive.
         then mv $folder/*.tar.gz $ARCHIVE
    fi

done

the output i experience is: 我经历的输出是:

[logs]$ ll
total 8
drwxr-xr-x 2 webadm webgrp 4096 sep 17 14:26 log_weblogic
-rwxr--r-- 1 webadm webgrp  456 sep 17 14:31 scriptArchivesLog

[log_weblogic]$ ll
total 200
-rw-r----- 1 webadm webgrp 98005 mai 16 04:04 test.log
-rw-r----- 1 webadm webgrp 98005 sep 13 15:29 WLS-CONSOLE-DOMAINE-PUB.log


[logs]$ ll
total 32
-rw-r--r-- 1 webadm webgrp 21734 sep 17 14:31 archive-log.tar.gz
drwxr-xr-x 2 webadm webgrp  4096 sep 17 14:26 log_weblogic
-rwxr--r-- 1 webadm webgrp   456 sep 17 14:31 scriptArchivesLog

When i execute my script, why do i have all files in my archive? 当我执行脚本时,为什么我的存档中有所有文件? I want only files that match mtime +30 我只想要与mtime +30匹配的文件

[logs]$ tar tvzf archive-log.tar.gz
-rw-r----- webadm/webgrp 98005 2013-05-16 04:04:00 ./log_weblogic/test.log
-rw-r----- webadm/webgrp 98005 2013-09-13 15:29:03 ./log_weblogic/WLS-CONSOLE-DOMAINE-PUB.log

You have made the critical error of not checking to see if there is already a program or library around that already does what you want. 您犯了一个严重的错误,即不检查周围是否已有某个程序或库已经在执行您想要的操作。 In this case, there is logrotate which is probably already present on your system, diligently cleaning up the system logfiles in /var/log. 在这种情况下,您的系统上可能已经存在logrotate,它会认真清理/ var / log中的系统日志文件。 As a bonus, it will also already be configured to run periodically, so you won't even have to remember to set up a cron job. 另外,它也已经配置为定期运行,因此您甚至不必记住设置cron作业。

There is a tutorial on using logrotate at https://www.linode.com/docs/uptime/logs/use-logrotate-to-manage-log-files https://www.linode.com/docs/uptime/logs/use-logrotate-to-manage-log-files上有一个使用logrotate的教程。

Replace the following: 替换以下内容:

for file in $(find $folder -name "*.log" -mtime +30)
do :
        # Archiver les fichiers ".log".
    tar czf archive-log.tar.gz $folder/*.log
done

with

tar czf archive-log.tar.gz $(find $folder -name "*.log" -mtime +30)

It is because of your tar command: 这是因为您的tar命令:

tar czf archive-log.tar.gz $folder/*.log

Which is actually archiving all the *.log files irrespective of the timestamp on those files. 实际上,这是在归档所有*.log文件,而不考虑这些文件上的时间戳。

gnu-tar has a switch: gnu-tar有一个开关:

--newer-mtime=date

For this use-case. 对于此用例。

Try this: 尝试这个:

for folder in $(find . -name log -type d ) # Pour chaque dossiers "log" trouvé.
do :
    for file in $(find $folder -name "*.log" -mtime +30) # Pour chaque dossier log contenant des fichiers ".log" vieux de +30jours.
    do :
        tar czf archive-log.tar.gz $folder/*.log && rm -f $folder/*.log  # Archiver les fichiers ".log". and remove the copy
    done

    if [ -e $folder/*.tar.gz ] # Si une archive existe.
         then mv $folder/*.tar.gz $ARCHIVE # Déplacer l'archive.
    fi

done

This is the wrong part: 这是错误的部分:

for file in $(find $folder -name "*.log" -mtime +30) # Pour chaque dossier log contenant des fichiers ".log" vieux de +30jours.
do :
    tar czf archive-log.tar.gz $folder/*.log # Archiver les fichiers ".log".
done

Despite searching for files, tar czf archive-log.tar.gz $folder/*.log would still archive all those files. 尽管搜索文件,但tar czf archive-log.tar.gz $folder/*.log仍将归档所有这些文件。

You can change that to something like this instead: 您可以改为如下所示:

readarray -t files < <(exec find $folder -name "*.log" -mtime +30) ## read those files (list) to array
[[ ${#files[@]} -gt 0 ]] && tar czf archive-log.tar.gz "${files[@]}"  ## create an archive if a file was found.

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

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