我正在尝试修复这个我一直看到的错误,但我不知道为什么会看到它们。该脚本生成超过 7 天的文件列表,然后将它们 tar/gzip 到备份目录中。最后,它会在备份成功完成后删除这些文件。我遇到了一个奇怪的错误。该脚本很简单,大部分是为了防止脚本运行两次或挂起,或者它处理日志记录。重要的代码在星号之间。
#!/bin/bash
#
#
# Title: fwdCleanup.sh
# Author: Matthew Sarro
# Date: 03/07/2012
# Desc: Backs up all files down the tree which are
# older than 7 days
#
#
#
#
DATE=`date +%F-%H%M`
LOGFILE=/root/testing/fwdcleanuplogs/$DATE.log
exec 3<>$LOGFILE
exec >&3 2>&3
#define the name of the lockfile
LOCKFILE='/root/testing/fwdcleanup.pid'
# check for existing lockfile
if [ -e "$LOCKFILE" ]; then
# lockfile exists
#check if file is readable
if [ ! -r "$LOCKFILE" ]; then
echo error: lockfile is not readable
exit 1
fi
#define PID as the process number in lockfile
PID=`cat "$LOCKFILE"`
# check if process is signal-able and redirect stderr to /dev/null
kill -0 "$PID" 2>/dev/null
#if process was signal-able echo an error
if [ $? == 0 ]; then
echo error: existing instance of this task is already running
exit 1
fi
# process that created lockfile is no longer running - delete lockfile
rm -f "$LOCKFILE"
#check if lockfile deleted properly
if [ $? != 0 ]; then
echo error: failed to delete lockfile
exit 1
fi
fi
# create lockfile containing current process ID
echo $$ >"$LOCKFILE"
if [ $? != 0 ]; then
echo error: failed to create lockfile
exit 1
fi
#************************
#EDIT IN HERE
#find all files older than 7 days and create a file list
find /root/testing/{MSP,CEMP} -mtime +7 -type f > /root/testing/fwdcleanuplogs/$DATE.list
#add all files from the file list into a tar.gz file
tar -czf /root/testing/backups/$DATE.tar.gz --files-from /root/testing/fwdcleanuplogs/$DATE.list
#delete all files from the file list off disk
rm -f `cat /root/testing/fwdcleanuplogs/$DATE.list`
*************************
# delete lockfile
rm -f "$LOCKFILE"
if [ $? != 0 ]; then
echo error: failed to delete lockfile
exit 1
fi
exec 3>&-
exit 0
当我运行脚本时,生成的日志中显示以下内容:
./fwdCleanup.sh: line 67: backups: command not found
我不知道为什么会出现这种情况 - 我似乎无法弄清楚。代码仍然执行得非常好,但错误让我抓狂。我在第 67 行中看不到任何可以告诉操作系统备份是一个命令的东西。
如有任何建议,我将不胜感激。
编辑:在任何人注意到之前:这是在测试虚拟机上完成的,因此使用了 root 帐户:)
答案1
第 67 行*************************
应该#
以 开头。第一个行*
由您的 shell 展开,并拾取backups
当前目录中调用的文件。
答案2
使用-x
bash 选项来跟踪脚本正在做什么。这样您就可以很好地了解哪里出了问题。