Abinash Koirala bio photo

Abinash Koirala

Full stack software engineer currently pursuing Masters of Science in Information Technology at Rensselaer Polytechnic Institute. Experienced in each stages SDLC of enterprise web application. Currently interested in Data Science and Analytics apart from Software Development.

Email LinkedIn Github Stackoverflow

‘pg_dump’ is a very useful tool to create backups of your postgresql database. It is very straightforward to use in normal linux servers. But in case of Openshift, especially if your app gear and database gear are different, it can be a little bit tricky. I have stumbled around many times to figure this out. Following approach has worked for me every time.

First we should get the ssh address of the postgress gear. For this we can use the ‘rhc app show’ command which lists the gears in your application and their ssh addresses.

rhc app show <app_name> --gears

This will give you a list of gears in your app along with their ssh address. Copy the ssh address for your postgres gear.

Now you need to ‘ssh’ into that gear using ‘rhc ssh’ command.

rhc ssh <ssh_address_of_postgres_gear>

You’ll be in the root directory of your gear. It is not a good idea to make a backup file in that directory. Every Openshift gears have a data directory which doesn’t get wiped out in every deployment(all other directories get wiped clean in each deployment). So we’ll use this data directory to create the backup file. First cd to the data dir.

cd /app-root/data

Now you can use ‘pg_dump’ command to create a backup.

pg_dump <database_name> > <backup_file_name>

After this if you want to download the backup to your local machine then you can do ‘scp’ using the ssh address that you got in the first step. First you need to logout from the server ‘Ctrl + c’ or ‘Ctrl + z’ will log you out from the ssh session. Before logging out from the server you need to copy the full path of the backup file. Usually ‘pwd’ command can give you the full path of the folder and after adding the filename to that path it will be the full path of the file. Now, you need to log out of the server and use scp from the terminal in your machine.

scp <ssh_address_of_postgres_gear>:<full_path_of_backup_file> <destination_path_in_local_machine>

This will download the backup file to the path that you entered as the destination.