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 commands:
$ heroku run bash --app myapp
Running `bash` attached to terminal... up, run.4805
~ $ heroku --version
bash: heroku: command not found
How can I get heroku commands available in my scheduled bash script?
I don't know anything about Ruby or Ruby Gems.
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.
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.
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.
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.
Related
I manage to deploy a Scala Play 2.7 App that uses Postgres but it doesn't run with error:
(base) bravegag#Zeus:~/code/myapp$ heroku open
› Error: Missing required flag:
› -a, --app APP app to run command against
› See more help with --help
(base) bravegag#Zeus:~/code/myapp$ heroku logs --tail
› Error: Missing required flag:
› -a, --app APP app to run command against
› See more help with --help
How do I fix that?
that's a heroku cli flag, not scala/play. just specify the name of your heroku app when using the heroku cli:
$ heroku open --app <app-name>
(same for other commands)
you can find the name of your app in heroku dashboard (somewhere in https://heroku.com)
If you enter your app in heroku you will reach a link like https://dashboard.heroku.com/apps/<app-name>
you need to set git remote to your application first, or heroku won't detect your flags,
heroku git:remote -a your_app_name
first you need to initialize git (which is required for Heroku)
$ git init
$ git add .
$ git commit -m "first"
then you should
$ heroku git:remote -a your_app_name
and then finally you can:
$ heroku container:push web
I used to run heroku create and deploying with git push heroku master but I've since moved onto a Github based workflow where code is first pushed to my Github repo, and deployed via a web hook.
I now have to run --app appname or -a appname after every command, and it gets annoying.
e.g.
heroku run rails db:migrate -a appname # instead of
heroku run rails db:migrate
heroku run rails console -a appname # instead of
heroku run rails console
heroku run logs -t -a appname # instead of
heroku run logs -t
How do I force heroku-toolbelt to link this repo to the heroku app so that I can run heroku without the --app parameter
Add a remote named heroku with the url https://git.heroku.com/appname.git
Run this:
$ git remote add heroku https://git.heroku.com/appname.git
I am trying to run bash on heroku to test it out and it is failing
$ heroku run bash
▸ Error: No app specified
▸ Usage: heroku run --app APP
▸ We don't know which app to run this on.
▸ Run this command from inside an app folder or specify which app to use with --app APP
▸
▸ https://devcenter.heroku.com/articles/using-the-cli#app-commands
$ heroku run --app bash
▸ Usage: heroku run COMMAND
▸
▸ Example: heroku run bash
So, the example says heroku run bash will work but it doesn't. I have no dynos running. I feel I am missing something basic here...
Try run commands:
First you need to login, then you to see your apps and finally run bash
heroku login
Insert you user and password
heroku apps
=== user#gmail.com Apps
myaplication
then look at the list apps and write
heroku run bash --app myaplication
You could also do this:
get app name using;
heroku apps
then set heroku remote;
heroku git:remote -a yourappname
and finally run bash on/in your app
heroku run bash
I think you have two issues.
Firstly, you need to run bash within some app. You can either specify the app via the --app key as the help actually says or you can run this command inside the folder which has a heroku app initialized already. For connecting the folder to a heroku app - see this answer How to link a folder with an existing Heroku app.
Second, running a bash actually takes away one dyno from your app. So you need to have at least one dyno.
Try this command:
heroku pg:psql -a appname
Why is heroku run bash not working?
$ heroku login
Enter your Heroku credentials.
Email: xx#yy.zz
Password (typing will be hidden):
Logged in as xx#yy.zz
$ heroku run bash
▸ No app specified.
▸ Run this command from an app folder or specify which app to use with --app APP
$ heroku run --app bash
▸ Usage: heroku run COMMAND
▸
▸ Example: heroku run bash
$ heroku run "ls /usr/bin/"
▸ No app specified.
▸ Run this command from an app folder or specify which app to use with --app APP
$ heroku run --app "ls /usr/bin/"
▸ Usage: heroku run COMMAND
▸
▸ Example: heroku run bash
$ heroku version
heroku-toolbelt/3.42.22 (universal.x86_64-darwin15) ruby/2.0.0
heroku-cli/4.27.11-7569c5d (amd64-darwin) go1.5.2
=== Installed Plugins
heroku-apps#1.1.0
heroku-cli-addons#0.1.1
heroku-fork#4.0.0
heroku-git#2.4.4
heroku-local#4.1.5
heroku-run#2.9.2
heroku-spaces#2.0.9
heroku-status#1.2.4
As the error message indicates, you need to specify which heroku app you want to run commands on. (If you're in a git repository with a remote pointing to heroku, it will use that one by default; that's what it means by "from an app folder".)
$ heroku apps
will give you a list.
$ heroku run --app INSERT_APP_NAME_HERE bash
will run bash on that app.
This error is because of not specifying the app you want to use. Try this
$heroku app
example
$heroku run bash --app examp
need to manually specify the name of the image with the run
command like:
heroku run bash --type worker -a app_name
I have to load pg_dump and the pg_restore to Heroku so that I can load a local database to Heroku. How can I install pg_dump and pg_restore on Heroku? What are their addresses?
So they way Heroku works is you install an addon to your Heroku app called Heroku Postgres. Then you add another app called PGBackups. These apps expose commands to your command line. They are like subcommands to the heroku command. So you'd do heroku pgbackups:restore to restore (or really, load initially) a database to your Heroku instance. See this link:
https://devcenter.heroku.com/articles/heroku-postgres-import-export