Backup all Odoo/PostgreSQL databases using Python script

I have made this simple python script to dump all PostgreSQL database. I am using this script for my Odoo PostgreSQL databases. & I often use this script when I am re-installing the OS. In my Development environment, I have databases for Odoo v6 to v11 and master. to back up those databases is a time-consuming process if I do it manually.

Here are the steps to run this script

Start the psql prompt in the terminal

1. switch to the postgres user

sudo su postgres

2. start PSQL prompt

psql

3. get the list of databases using below query.

SELECT datname FROM pg_database;

4. copy the database names and paste them into the text file. (if you wanted to remove any database you can remove them from the file.)

5. copy the script below and replace the file path.

import os

lines = [line.strip() for line in open('<<txt file path>>')]
for i in lines:
    print os.system("pg_dump %s > <<db out directory>>/%s.sql" % (i,i))

6. run the script using python. it will dump the listed database and save them to <<db outdirectory>>.

hope this will save a lot of your time.

 

 

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.