I have the following script which checks backup logs for errors and want it to print the error information that is standing in the line before the errorcode without also printing the errorcode. I tried this solution but I still get both lines.
#!/bin/bash
mytail=$(tail -n 13 bck.log)
mydate=$(date -dyesterday +%y%m%d)
bckerr=$(grep -B1 -i "backupexitcode:" bck.log | grep -v "backupexitcode:")
if [[ $mytail =~ $mydate ]]
then
echo "Backup is up to date!"
mytail=${mytail,,}
if [[ "$mytail" == *failed* || "$mytail" != *backupexitcode:0* ]]
then
echo "There is an error in the last backup!"
echo "$bckerr"
exit 1001
else
echo "No errors were found."
exit 0
fi
else
echo "Backup didn't run!"
exit 1002
fi
The 2 lines that are involved seperated from the rest:
INFO Older backups were deleted without error.
run_backup.sh(25863) 220228-200357: Instance:server:port Schema:"_instanceBackup" Finish: Timestamp:220228_200236 BackupExitCode:0
I've seen a few similar problems that were solved using sed
or awk
but none of those worked for me.