DB reset after deploy to meteor servers - database

I re-deployed my app to meteor by using 'meteor deploy '
and my Database was reset.
Any clue why this happened or how I can avoid it in the future ?

When a meteor app is deployed, your data saved in local mongo would not deployed to the server. So you could use mongodump and mongorestore to solve it:(docs)
Now first dump your database somewhere
mongodump --host localhost:3001
Get your mongodb`s credentials by running (in your app dir):
meteor mongo myapp.meteor.com --url
This will give you database url in the form:
mongodb://username:password#host:port/databasename
With these info you could fill them into mongorestore (docs) and restore your local database over
mongorestore -u username -p password -h host:port -d databasename ~/desktop/location_of_your_mongodb_dump
All of your data would get transferred in this way. I wish it could help.

Related

MLAB / Heroku - mongorestore undefined

According to the Mlab & MongoDb documentation, inserting an existing local BSON collection into a new cloud collection should be fairly straight forward.
Import collection
mongorestore -h <host_url> -d <db_name> -u <user> -p <password> <input .bson file>
https://www.mlab.com/databases/your_db_name#tools
I've successfully connected to the mongo shell, but:
a) I'm unable to verify the OS used per https://docs.mongodb.com/guides/server/import/#check-your-environment - ls returns [native code]
b) Running the command for mongorestore returns a lot of missing ; before statement SyntaxErrors (which I am able to fix despite the strangeness)
c) Running the command returns mongorestore is not defined
Depressingly, i'll have to spin up an endpoint in the cloud and post all my docs from local to cloud via that method.
However if anyone knows what the issue here is? Why would mongorestore (and incidentally mongoimport) not be available?

Postgres copy Heroku Production DB to local development DB

I have a heroku database, d76mj7ltuqs.
I then have a local database, test_development.
The schema is the same on both of these databases - I want to pull all of the data from my production database and overwrite my local database, so that local is an exact replica of production at the time of pull.
How can I do that in Postgres?
Use heroku's "pg:pull":
You'll need to clear your local DB:
rake db:drop
Then collect some information from Heroku:
heroku pg:pull DATABASE_URL test_development
This will connect to the heroku DB, and copy it to the local database.
See Heroku's documentation on pg:pull for more details.
This command should do the work:
heroku pg:pull DATABASE_URL database-name --app heroku-app-name
clean your local database:
rake db:schema:load
dump your heroku database:
heroku pg:backups:capture -r <**your production git repo name**>
heroku pg:backups:download -r <**your production git repo name**>
load data in your local database
pg_restore --verbose --clean --no-acl --no-owner -h localhost -d <**test database name**> latest.dump
this is how i do it, be sure to gzip it as your database grows. also don't export the ACL as you likely don't have the same postgres user on heroku and local accounts. replace with your specific details.
pg_dump -h ec2-##-##-##-##.compute-1.amazonaws.com -p <port> -Fc --no-acl --no-owner -o -U <username> <databasename> | gzip > dumpfile.gz
#<Prompt for Password>
gunzip -c dumpfile.gz | pg_restore --verbose --clean --no-acl --no-owner -d test_development -U <local_username>
Use your terminal to make a local pg_dump and then either psql or pg_restore it into your local database.
Similar method can be found here.
If this is a Rails app, you can use the following script to overwrite your local database with the latest dump you've generated on Heroku. If you uncomment the line with heroku pg:backups capture, the script will generate a new snapshot on Heroku before downloading it to your machine.
Note that you shouldn't have to edit the script, since it reads all the configuration from your database.yml file.
#!/usr/bin/env ruby
require_relative '../config/environment'
# Uncomment the line below if you want to generate a new snapshot of the
# Heroku production database before downloading it to the local machine
# `heroku pg:backups capture`
database_dump_file_pathname = Tempfile.new('latest.dump').path
`heroku pg:backups:download --output #{database_dump_file_pathname}`
# Get database config fom database.yml file
database_config = YAML::load_file(Rails.root.join('config', 'database.yml'))
database_name = database_config['development']['database']
database_username = database_config['development']['username']
database_password = database_config['development']['password']
# Overwrite local database with dump
cmd_line_arguments = [
'--verbose',
'--clean',
'--no-acl',
'--no-owner',
'--host localhost',
"-U #{database_username}",
"-d #{database_name}",
database_dump_file_pathname
].join(' ')
`PGPASSWORD=#{database_password} pg_restore #{cmd_line_arguments}`
See the Heroku docs on downloading DB backups for details.

Remote database not empty

I'm trying to push my local database to Heroku using the following command:
heroku pg:push <local db name> DATABASE --app <heroku appname>
and I get:
env: psql: No such file or directory ! Remote database is not
empty. ! Please create a new database, or use heroku pg:reset
So I run heroku pg:reset which resets it but I then get the following when trying to push again therefore going around in circles:
env: psql: No such file or directory ! Remote database is not
empty. ! Please create a new database, or use heroku pg:reset
Any ideas on how to solve this?

