ruby on rails heroku restore - ruby-on-rails

I just try to learn RoR from this TUTORIAL and in 25th video i didn't get the idea how to restore my data. Example codes are deprecated.
What I have done :
1 - heroku pg:backups capture
2 - pg_dump -Fc --no-acl --no-owner -h localhost -U postgres myrubyblog > myrubyblog.dump
3 - on this point i didn't get the idea how to restore my data from dump
heroku pg:backups restore
'https://www.dropbox.com/home?preview=myrubyblog.dump' DATABASE_URL
What does it mean DATABASE_URL? the database form my local or heroku's database URL ?

Hello there If you had backed up you data with heroku then you can easily restore it there is a good documentation on heroku dev centre Heroku pg data restore

Related

How to Upload data to Heroku ? Rails 4

My Rails app is about an Online Shopping site.
I'm trying Deploy it to Heroku.
I've done push to heroku,and rake db:migrate.
But now in the My web,it doesn't have any Data.
In the localhost:3000,I can show Products,Phones,Users,etc.
But in my web:https://boiling-bayou-5793.herokuapp.com/welcome/home
It doesn't show anything.
So what should I do to upload data from my PC to my web
The data from your local development database is not automatically transferred to the production environment. There is no rake task to do that.
You can use seed files and the rake db:seed task to get some initial data in your (production) database.
A more heroku-specific method could be just loading a database dump.
Assuming you're using Postgresql, n your local computer do
pg_dump -h localhost -U username -Fc dbname > db.dump
upload the file somewhere on the internet (like Amazon S3 so you can get a direct download link) and then on heroku run
heroku pgbackups:restore SHARED_DATABASE http://www.example.com/db.dump
Remember to remove the db dump right away if it's any kind of sensitive data!

How to deploy a local postgresql database of Ruby on rails app to heroku?

I have been trying to deploy a local database which has some data into heroku. My app's name is myFirstBlog. But after deploying when I refresh my app, it says something went wrong. After so troubeshooting. I found out this:
Before exporting data into heroku database, I ran the command
heroku pg:info
And the output was:
Plan:Hobby-dev
Status:Available
Connections:1/20
PG Version: 9.4.1
Created: 2015-07-09 08:20 UTC
Data Size:6.6 MB
Tables: 3
Rows: 2/10000 (In compliance)
Fork/Follow: Unsupported
Rollback: Unsupported
And after I exported data to heroku and ran the same command, output was:
Plan: Hobby-dev
Status: Available
Connections:1/20
PG Version:9.4.1
Created:2015-07-09 08:20 UTC
Data Size:6.5 MB
Tables:0
Rows: 0/10000 (In compliance)
Fork/Follow:Unsupported
Rollback:Unsupported
The number of tables becomes 0 after export. Why is it happenning?
This is how I am exporting my local database's data to heroku:
PGPASSWORD="password" pg_dump -Fc --no-acl --no-owner -h localhost -U aditya9509 myFirstBlog_development > backup.dump //This command dumps the data in the backup.dump
Then I saved backup.dump in my github account because the tutorials stated that in order to export data from local database to heroku, it must be at a location which can be retrieved using a http protocol. I did not understand why but I did what it said.
Then finally I ran this command:
heroku pg:backups restore "http://github.com/aditya9509/rubyOnRails/blob/master/backup.dump" DATABASE -a stark-beach-9626
The "stark-beach-9626 is the name of the app given by heroku.
After running this command, when I access the app, it shows "something went wrong". What am I missing here?
P.S. I am new to ruby on rails so please be as simple as you can when you answer. I have been busting my head to solve thi problem for hours now. Also, let me know if you need some additional info. I gave all the info I thought was relevant.
You are trying to restore the git blob and not the raw file, use https://github.com/aditya9509/rubyOnRails/raw/master/backup.dump for the url and it will work.

heroku pgbackups:restore not working on rails app

