1

I am running a heroku command heroku pg:backups:capture --app appname to backup a database, which returns an output in the terminal like this:

Use Ctrl-C at any time to stop monitoring progress; the backup will continue running.
Use heroku pg:backups:info to check progress.
Stop a running backup with heroku pg:backups:cancel.

Starting backup of postgresql-xxxxxxxxxx... done
Backing up DATABASE to b007... done

How can I store the backup name b007 to a variable automatically?

3 Answers 3

3

You could pipe your command to sed to extract the desired string:

 dbname=$(heroku pg:backups:capture --app appname | sed -n 's/.*DATABASE to \([^\.]*\)\.\.\..*/\1/p')
Sign up to request clarification or add additional context in comments.

2 Comments

Actually this doesn't work. After running the heroku command, the terminal interface shows the progress, which looks something like this ibb.co/ZVQvW0W, and after the backup is complete, it shows as done. Maybe that's why the command you suggested doesn't work?
@vikiboy try adding 2>&1 before the pipe. It could be that heroku uses stderr instead of stdout.
2

You could redirect via pipe | the output of the program to

grep "Backing up DATABASE to " | awk '{print substr($5,0,5)}'

It would be:

var=$(heroku pg:backups:capture --app appname | grep "Backing up DATABASE to " | awk '{print substr($5,0,5)}')

2 Comments

I edited the output line in the question. Will that update your answer? Sorry for the trouble. I am seeing time and check as the output with your suggestion.
Depending on the output you just have to change the parameter you give to grep and awk. grep will filter all the lines that do not contain the string you provide, and awk will take out the part you want from the line.
0

Simplistic possibility:

while read -r line
do echo "$line"
   case "$line" in
   Backing\ up\ DATABASE\ to\ *done)
       backup="${line%%... done}"
       backup="${backup##* }"
   esac
done < <( heroku pg:backups:capture --app appname )
echo "Backup: [$backup]"

2 Comments

Backup: [] Tried it. I am getting this.
Make sure the match string is accurate. This worked for what I copy/pasted from your OP, but if the output differs you'll have to tweak.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.