【发布时间】:2019-01-16 13:59:27
【问题描述】:
我创建了一个脚本来压缩并将日志文件从一个目录移动到另一个目录以释放空间。这是脚本:
#!/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)中运行了相同的脚本,但没有出现错误。
我的问题:
- 如何防止出现此错误?
- 我在脚本中做错了什么?
【问题讨论】:
-
文件
/data/logs/log01.log.gz存在于$logsDirectory中。gzip告诉您该文件已经具有.gz扩展名并且不会再次压缩它。然后您尝试将/data/logs/log01.log.gz.gz移动到备份目录中。那失败了。另外,请考虑使用set -x运行您的脚本。 -
只需添加一个步骤进行测试,如果它已经以.gz结尾,则跳过它,或者移动它。
-
感谢您的解决方案,他们帮助我找到了解决方案。