For some reason heroku pgbackups:restore won't work on my rails app,
I'm creating my dumpfile using the command:
pg_dump -F c -v -U john -h localhost project_development > -f dumpfile.dump
I then upload it to dropbox, as the heroku documentation says it needs to be needs to uploaded somewhere with an HTTP-accessible URL. After I've uploaded the file to dropbox, I then run the command
heroku pgbackups:restore HEROKU_POSTGRESQL_RED_URL 'https://dl.dropboxusercontent.com/content_link/JgHkmg0iv8vIMKWqlN6medJqjClPeWYl2AQqJWV8IeEiujnfYvPdtvq7xh26dvCQ'
the output of which is
Retrieving... done
Restoring... done
HEROKU_POSTGRESQL_RED_URL is my database url and the line in the single quotes is a raw version of my dumpfile(the dropbox documentation showed how to turn the dl file into a raw file).
But whenever go to my apps homepage I get an error, I can see from running heroku logs that the error is a database error
PG::UndefinedTable: ERROR: relation "listings" does not exist
LINE 1: SELECT "listings".* FROM "listings" ORDER BY listings.pri..
Also, when I run heroku pg:info I can see that nothing has actually been inserted into the database
=== HEROKU_POSTGRESQL_RED_URL (DATABASE_URL)
Plan: Hobby-dev
Status: Available
Connections: 2/20
PG Version: 9.3.4
Created: 2014-10-21 15:24 UTC
Data Size: 7.0 MB
Tables: 0
Rows: 0/10000 (In compliance)
Fork/Follow: Unsupported
Rollback: Unsupported
Can anyone tell me why my database isn't being updated? Thanks.
I tested this on a blank hobby dev database with heroku. You don't need to include the the no-owner and no-acl options (-x -O) but it's considered good practice since those alter statements will fail.
The problem with your dump command is the stray >
pg_dump -F c -v -O -x -U john -h localhost project_development -f dumpfile.dump
Upload the resulting file to dropbox or s3 and try the restore again. If there are errors you can run
heroku logs -n 100 --ps pgbackups
To see the log output from the pg_restore command.

Transfer initial PostgreSQL database from development to Heroku production

I have an initial set of production data stored locally in the development database that I'd like to migrate to production for a starting point for data. What's the best way to transfer this data?
It does not seem evident if there is a way to use pgbackups as per the instructions. Perhaps I have to run a manual backup of some sort locally and then push it over with pgbackups and if that is the case, I'd appreciate some specific instructions on accomplishing this.
After some additional digging and an answer from Heroku, the answer for importation of initial data is to:
1) If using PGSQL locally, first dump the data:
pg_dump -U your_username your_database_name -f backup.sql
2) Then follow the instructions found here for importation to Heroku's database:
http://devcenter.heroku.com/articles/pgbackups#importing_from_a_backup
First dump your local database using pg_dump:
pg_dump -Fc --no-acl --no-owner -h ... mydb > mydb.dump
and then use heroku pgbackups:restore:
heroku pgbackups:restore heroku-database-url url-to-your-mydb.dump-file
Note that the mydb.dump file needs to be accessible by the Heroku servers.
The Heroku Dev Center page has detailed instructions:
https://devcenter.heroku.com/articles/heroku-postgres-import-export

transfer db from one heroku app to another faster

