I created a backup-script for /var/www
and /usr/lib/cgi-bin
. Every time when a file gets edited or added the script should be called via 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
I tested the script in a shell and it works fine, but when using incron
the script always returns an error.
The error messages I receive via 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.
Translation:
cp: cant create file /var/tmp/http-documents-and-cgi-of-corkea.gtar, because it exist already.
Stopping script because of previous error(s).
This error only occur when using incron
to start the script.
Somehow the script failed at the following if else
command ...
if [ -d "${BackupDirectory}/${SubDir}" ]
then
cp -f "$FileIncremental" "$TmpDir"
else
mkdir -p "${BackupDirectory}/${SubDir}"
fi
I added && [ -e "$FileIncremental" ]
to the if
command, but it didnt help neither.
Here is the script...
#!/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
What did I wrong and can you help me ?