我正在努力解决这个我一直在看的错误,而且我不确定为什么要看到它们。 该脚本生成7天以前的文件列表,然后将它们/ gzip压缩到备份目录中。 最后,一旦备份成功完成,它将删除这些文件。 我遇到一个奇怪的错误。 该脚本很简单,大部分是防止脚本运行两次或挂起,或处理日志logging。 重要的代码是在星号之间。
#!/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
我不知道这是为什么显示 – 我似乎无法弄清楚。 代码仍然执行出色,但错误是驱使我batty。 我看不到第67行的任何内容,甚至可以远程告诉操作系统备份是一个命令。
任何build议真的会被赞赏。
编辑:在任何人注意之前:这是在一个testing虚拟机,因此使用的根帐户:)
你的第67行是************************* ,开头应该是# 。 第一个*由shell进行扩展,并在当前目录中backups名为backups的文件。
使用-x选项打个招呼,以获得脚本正在做的事情。 这会给你一个很好的想法,说明事情进展不顺。