gzip 在脚本中的 gz 后缀已未更改



我创建了一个脚本来压缩日志文件并将其从一个目录移动到另一个目录以释放空间。这是脚本:

#!/bin/bash
logsDirectory="/test//logs/" 
email="" 
backupDirectory="/test/backup" 
pid="/data/test/scripts/backup.pid"
usage=$(df | grep /data/logs | awk '{ print $2 }') 
space=450000000
getBackup () 
{ 
if [[ ! -e $pid ]] then
            if [[ $usage -le $space ]]
                    then
                    touch $pid
                    find $backupDirectory -mtime +15 -type f  -delete;
                    for i in $(find $logsDirectory -type f -not -path "*/irws/*")
                            do
                                    /sbin/fuser $i > /dev/null 2>&1
                                    if [ $? -ne 0 ]
                                    then
                                            gzip  $i
                                            mv -v $i.gz $backupDirectory
                                    else
                                            continue
                                    fi
                            done
                    [[ ! -z $email ]] && echo "Backup is ready" | mas"Backup" $email
                    rm -f $pid
            fi 
fi 
} 
getBackup

我收到此错误:

gzip: /data/logs/log01.log.gz already has .gz suffix -- unchanged
mv: cannot stat `/data/logs/log01.log.gz': No such file or directory

每次在 DEV 和 PROD(CentOS 服务器)环境中运行脚本时,我都会收到错误。为了分析它,我在笔记本电脑的虚拟机(Ubuntu)中运行了相同的脚本,并且在那里没有收到错误。

我的问题:

  1. 如何防止此错误?
  2. 我在剧本中做错了什么?

您的脚本包含许多常见的笨拙或低效的反模式。 这是一个重构。 唯一真正的更改是跳过任何*.gz文件。

#!/bin/bash
logsDirectory="/test//logs/" 
email="" 
backupDirectory="/test/backup" 
pid="/data/test/scripts/backup.pid"
# Avoid useless use of grep -- awk knows how to match a regex
# Better still run df /data/logs
usage=$(df /data/logs/ | awk '{ print $2 }') 
space=450000000
getBackup () 
{
    # Quote variables
    if [[ ! -e "$pid" ]]; then
        if [[ "$usage" -le "$space" ]]; then
            touch "$pid"
            find "$backupDirectory" -mtime +15 -type f  -delete;
            # Exclude *.gz files
            # This is still not robust against file names with spaces or wildcards in their names
            for i in $(find "$logsDirectory" -type f -not -path "*/irws/*" -not -name '*.gz')
            do
                # Avoid useless use of $?
                if /sbin/fuser "$i" > /dev/null 2>&1
                then
                        gzip  "$i"
                        mv -v "$i.gz" "$backupDirectory"
                # no need for do-nothing else
                fi
            done
            [[ ! -z "$email" ]] &&
            echo "Backup is ready" | mas"Backup" "$email"
            rm -f "$pid"
        fi
    fi
}
getBackup

通过稍微更具侵入性的重构,对find循环的正确修复可能看起来像

            find "$logsDirectory" -type f 
              -not -path "*/irws/*" -not -name '*.gz' 
              -exec sh -c '
                for i; do
                    if /sbin/fuser "$i" > /dev/null 2>&1
                    then
                        gzip  "$i"
                        mv -v "$i.gz" "$backupDirectory"
                    fi
                done' _ {} +

秘诀是让find ... -exec +将参数传递给sh -c脚本,其方式根本不涉及将参数暴露给当前 shell。

我在剧本中做错了什么?

您的脚本尝试压缩每个文件,但 gzip 命令拒绝已压缩的文件

如何防止此错误?

让脚本检查文件是否已压缩,并且仅在 gzip 对应时检查 (1)。或者,即使它已经压缩,您也可以强制重新压缩 (2)。

使用选项 1):

getBackup () 
{ 
  if [[ ! -e $pid ]] then
    if [[ $usage -le $space ]]
    then
      touch $pid
      find $backupDirectory -mtime +15 -type f  -delete;
      for i in $(find $logsDirectory -type f -not -path "*/irws/*")
      do
        /sbin/fuser $i > /dev/null 2>&1
        if [ $? -ne 0 ]
        then
          if [[ $i =~ .gz$ ]]
            # File is already zipped
            mv -v $i $backupDirectory
          else
            gzip  $i
            mv -v $i.gz $backupDirectory
          fi
        else
          continue
        fi
      done
      [[ ! -z $email ]] && echo "Backup is ready" | mas"Backup" $email
      rm -f $pid
    fi 
  fi 
} 

最新更新