script to download heroku postgres DB dump to local server - ruby-on-rails

I would like to create a script that would make a dump of a postgresql DB on heroku and download it to my local server.
I using windows server 2008 R2 and would assume that this would be activated with scheduler.
On the local server installed is ruby 1.93 and chocolately (run curl on a PC).
I am assuming that the script would be a ruby file and have the commands to both create a backup and and then use a curl command to download it. The latest backup would be the only one downloaded
The commands would be something like
heroku pgbackups:capture --expire -a appname
curl -o latest.dump heroku pgbackups:url
thanks in advance

The easiest way would be to get curl for your Windows machine at http://curl.haxx.se

Related

Is there a way to open a Heroku Postgres DB backup in a gui to explore the data?

Last night I was asked to take down a client's website and close out their accounts as they are going out of business. I made sure to go to the Heroku Dashboard, go into the Heroku Postgres dashboard, Durability > Create Manual Backup.
I then downloaded the back up to my computer just in case something came up.
Once I had the backup downloaded, I deleted everything off of Heroku.
Well sure enough they messaged me today because they wanted to look at some data. However I closed the project completely. Now I'm left with this backup file format extension, so I don't know what to do with it. Something like: "1c02eb4e-d4ac-4631-9b61-742e9ea42659"
I have Postgres installed on my dev machine and I still have a dev version of the Ruby on Rails project set up. Is there a Heroku CLI command I can use to replace my dev database with this backup? Or is there a GUI program I can open this up in and write some queries? I'm on Mac Mojave.
Here is the command to load the dump file on development
pg_restore --verbose --clean --no-acl --no-owner -h localhost -U [username] -d [database name] infile
Relevant documentation: https://devcenter.heroku.com/articles/heroku-postgres-import-export#restore-to-local-database

Cannot connect via heroku pg:psql (no response) [Windows]

In GitBash:
$ heroku pg:psql
--> Connecting to postgresql-rectangular-74530
And no response is given... it's just kept there
If I type, for example:
$ heroku pg:psql
That works great. (I've already set my HEROKU_APP enviroment variable, that's why I'm not passing it as a parameter.)
If I only type psql, like this:
$ psql
psql: FATAL: role "MyPCUsername" does not exist
An error is thrown... I don't if it has something to do.
I'm on Windows 7 64bits.
Any suggestion?
Run heroku pg:psql in windows command prompt, for some reason not all heroku commands work properly in git bash
Only "solution" I have is to access with pgAdmin as proposed here
Or install Ubuntu at Virtual Box and accessing via heroku pg:psql. Works like a charm in Linux

How to backup of deploy rails application from server?

Hi I have a ruby on rails app hosted on AWS EC2 and it is using mysql3 as database.Now I have to take backup of the database to my local machine.
There are two ways to take backup.
Using mysql workbench UI tool connect you database via ssh tunnel.
Connect to the AWS EC2 and take backup there itself and copy the backup file using scp command.
Hope this might help you.
I did the same with a DigitalOcean application with PostgreSql, to do that, this is what I did, for that you will need a ssh connection, everywhere (DigitalOcean... and probably Amazon) explains how to do that
In the server (AWS in you case):
make a cron to execute daily a script to make a backup of the database
crontrb -e
and add, to perform a copy every day at 23:00
23 * * * sh /home/rails/backup/backup_dump.sh
create /home/rails/backup/backup_dump.sh:
NOW=$(date +"%d")
FILE="app_production_$NOW.sql"
pg_dump -U rails -w app_production > /home/rails/backup/$FILE
of course pg_dump is what I use to make a backup of my PostgreSql database, in you case with MySQL will need another
In your local machine:
Add in /etc/cron.daily directory the script file that contains the recover from AWS backup file, and populate:
NOW=$(date +"%d") # date - 1... the day before, don't remeber the script sintax
scp -r root#ip_server:/home/rails/backup/app_production_$NOW.sql /local_machine/user/local_backups
And that's all, I hope will help you

Download PG database as a file from Heroku

I'm trying to download a file (a copy) of our staging DB. I'm using pgbackups following these docs.
I created a backup, than got the public-url using heroku pgbackups:url b462 --app staging-appname, which worked.
Than I run the command curl -o latest.dump heroku pg:backups <public-url> b462 -a staging-appname and get curl: no URL specified!
Where it says <public-url> I've copied the actual long public-url, as in https://s3.amazonaws.com/reallly-long-url-with-acess-key-ect.
What am I missing here?
The following will create the backup:
heroku pg:backups capture --app name-of-the-app
Using the following information printed in terminal
---backup---> b001
In the following command
heroku pg:backups public-url b001 -a name-of-the-app
Will give you the url of the dump
Try just the following to get the latest file immediately after running heroku pgbackups:capture --app=staging-appname:
curl -o latest.dump `heroku pgbackups:url --app=staging-appname`
The b.. is required only if you are looking for a specific older file.
I was also struggling with this doc, and Prakash's suggestion didn't work for me (error complained about not being able to resolve 'heroku' which isn't part of the URL, so the syntax is seems to be bad, at least for the version of the pg or pgbackup gem I'm using.)
I also tried wget, as suggested in the heroku doc, to no avail.
When I'm stumped I try to think it down to the most simple elements, which led me to a far simpler solution than is provided by the doc...
I used the command in Rafael's answer to generate a temporary URL to retrieve the dump file:
heroku pg:backups public-url b001 -a name-of-the-app
Then, instead of plugging the resulting URL into the curl command, I just pasted that URL into Chrome's address bar and downloaded the file using my web browser.
The download was successful, the db file is valid.
With a little more sugar, here an example of shell script to download mysql dump from heroku and to restore it as a local database
backup_name=$1
if [ -z "$backup_name" ]
then
echo "You must supplied heroku backup name (ex: b001)"
return
fi
echo "Loading '$backup_name' database from heroku"
heroku pg:backups public-url $backup_name -a your-app-name > last_url.txt
curl -o latest.dump `cat last_url.txt`
dropdb local-db-name
createdb local-db-name
pg_restore -c -d local-db-name latest.dump
You need to give the heroku backup name as input.

Grabbing mysql dump file from remote server

Im working as an intern and am new to rails and it's production evn. I was wondering how I could grab a database dump from a remote server and import into my local database so that my local env mirrors that of the live version of a site. I have access to the database, and I have the current version of the code in my environment. I am missing the pictures and files attached to the site, and need it to make changes locally.
In the production server execute the following command
mysqldump -u username -ppassword db_name > production_dump.sql
scp the production_dump.sql file to your local machine
In your local machine execute the following command.
mysql -u username -ppassword db_name < production_dump.sql

Resources