0

I have several cron jobs I run to back up dbs:

0 8 * * * BACKUP=DEV DB=01 /usr/local/bin/backup.sh > /var/log/backup-db01-`date "+\%m\%d\%y"`.log 2>&1||/usr/local/bin/create-ticket.sh

0 16 * * * BACKUP=DEV DB=02 /usr/local/bin/backup.sh > /var/log/backup-db02-`date "+\%m\%d\%y"`.log 2>&1||/usr/local/bin/create-ticket.sh

In the script I am trying to create JIRA ticket on failure and attach the logs for the backup:

create-ticket.sh script:

#create Ticket for failed backup#
JIRA=`/opt/atlassian-cli/jira.sh --server https://blahblah --user admin --password blahblah --action createIssue --project "DEV" --type "Incident" --summary "Failed backup on $BACKUP $DB" --components "blah" --priority "Major"| awk '{print $2}'`

###Atttach logs:###
/opt/atlassian-cli/jira.sh --server https://blahblah --user admin --password blahblah --action addAttachment --issue "${JIRA}" --file "?????"

I need to know how to pass in the different file names for each time the cron runs to the {--file "???"} section... the file name changes daily and i need the script to pick up the newly created file for each DB on each day it fails.

0

I think you can introduce log file name parameter to be used for both redirection and when calling create-ticket.sh

0 8 * * * BACKUP=DEV DB=01 LOG=/var/log/backup-db01-$(date "+%m%d%y").log /usr/local/bin/backup.sh > $LOG 2>&1 || /usr/local/bin/create-ticket.sh $LOG

so create-ticked.sh should be then expecting that parameter and you can use it inside like

 ... --file "$1" ...

You may not want to use escaping % sign - $(date "+%m%d%y") - part will result in string like 021318 once so you guaranteed to stick with the same name.

0

Make a wrapper script to clean up your crontab;

0 8 * * * /usr/local/bin/backupOrTicket.sh 01

and put this in it;

DB=$1
BACKUP=DEV
FILE="/var/log/backup-$DB-$(date +%m%d%y).log"
/usr/local/bin/backup.sh > "$FILE" 2>&1 \
|| /usr/local/bin/create-ticket.sh "$FILE"

Using --file "$1" in create-ticket.sh . You might want to remove the BACKUP variable if it's not actually used. Also you should use date --iso-8601=s so that your log files sort properly. (Using any date format other than numerical largest to smallest is an anti pattern)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.