How can I import a .sql file into my Heroku postgres database?

I have a backup sql file from another database that I want to import into Heroku's postgres database. How do you do that?
This is how you do it:
heroku pg:psql --app YOUR_APP_NAME_HERE < updates.sql
And if you want to restore your production into staging (assuming both are heroku postgres DBs):
heroku pgbackups:restore YOUR_STAGING_DATABASE_NAME `heroku pgbackups:url --app YOUR_PRODUCTION_APP_NAME` --app YOUR_STAGING_APP_NAME --confirm YOUR_STAGING_APP_NAME
Make sure to preserve the special single quotes around heroku pgbackups:url --app YOUR_PRODUCTION_APP_NAME.
HEROKU TOOLBELT UPDATE
Heroku has recently updated their toolbelt so the old commands are no longer valid (see this link for more info). Below is the new version of the restore command.
heroku pg:backups restore \
`heroku pg:backups public-url -a YOUR_PRODUCTION_APP_NAME` \
YOUR_STAGING_DATABASE_NAME \
--app YOUR_STAGING_APP_NAME \
--confirm YOUR_STAGING_APP_NAME
Making backup file:
pg_dump -U USERNAME DATABASE --no-owner --no-acl -f backup.sql
Restoring from sql file to heroku :
heroku pg:psql --app APPNAME < backup.sql
(Bonus) Deleting all tables from heroku app database (example):
heroku pg:reset --app APPNAME HEROKU_POSTGRESQL_ROSE
get DATABASE_URL from posgresql heroku panel (psql line)
Load the SQL into a local Postgres instance and make sure it's correct. Then dump the data using the directions here: https://devcenter.heroku.com/articles/heroku-postgres-import-export
Finally, upload the dump to a public web server (like S3) and restore to Heroku like this:
heroku pgbackups:restore DATABASE 'https://s3.amazonaws.com/me/items/3H0q/mydb.dump'
Django local db import on Heroku on windows
create backup
pg_dump -U postgres -d hawkishfinance > C:\Users\Fauzan\Projects\hawkishfinance.sql
dump on server
heroku pg:psql --app hawkishfinance < hawkishfinance.sql
You must add run in this command. It will work successfully!
heroku pg:psql run --app YOUR_APP_NAME_HERE < updates.sql

How can I attach a database to an app in Heroku?

I'm using Heroku's Postgres addon, and I created a new production database from the Heroku Postgres addon page.
I Didn't add it directly to my App using the Resources page of my App.
Now I want to attach this database to my App so it'll be recognized by the heroku pg command.
I'm able to use the database btw after setting the DATABASE_URL config var of my app to point to it, but heroku pg command doesn't recognize it yet.
Additional info: The previous database was Shared, and the new one is a Production.
Heroku add-ons may now be attached across applications and multiple times on a single app.
heroku addons:attach ADDON_NAME -a APP_NAME
Source: https://devcenter.heroku.com/changelog-items/646
To know the name of your addon, do:
heroku addons
Source: https://devcenter.heroku.com/articles/managing-add-ons
Did you add the database using the app-independent https://postgres.heroku.com/ site? Or did you just create a postgresql database in your Heroku control panel?
If you created your database on https://postgres.heroku.com/, you will not see the database via your heroku pg:info command. What you can do to add your database to your application, however, would be to:
Log into https://postgres.heroku.com/.
Click on the database you want to attach to your application.
Under 'Connection Settings', click the configuration button at the top right.
Then click the 'URL' option.
Copy your database URL, this should be something like "postgres://blah:blah#ec2-23-23-122-88.compute-1.amazonaws.com:5432/omg".
In your application, on the command line, run heroku config:set DATABASE_URL=postgres://blah:blah#ec2-23-23-122-88.compute-1.amazonaws.com:5432/omg
What we did there was assign your database to the DATABASE_URL environment variable in your application. This is the variable that's used by default when you provision databases locally to your application, so theoretically, assigning this value should work just fine for you.
To get your database that you created at https://postgres.heroku.com/ attached to your actual heroku app that you are working on you can't use any of the pg backup commands and as far as I can tell there is no supported Heroku way of attaching a database to a heroku app.
You can however create a backup of your database using pg_dump and then use pg_restore to populate your new database that is attached to your app:
pg_dump -i -h hostname -p 5432 -U username -F c -b -v -f "backup-filename" database_name
Once that is complete you can populate your new database with:
pg_restore -i -h new_hostname -p 5432 -U new_username -d new_database_name -v "same_backup_filename"
Even if you are upgrading from the "basic plan" to a the "crane plan" you still have to do a backup and restore, but since the db's are already attached to your app you have the advantage of using the heroku backup commands.

Resources