Welcome, guest | Sign In | My Account | Store | Cart

Simple script to backup some databases from PostgreSQL on unix\linux.

Python, 24 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/env python
import os
import time

username = 'root'
defaultdb = 'postgres'
port = '5433'
backupdir='/www/backup/'
date = time.strftime('%Y-%m-%d')

#GET DB NAMES
get_db_names="psql -U%s -d%s -p%s --tuples-only -c '\l' | awk -F\| '{ print $1 }' | grep -E -v '(template0|template1|^$)'" % (username, defaultdb, port)

#MAKE BACKUP OF SYSTEMGRANTS
os.popen("pg_dumpall -p%s -g|gzip -9 -c > %s/system.%s.gz" % (port, backupdir, date))

#MAKING DB BACKUP
for base in os.popen(get_db_names).readlines():
        base = base.strip()
        fulldir = backupdir + base
        if not os.path.exists(fulldir):
                os.mkdir(fulldir)
        filename = "%s/%s-%s.sql" % (fulldir, base, date)
        os.popen("nice -n 19 pg_dump -C -F c -U%s -p%s %s > %s" % (username, port, base, filename))

1 comment

enzo cocca 8 years, 4 months ago  # | flag

hi,

very great! but how can i to do run this code on win?

thanks!