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?
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:
Perversely, I found the easiest thing to do was just to email the content to myself.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With