This is way overcomplicated. All you actually have to do to grab your latest scheduled Postgres DB dump is run:
curl -o latest.dump `heroku pg:backups public-url -a [app]`
(and then pg_restore)