Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I download a file from Heroku bash?

Tags:

bash

heroku

People also ask

How can I download my code from heroku?

Just go to https://dashboard.heroku.com/apps/YOUR_APP_NAME/deploy/heroku-git. If you haven't already, log in to your Heroku account and follow the prompts to create a new SSH public key. Use Git to clone YOUR_APP_NAME's source code to your local machine.

Can heroku save files?

Heroku has an “ephemeral” hard drive, this means that you can write files to disk, but those files will not persist after the application is restarted. By default Active Storage uses a :local storage option, which uses the local file system to store any uploaded files.

How do I access heroku files?

The dashboard can be accessed via the CLI: $ heroku addons:open filepicker Opening filepicker for sharp-mountain-4005… or by visiting the Heroku Dashboard and selecting the application in question. Select Ink file picker from the Add-ons menu.

What is a heroku clone?

This command creates a copy of the Heroku-hosted repository that contains your app's source and complete repository history. It also includes a heroku Git remote to simplify future pushes. You cannot clone your app's source from Heroku if you deploy your app with any method besides git push .


If you manage to create the file from heroku run bash, you could use transfer.sh.

You can even encrypt the file before you transfer it.

cat <file_name> | gpg -ac -o- | curl -X PUT -T "-" https://transfer.sh/<file_name>.gpg

And then download and decrypt it on the target machine

curl https://transfer.sh/<hash>/<file_name>.gpg | gpg -o- > <file_name>

There is heroku ps:copy:

#$ heroku help ps:copy 
Copy a file from a dyno to the local filesystem

USAGE
  $ heroku ps:copy FILE

OPTIONS
  -a, --app=app        (required) app to run command against
  -d, --dyno=dyno      specify the dyno to connect to
  -o, --output=output  the name of the output file
  -r, --remote=remote  git remote of app to use

DESCRIPTION
  Example:

       $ heroku ps:copy FILENAME --app murmuring-headland-14719

Example run:

#$ heroku ps:copy app.json --app=app-example-prod --output=app.json.from-heroku
Copying app.json to app.json.from-heroku
Establishing credentials... done
Connecting to web.1 on ⬢ app-example-prod... 
Downloading... ████████████████████████▏  100% 00:00 

Caveat

This seems not to run with dynos that are run via heroku run.

Example

#$ heroku ps:copy tmp/some.log --app app-example-prod --dyno run.6039 --output=tmp/some.heroku.log
Copying tmp/some.log to tmp/some.heroku.log
Establishing credentials... error
 ▸    Could not connect to dyno!
 ▸    Check if the dyno is running with `heroku ps'

It is! Prove:

#$ heroku ps --app app-example-prod
=== run: one-off processes (1)
run.6039 (Standard-1X): up 2019/08/29 12:09:13 +0200 (~ 16m ago): bash

=== web (Standard-2X): elixir --sname dyno -S mix phx.server --no-compile (2)
web.1: up 2019/08/29 10:41:35 +0200 (~ 1h ago)
web.2: up 2019/08/29 10:41:39 +0200 (~ 1h ago)

I could connect to web.1 though:

#$ heroku ps:copy tmp/some.log --app app-example-prod --dyno web.1 --output=tmp/some.heroku.log
Copying tmp/some.log to tmp/some.heroku.log
Establishing credentials... done
Connecting to web.1 on ⬢ app-example-prod... 
 ▸    ERROR: Could not transfer the file!
 ▸    Make sure the filename is correct.

So I fallen back to using SCP scp -P PORT tmp/some.log user@host:/path/some.heroku.log from the run.6039 dyno command line.


Heroku dyno filesystems are ephemeral, non-persistant and not shared between dynos. So when you do heroku run bash, you actually get a new dyno with a fresh deployment of you app without any of the changes made to ephemeral filesystems in other dynos.

If you want to do something like this, you should probably either do it all in a heroku run bash session or all in a request to a web app running on Heroku that responds with the CSV file you want.


Another way of doing this (that doesn't involve any third server) is to use Patrick's method but first compress the file into a format that only uses visible ASCII charaters. That should make it work for any file, regardless of any whitespace characters or unusual encodings. I'd recommend base64 to do this.

Here's how I've done it:

  1. Log onto your heroku instance using heroku run bash
  2. Use base64 to print the contents of your file: base64 <your-file>
  3. Select the base64 text in your terminal and copy it
  4. On your local machine decompress this text using base64 straight into a new file (on a mac I'd do pbpaste | base64 --decode -o <your-file>)