Access to heroku toolbelt commands in a scheduled heroku dyno?

后端 未结 4 968
礼貌的吻别
礼貌的吻别 2021-01-02 11:04

I want to call heroku postgres backup/restore commands within a scheduled heroku task, but the heroku toolbelt isn\'t available from bash prompt, so I can\'t call heroku com

相关标签:
4条回答
  • 2021-01-02 11:12

    Put this at the top of your scheduled bash script:

    # install heroku toolbelt
    # inspired by https://toolbelt.heroku.com/install.sh
    curl -s https://s3.amazonaws.com/assets.heroku.com/heroku-client/heroku-client.tgz | tar xz
    mv heroku-client/* .
    rmdir heroku-client
    PATH="bin:$PATH"
    

    then, for example, you can call:

    heroku pg:reset HEROKU_POSTGRESQL_YELLOW_URL  --app myapp-staging --confirm myapp-staging
    heroku pgbackups:restore HEROKU_POSTGRESQL_YELLOW_URL `heroku pgbackups:url --app myapp-production` --app myapp-staging --confirm myapp-staging
    

    and poof! Staging database is updated from production database.

    0 讨论(0)
  • 2021-01-02 11:12

    As Heather Piwowar said, you can indeed download and untar the heroku tollbelt yourself, but no need to move files around after this. Here is a shorter version:

    curl -s https://s3.amazonaws.com/assets.heroku.com/heroku-client/heroku-client.tgz | tar xz
    PATH="/app/heroku-client/bin:$PATH"
    

    You can now use the heroku command as you wish.

    For authentification, you may want to set the HEROKU_API_KEY env var (using heroku config:set HEROKU_API_KEY=1234567890 -a your-app-name).

    Also, note that the first use of the heroku command will run longer than expected because it will try to install the latest version, dependencies and core plugins.

    0 讨论(0)
  • 2021-01-02 11:26

    None of the other solutions worked for me since Heroku locks the filesystem after the buildpacks finish running.

    There's a third-party buildpack that installs the CLI for you. First you set your auth key as an ENV variable on your app:

    heroku config:set HEROKU_API_KEY=`heroku auth:token` -a myapp
    

    Then add the buildpack:

    heroku buildpacks:add https://github.com/heroku/heroku-buildpack-cli -a myapp

    Redeploy your app and it should be able to access your apps via the CLI. If you have https://devcenter.heroku.com/articles/dyno-metadata enabled, you can even access your current app's name with $HEROKU_APP_NAME.

    0 讨论(0)
  • 2021-01-02 11:33

    You can use Heroku Scheduler and configure the following command (as an example) to create a database backup:

    curl -s https://s3.amazonaws.com/assets.heroku.com/heroku-client/heroku-client.tgz \
     | tar xz && ./heroku-client/bin/heroku pg:backups:capture -a you-app-name-here
    

    For this to work, you need to add a Config Variable named HEROKU_API_KEY and set its value to the "API Key" value from your Accounts page.

    0 讨论(0)
提交回复
热议问题