0

I need to calculate the time difference between RMAN start and RMAN End. Log is like this

-rw-r----- oracle/dba 2017-03-11 21:56:35 storage/backup/daily/ALG_PDB_2drut5jf.alg
-rw-r----- oracle/dba 2017-03-11 21:56:34 storage/backup/daily/ALG_PDB_2erut5jf.alg
-rw-r----- oracle/dba 2017-03-11 21:56:07 storage/backup/daily/ALG_PDB_2frut5jf.alg
-rw-r----- oracle/dba 2017-03-11 21:56:29 storage/backup/daily/ALG_PDB_2grut5jv.alg
-rw-r----- oracle/dba 2017-03-11 21:39:14 storage/backup/daily/BKPPDB_28rut42s.F_bkp
-rw-r----- oracle/dba 2017-03-11 21:47:28 storage/backup/daily/BKPPDB_29rut42s.F_bkp
-rw-r----- oracle/dba 2017-03-11 21:42:41 storage/backup/daily/BKPPDB_2arut42s.F_bkp
-rw-r----- oracle/dba 2017-03-11 21:55:33 storage/backup/daily/BKPPDB_2brut4k8.F_bkp
-rw-r----- oracle/dba 2017-03-11 21:55:36 storage/backup/daily/c-2819904582-20170311-00
-rw-r----- oracle/dba 2017-03-11 21:56:42 storage/backup/daily/c-2819904582-20170311-01
-rw-r----- oracle/dba 2017-03-11 21:56:41 storage/backup/daily/control_PDB_2hrut5kp.ctl

I wrote a script that first sort the column based on 4th column which is Time , Below is the script

#!/bin/bash -x
if [ -f /storage/backup/weekly/DB-Backup-$(date -d "yesterday" '+%a-%d%m%y').tgz ]
 then

export StartMin=$(tail -30 /storage/backup/weekly/DailyCompressionLog|grep "^-\|^ -" |sort -k5|awk '{print $5}' |awk -F ":" '{print $2}'|awk 'NR==1; END{print}'|awk 'FNR==1 {print $1}')
export EndMin=$(tail -30 /storage/backup/weekly/DailyCompressionLog|grep "^-\|^ -" |sort -k5|awk '{print $5}' |awk -F ":" '{print $2}'|awk 'NR==1; END{print}'|awk 'FNR==2 {print $1}')

export StartHour=$(tail -30 /storage/backup/weekly/DailyCompressionLog|grep "^-\|^ -" |sort -k5|awk '{print $5}' |awk -F ":" '{print $1}'|awk 'NR==1; END{print}'|awk 'FNR==1 {print $1}')
export EndHour=$(tail -30 /storage/backup/weekly/DailyCompressionLog|grep "^-\|^ -" |sort -k5|awk '{print $5}' |awk -F ":" '{print $1}'|awk 'NR==1; END{print}'|awk 'FNR==2 {print $1}')

echo "Backup for `date -d "yesterday" '+%a-%d%m%y'`" >> /storage/backup/weekly/MonthlyCompressionLog
echo -e "RMAN Backup Started at $StartHour":"$StartMin" >> /storage/backup/weekly/MonthlyCompressionLog
echo -e "RMAN Backup Ended   at $EndHour":"$EndMin" >> /storage/backup/weekly/MonthlyCompressionLog
echo -e "Time taken to Complete Backup Job Hours:Mins $((EndHour-StartHour))":"$((EndMin-StartMin))" >> /storage/backup/weekly/MonthlyCompressionLog

echo -e "-------------------------------------------------------\n" >> /storage/backup/weekly/MonthlyCompressionLog

When Script works (Success)

If End Time is Greater Than StartTime

When Script Fails

In current state if the Start Time is great than End Time , In this case Endhour will be greater than the StartHour

Scenario :: Lets say if RMAN started at 14:56 and completes at 15:02 , In this case total Time from Start to End is just 00:06 Min but script will show 1:-54

Any ideas ....

OmiPenguin
  • 111
  • 6

1 Answers1

1

Instead of calculating Hours and Minutes separately, calculate the number of minutes since midnight for Start and End, and perform arithmentic using that.

So something like :

StartNumMins=$(( $StartHour * 60 + $StartMin ))
EndNumMinutes=$(( $EndHour * 60 + $EndMin ))

MinutesDiff=$(( $EndNumMinutes - $StartNumMins ))

DiffHours="0$(($MinutesDiff / 60))"
DiffMins="0$(($MinutesDiff % 60))"

echo "Diff is $MinutesDiff minutes, which is ${DiffHours:-2}:${DiffMins:-2}"

which produces :

Diff is 6 minutes, which is 00:06

Of course, if your process crosses midnight, then you'll have to take the start/end dates into consideration, not just the times. There are a few options to do that (such as using GNU's 'date' if that's available to you - eg. see Convert an ISO date to seconds since epoch in linux bash ), but that's another issue.

Community
  • 1
  • 1
racraman
  • 4,988
  • 1
  • 16
  • 16