0

I have a ubuntu remote server say 172.123.342.12. I want to take backup of a postgresql database on my local machine via a python script.

My Script is:

def backUp(self):
        Pass = 'fb2024d4'
        os.putenv("PGPASSWORD",Pass)
        dt = datetime.now()
        format = "%Y_%b_%d"
        cur_time = dt.now()
        form_time = cur_time.strftime(format)
        backup_str = "C:\\Bitnami\\odoo-8.0-7\\postgresql\\bin\\pg_dump.exe --format=c -h 172.123.342.12 -p 5432 -d new_db -U bn_openerp > C:\\Users\\n\\Desktop\\Odoo_Backups\\%s.dump" %form_time
        os.system(backup_str)
        print ("Backup Created in Desktop")
        box.showinfo("Information", "Backup Created")

backup()

It does nothing. Some help will be appreciated.

EDIT: The Script works on a database on windows as i am using admin account. So it does not asks for password. But When i try to backup a database from remote ubuntu server. It asks for password. I have tried following solutions:

1.) SET PGPASSPASSWORD = C:\foo\bar..\pgpass.conf.
2.) os.putenv("PGPASSWORD","password")
3.) PGPASSWORD='password' pg_dump.exe -h localhost.....

No one worked for me.

Manish Gupta
  • 4,438
  • 18
  • 57
  • 104

1 Answers1

0

I was able to use a python script to create a dump file using pg_dump.exe:

filename = 'C:/Path/To/File/mydb_dump.sql'
pgDump = 'C:/Program Files/PostgeSQL/9.5/bin/pg_dump'

subprocess.Popen('"{}" -h 127.0.0.1 dbname > "{}"'.format(pgDump, filename), shell=True)

A few things to note:

I STRONGLY CAUTION AGAINST USING shell=True !!!

There is a huge security hazard with possible shell injections as per the documentation.

I'm not sure if will work with a remote Ubuntu server, but I couldn't see why not if all permissions and sharing is setup properly.



I know this is pretty old, but I hope it helps.

Community
  • 1
  • 1
Casey
  • 419
  • 5
  • 13