Commit 7a4bf5d7 authored by Fjen Undso's avatar Fjen Undso
Browse files

move failstate into seperate function

parent 322a7e0d
......@@ -31,23 +31,37 @@ declare -r FTPNUMBACKUPS=90
declare -r GPGPASS="" # optional gpg symmetrical passphrase
##### END SETTINGS
fail() {
if [ -n "$ADMINMAIL" ]; then
### Mail admin on error
tmplogfile=`mktemp`
echo "Date: $(date)" >> $tmplogfile
echo "Hostname: $(hostname)" >> $tmplogfile
echo "Failed with message $1" >> $tmplogfile
mail -s "MySQL BACKUP FAILED" "$ADMINMAIL" < $tmplogfile
rm $tmplogfile
fi
echo "$1" >&2
exit 1
}
execftp() {
lftp -u $FTPUSER,$FTPPASS $FTPTYPE://$FTPHOST -e "$1"
[ $? -ge 1 ] \
&& fail "ftp command failed: $1"
}
### Check prerequisites
for i in mydumper lftp mail tar $COMPRESS; do
if [ ! "$(which $i)" ]; then
echo "Error: prerequisite $i not found in PATH" >&2
exit 1
fail "Error: prerequisite $i not found in PATH"
fi
done
### Check local directory
if [ ! -d "$LOCALDIR" ]; then
mkdir -p "$LOCALDIR" \
|| echo "Failed to create $LOCALDIR" >&2 \
&& exit 1
|| fail "Failed to create $LOCALDIR"
fi
### Delete local old backups
......@@ -62,10 +76,10 @@ echo "### MySQL Backup..."
date=$(date +"%Y%m%d-%H%M%S")
backupdir="$LOCALDIR/$date"
mkdir "$backupdir" \
|| echo "Failed to create $backupdir" >&2 \
&& exit 1
|| fail "Failed to create $backupdir"
mydumper -u "$MYUSER" -p "$MYPASS" -h "$MYHOST" --regex "$EXLUDEDBS" -o "$backupdir"
faildump=$?
[ $? -ge 1 ] \
&& fail "mydumper failed"
### Compression
echo "### Compression..."
......@@ -94,14 +108,12 @@ fi
if [ "$FTPTYPE" != "ftp" ] && [ "$FTPTYPE" != "sftp" ]; then
echo "NO FTP UPLOAD CONFIGURED"
failftp=0
else
### FTP: directory permission check
ftppwd=$(execftp "mkdir -p $FTPDIR/$date; cd $FTPDIR/$date; pwd; exit")
FTPDIRnoslash=${FTPDIR%\/} # remove trailing /
if [[ "$ftppwd" != *"$FTPDIRnoslash/$date" ]]; then
echo "Error: failed to create $FTPDIRnoslash/$date on ftp" >&2
failftp=1
fail "Error: failed to create $FTPDIRnoslash/$date on ftp"
fi
### FTP: delete old backups
......@@ -118,20 +130,7 @@ else
execftp "cd $FTPDIR; rm -r ${ftpfilesdelete[@]}; exit"
### FTP: upload
if [ -z $failftp ]; then
echo "### Upload to FTP..."
execftp "cd $FTPDIR; mirror -R $backupdir; exit"
failftp=$?
fi
fi
### Mail admin on error
if [ "$faildump" -ge 1 ] || [ "$failftp" -ge 1 ]; then
tmplogfile=`mktemp`
echo "Date: $(date)" >> $tmplogfile
echo "Hostname: $(hostname)" >> $tmplogfile
echo "Failed with mysqldump: $faildump / ftp: $failftp" >> $tmplogfile
mail -s "BACKUP FAILED" "$ADMINMAIL" < $tmplogfile
rm $tmplogfile
echo "### Upload to FTP..."
execftp "cd $FTPDIR; mirror -R $backupdir; exit"
fi
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment