如何使用clamav只扫描最近24小时的文件

我创build了一个bash脚本来通过clamav扫描整个服务器的病毒。 脚本每天晚上都通过cron运行。 因此,我只想扫描最近24小时添加的文件。 现在我正在脚本中使用这个命令:

find /home -type f -mmin -1440 -print0 | xargs -0 -r clamscan --infected 

但是速度太慢了,查找命令是慢的原因吗? 如果是的话,有什么更好的方式来扫描最后24小时的文件与clamscan? 克拉马夫有做这个select吗?

任何帮助将非常感激。

当我正在寻找一个clamscan脚本时,我偶然发现了这个页面。 我遵循以上的build议,并得到它的工作:

 #!/usr/bin/bash # Create Hourly Cron Job With Clamscan # Directories to scan scan_dir="/home" # Temporary file list_file=$(mktemp -t clamscan.XXXXXX) || exit 1 # Location of log file log_file="/var/log/clamav/hourly_clamscan.log" # Make list of new files if [ -f "$log_file" ] then # use newer files then logfile find "$scan_dir" -type f -cnewer "$log_file" -fprint "$list_file" else # scan last 60 minutes find "$scan_dir" -type f -cmin -60 -fprint "$list_file" fi if [ -s "$list_file" ] then # Scan files and remove (--remove) infected clamscan -i -f "$list_file" --remove=yes > "$log_file" # If there were infected files detected, send email alert if [ `cat $log_file | grep Infected | grep -v 0 | wc -l` != 0 ] then HOSTNAME=`hostname` echo "$(egrep "FOUND" $log_file)" | mail -s "VIRUS PROBLEM on $HOSTNAME" -r [email protected] [email protected] fi else # remove the empty file, contains no info rm -f "$list_file" fi exit 

这是一个小时脚本在我的情况,但应该每天工作(修改第二次查找)。

根据实际上受到影响的文件数量,我不认为2小时的病毒扫描时间太长。 无论如何,你可以尝试通过以下方式提高速度:

find结果输出到文件中,而不是将其input到xargs ,然后使用clamscan--file-list=FILE选项。 这可能会改善运行时间,因为clamav只需要启动并初始化一次而不是多次。 请留下评论,并告诉我,如果有的话,这个速度有多快。

另一种select(或额外的一种)将限制您的扫描某些易受攻击的文件types,但个人而言,我不喜欢这种方法。

我还没有testing过,但我正在计划将我的clamscan运行与我的备份运行进行整合。 我的备份工具会生成一个修改后的文件列表,以便执行增量备份,那么为什么要重新计算两次相同的文件列表呢?

我使用dirvish来创build我的备份,它使用下面的rsync。 最后,我得到一个log.bz2给我一个备份所有文件的报告,包括备份的文件列表。

这个genclamfilelist.sh脚本将从最新备份的log.bz2中提取文件列表并打印出来:

 #!/bin/sh AWK=/usr/bin/awk BUNZIP2=/bin/bunzip2 HEAD=/usr/bin/head HOSTNAME=/bin/hostname LS=/bin/ls SED=/bin/sed SNAPSHOT_HOME=/path/to/dirvish/snapshots for vaultHome in ${SNAPSHOT_HOME}/*; do # vault naming convention: <hostname>-<sharename> vaultName="`echo ${vaultHome} | ${SED} -e 's/^.*\/\([^\/]\+\)$/\1/'`" vaultHost="`echo ${vaultName} | ${SED} -e 's/\([^\-]\+\)\-.*$/\1/'`" # only proceed if vault being considered is for the same host if [ "${vaultHost}" = "`${HOSTNAME}`" ]; then logfile="`${LS} -1t ${vaultHome}/20??????-???? \ | ${HEAD} -1 \ | ${SED} -e 's/^\(.*\)\:$/\1/'`/log.bz2" if [ -f ${logfile} ]; then ${BUNZIP2} -c ${logfile} | ${AWK} ' /^$/ { if (start) { start=0 } } { if (start) { print $0 } } /^receiving\ file\ list\ \.\.\.\ done$/ { start=1 }' | ${SED} -e "s/^\(.*\)$/\/\1/" fi # else skip - no log file found, probably backup didn't run or failed fi # else skip - another vault done exit 0 

这个/etc/cron.d/clamav cron脚本将使用文件列表:

 # /etc/cron.d/clamav: crontab fragment for clamav CLAMAV_FILELIST=/tmp/clamav_filelist_`/bin/hostname`.txt # run every night 0 19 * * * root /usr/bin/test -f ${CLAMAV_FILELIST} && /usr/bin/clamscan --any-desired-options --file-list=${CLAMAV_FILELIST} && /bin/rm ${CLAMAV_FILELIST} 

由于我使用dirvish,我修改了/etc/dirvish/dirvish-cronjob来调用第一个脚本来生成供上一个脚本使用的文件列表:

 # ... /usr/sbin/dirvish-expire --quiet && /usr/sbin/dirvish-runall --quiet rc=$? # v--- BEGIN ADDING NEW LINES touch /tmp/clamav_filelist_`hostname`.txt chmod 400 /tmp/clamav_filelist_`hostname`.txt /usr/local/bin/genclamfilelist.sh >> /tmp/clamav_filelist_`hostname`.txt # ^--- END ADDING NEW LINES umount /mnt/backup0 || rc=$? # ...