gzip已经在脚本中保持gz后缀不变

问题描述 投票:0回答:2

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

#!/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服务器)环境中运行脚本时都会出错。为了分析它,我在笔记本电脑的VM(Ubuntu)中运行了相同的脚本,我没有在那里得到错误。

我的问题:

  1. 我该如何防止此错误?
  2. 我在脚本中做错了什么?
linux bash scripting gzip
2个回答
0
投票

您的脚本包含许多常见的笨拙或低效的反模式。这是一个重构。唯一真正的变化是跳过任何*.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。


0
投票

我在脚本中做错了什么?

您的脚本尝试压缩每个文件,但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 
} 
© www.soinside.com 2019 - 2024. All rights reserved.