Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heroku Rails Console Write to Local File

I want to pull some information from my database into a text file.

What's a good way to do that? I originally thought of running my heroku bash and rails console as I only need to do a simple loop to get the info I need. But I don't know the proper way to write to a file from heroku. It works in my local rails console

I've tried

File.open('text.txt', 'w') do |f|
    User.all.each do |u|
        f.puts u.email
    end
end

or something like $stdout = File.new('/path/to/text.txt', 'w')

but I think these files don't end up in my local directory...

How do I do this?

Alternate simple solutions also welcome since I didn't think I was doing anything overly complex

Attempting Pakrash's answer

In heroku bash + rails c

I put in

 require 'net/ftp'

ftp = Net::FTP.new('address.herokuapp.com','[email protected]', 'somepassword')

This just hangs though. No error.. I have to crl +c out of it

I previously had my full address https://adddress.herokuapp.com/ in there but it was saying getaddrinfo: Name or service not known

Should I be entering this in differently?

like image 367
Clam Avatar asked Jan 26 '15 04:01

Clam


3 Answers

Heroku supports ftp in passive mode. So create the file on the server and download the file to local machine with ftp.

# FTPing from Heroku
ftp = Net::FTP.new(server, user, pass)
ftp.passive = true
ftp.getbinaryfile(remote_filename, tmp_filename)

References:

  1. http://www.web-l.nl/posts/30-downloading-files-using-ftp-on-heroku
  2. https://stackoverflow.com/a/5570645/429758
like image 99
Prakash Murthy Avatar answered Oct 29 '22 21:10

Prakash Murthy


Perversely, I found the easiest thing to do was just to email the content to myself.

like image 20
John with waffle Avatar answered Oct 29 '22 20:10

John with waffle


I don't know that it's advisable, but you can actually start a rails console instance locally while connecting to the remote database on Heroku. Get the database connection information from the Heroku dashboard and add it to your local database.yml file in place of your regular development stanza. Something like

development:
  adapter: postgresql
  encoding: unicode
  database: agblah9dff3
  host: ec2-44-333-444-100.compute-1.amazonaws.com
  pool: 5
  username: 8fk38hg72hd98d
  port: 5462
  password: 88dk3jblahblah8sk83df8sdfj23
  timeout: 5000

But know that you're playing with production code, so...you know, it's pretty risky.

A better idea might be to just use a database IDE that creates exports for you, or write a rake task that collects the info in a file and dumps it on S3. You won't be able to write files to disk Heroku.

like image 31
akbrown Avatar answered Oct 29 '22 20:10

akbrown