0

/var/www我为and创建了一个备份脚本/usr/lib/cgi-bin。每次编辑或添加文件时,都应通过incron.

root@corkea:~# incrontab -l
/usr/lib/cgi-bin IN_ATTRIB,IN_CREATE,IN_DELETE,IN_CLOSE_WRITE,IN_NO_LOOP create_incremental_backup_of_http_documents_and_cgi.sh
/var/www/html IN_ATTRIB,IN_CREATE,IN_DELETE,IN_CLOSE_WRITE,IN_NO_LOOP create_incremental_backup_of_http_documents_and_cgi.sh

我在 shell 中测试了脚本,它工作正常,但是使用incron脚本时总是返回错误。

我通过 Mail 收到的错误消息..

Apr 27 22:57:49 corkea create_incremental_backup_of_http_documents_and_cgi.sh[15011]: cp: regulÀre Datei â/var/tmp/http-documents-and-cgi-of-corkea.gtarâ kann nicht angelegt werden: Die Datei existiert bereits
Apr 27 22:57:49 corkea create_incremental_backup_of_http_documents_and_cgi.sh[15011]: Beende Skript, aufgrund vorheriger Fehler.

翻译:

cp: cant create file /var/tmp/http-documents-and-cgi-of-corkea.gtar, because it exist already.
Stopping script because of previous error(s).

此错误仅在incron用于启动脚本时发生。

不知何故,脚本在以下if else命令中失败了......

if [ -d "${BackupDirectory}/${SubDir}" ]
then
        cp -f "$FileIncremental" "$TmpDir"
else
        mkdir -p "${BackupDirectory}/${SubDir}"
fi

我添加 && [ -e "$FileIncremental" ]if命令中,但它也没有帮助。

这是脚本...

#!/bin/bash
# Create an incremental GNU-standard backup of Files for the http-Server.
# This script works with Debian Jessie and newer systems.
# Created for my corekea GW 2016-11-27.

MailTo="xxx@web.de"

Source=('/usr/lib/cgi-bin' '/var/www')
BackupDirectory=/media/nas_backups/corkea
TmpDir=/var/tmp

cd /

SubDir="http_documents_and_cgi.d"
FileTimeStamp=$(date "+%Y%m%d%H%M%S")
FileName="http-documents-and-cgi-of-$(uname -n)"
File="${BackupDirectory}/${SubDir}/${FileName}-${FileTimeStamp}.tgz"
FileIncremental="${BackupDirectory}/${SubDir}/${FileName}.gtar"

TimeStamp=$(date "+%F %T")              # This format "2011-12-31 23:59:59" is needed to read the journal
exec 1> >(logger -i -s -t "${0##*/}" -p 3) 2>&1 # all error messages are redirected to syslog journal and after that to stdout
trap "BriefExit" ERR        # Provide information for an admin (via sendmail) when an error occurred and exit the script

function BriefExit(){
    rm -f "$File" "$FileIncremental"
    mv "${TmpDir}/${FileIncremental##*/}" "${BackupDirectory}/${SubDir}" >/dev/null 2>&1
    case "$LANG" in
    de_DE.UTF-8)
        echo "Beende Skript, aufgrund vorheriger Fehler." 1>&2
    ;;
    *)
        echo "Stopping script because of previous error(s)." 1>&2
    ;;
    esac
    MailContent=$(journalctl -p 3 -o "short" --since="$TimeStamp" --no-pager)
    ScriptName="${0##*/}"
    SystemName=$(uname -n)
    MailSubject="${SystemName}: ${ScriptName}"
    printf 'Subject: %s\n\n%s\n' "$MailSubject" "$MailContent" | sendmail "$MailTo"
    exit 1
}

SourceCount="${#Source[@]}"
for ((Index=0;Index<=$SourceCount-1;Index++))
do
    FormatedSource[$Index]="${Source[$Index]#/}"
done

LoopCount=0
OpenFiles=1
while [ "$OpenFiles" -ne 0 ]
do
    if [ "$LoopCount" -le 30 ]
    then
        sleep 1
        OpenFiles=$(lsof "${Source[@]}" | wc -l)
        LoopCount=$((LoopCount + 1))
    else
        case "$LANG" in
        de_DE.UTF-8)
            echo "Es kann keine inkrementelle Sicherheitskopie erstellt werden, weil betroffene Dateien im Schreibemodus sind." 1>&2
        ;;
        *)
            echo "Can't create incremental backup, because some files are open." 1>&2
        ;;
        esac
        BriefExit
    fi
done

if [ -d "${BackupDirectory}/${SubDir}" ] && [ -e "$FileIncremental" ]
then
        cp -f "$FileIncremental" "$TmpDir"
else
        mkdir -p "${BackupDirectory}/${SubDir}"
fi

tar -cpzf "$File" -g "$FileIncremental" "${FormatedSource[@]}"
chmod 0700 "$File"

rm -f "${TmpDir}/${FileIncremental##*/}"

exit 0

我做错了什么,你能帮帮我吗?

4

2 回答 2

0

我找到了解决方案。即使激活incron了我的脚本也会运行两次。IN_NO_LOOP我不得不删除IN_ATTRIB,我删除了IN_DELETE. 现在它像它应该的那样工作。

于 2017-04-28T15:08:56.250 回答
0
if [ -d "${BackupDirectory}/${SubDir}" ]
then
    cp -f "$FileIncremental" "$TmpDir"
else
    mkdir -p "${BackupDirectory}/${SubDir}"
fi

cp使用的命令mkdir可能是别名或在 PATH 中找到的第一个。您可以添加type cptype mkdir之前添加信息以输出使用的命令。您可以显式指定可执行文件的路径:

/usr/bin/cp
/usr/bin/mkdir
于 2017-04-28T12:36:02.033 回答