Is there a faster way to transfer my production database to a test app?
Currently I'm doing a heroku db:pull to my local machine then heroku db:push --app testapp but this is becoming time consuming. I have some seed data but it is not nearly as accurate as simply testing with my real-world data. And since they're both stored on a neighboring AWS cloud, there must be a faster way to move the data?
I thought about using a heroku bundle, but I noticed the animate command is gone?
bundles:animate <bundle> # animate a bundle into a new app
It's quite common to migrate databases between staging, testing and production environments for Rails Apps. And heroku db:pull/push is painfully slow. The best way I have found so far is using Heroku PG Backups add-on and it's free. I followed following steps to migrate
production database to staging server:
1) Create the backup for the production-app db
heroku pg:backups capture --app production-app
This will generate b001 backup file from the main database (usually production db in database.yml)
2) To view all the backups (OPTIONAL)
heroku pg:backups --app production-app
3) Now use the pg:backups restore command to populate staging server database from the last backup file on production server
heroku pg:backups restore $(heroku pg:backups public-url --app production-app) DATABASE_URL --app staging-app
Remember that restore is a destructive operation, it will delete existing data before replacing it with the contents of the backup file.
So things are even easier now .. checkout the transfer command as part of pgbackups
heroku pgbackups:transfer HEROKU_POSTGRESQL_PINK sushi-staging::HEROKU_POSTGRESQL_OLIVE -a sushi
https://devcenter.heroku.com/articles/upgrading-heroku-postgres-databases#4b-alternative-transfer-data-between-applications
This has worked beautifully for me taking production code back to my staging site.
The correct answer has changed again as of March 11, 2015.
heroku pg:backups restore $(heroku pg:backups public-url --app myapp-production) DATABASE_URL --app myapp-staging
Note specifically that the argument is now public-url.
https://blog.heroku.com/archives/2015/3/11/pgbackups-levels-up
Update for mid-2015...
The pgbackups add-on has been deprecated. No more pgbackups:transfer. pg:copy is ideal for this scenario.
To copy a database from yourapp (example db name: HEROKU_POSTGRESQL_PINK_URL to yourapp_staging (example db name: HEROKU_POSTGRESQL_WHITE_URL)
# turn off the web dynos in staging
heroku maintenance:on -a yourapp-staging
# if you have non-web-dynos, do them too
heroku ps:scale worker=0 -a yourapp-staging
# backup the staging database if you are paranoid like me (optional)
heroku pg:backups capture -a yourapp-staging
# execute the copy to splat over the top of the staging database
heroku pg:copy yourapp::HEROKU_POSTGRESQL_PINK_URL HEROKU_POSTGRESQL_WHITE_URL -a yourapp-staging
Then when it's complete, turn staging back on:
# this is if you have workers, change '1' to whatever
heroku ps:scale worker=1 -a yourapp-staging
heroku maintenance:off -a yourapp-staging
Reminder: you can use heroku pg:info -a yourapp-staging (and yourapp) to get the database constants.
(source: https://devcenter.heroku.com/articles/upgrading-heroku-postgres-databases#upgrade-with-pg-copy-default)
psql -h test_host -c 'drop database test_db_name; create database test_db_name;'
pg_dump -h production_host production_db_name | psql -h test_host test_db_name`
This can be done on production_host or on test_host — will work both ways.
Have not tested this, but it might work.
Do this to get the URL of your source database:
heroku console "ENV['DATABASE_URL']" --app mysourceapp
Then try executing db:push with that.
heroku db:push database_url_from_before --app mytargetapp
This might not work if Heroku doesn't allow access to the DB machines from outside their network, which is probably the case. You could, perhaps, try using taps (gem that heroku db commands use internally) from within your app code somewhere (maybe a rake task). This would be even faster than the above approach because everything stays completely within AWS.
Edit:
Here's an (admittedly hacky) way to do what I described above:
Grab the database URL as in the first code snippet above. Then from a rake task (you could do it on console but you risk running into the 30 second timeout limit on console commands), execute a shell command to taps (couldn't easily determine whether it's possible to use taps directly from Ruby; all docs show use of the CLI):
`taps pull database_url_from_source_app #{ENV['DATABASE_URL']}`
The backticks are important; this is how Ruby denotes a shell command, which taps is. Hopefully the taps command is accessible from the app. This avoids the problem of accessing the database machine from outside Heroku, since you're running this command from within your app.
Heroku enables you to fork existing applications in production. Use heroku fork to copy an existing application, including add-ons, config vars, and Heroku Postgres data.
Follow the instructions on Heroku: https://devcenter.heroku.com/articles/fork-app
Update for mid-2016...
Heroku now have a --fast flag when creating forks, however they will be up to 30 hours out-of-date.
$ heroku addons:create heroku-postgresql:standard-4 --fork HEROKU_POSTGRESQL_CHARCOAL --fast --app sushi
https://devcenter.heroku.com/articles/heroku-postgres-fork#fork-fast-option

Resources