Automate Data Dumps for PostgreSQL Databases Building your own backup utility doesn't have to be scary The Code [Discuss (1) | Link to this hack]
The Code
#!/usr/local/bin/python
# /usr/local/bin/pg2gz.py
# This script lists all PostgreSQL
# databases and pipes them separately
# through gzip into .gz files.
# INSTRUCTIONS
# 1. Review and edit line 1 to reflect the location
# of your python command file.
# 2. Redefine the save_dir variable (on line 22) to
# your backup directory.
# 3. To automate the backup process fully, consider
# scheduling the regular execution of this script
# using cron.
import os, string
# Redefine this variable to your backup directory.
# Be sure to include the slash at the end.
save_dir = '/mnt/backup/databases/'
# Rename all *.gz backup files to *.gz.old.
curr_files = os.listdir(save_dir)
for n in curr_files:
if n[len(n)-2:] = = 'gz':
os.popen('mv ' + save_dir + n + " " + save_dir + n + '.old')
else:
pass
# Vacuum all databases
os.popen('vacuumdb -a -f -z')
# 'psql -l' produces a list of PostgreSQL databases.
get_list = os.popen('psql -l').readlines( )
# Exclude header and footer lines.
db_list = get_list[3:-2]
# Extract database names from first element of each row.
for n in db_list:
n_row = string.split(n)
n_db = n_row[0]
# Pipe database dump through gzip
# into .gz files for all databases
# except template*.
if n_db = = 'template0':
pass
elif n_db = = 'template1':
pass
else:
os.popen('pg_dump ' + n_db + ' | gzip -c > ' + save_dir +
n_db + '.gz')
Apply this diff to rename old backups with a datestamp instead of 'old', to prevent them being overwritten:
18c18
< import os, string
---
> import os, string, time
28c28,29
< os.popen('mv ' + save_dir + n + " " + save_dir + n + '.old')
---
> t = time.strftime('%y%m%d', time.gmtime(os.stat(save_dir + n).st_atime))
> os.popen('mv ' + save_dir + n + " " + save_dir + n + '.' + t)
18c18
< import os, string
---
> import os, string, time
28c28,29
< os.popen('mv ' + save_dir + n + " " + save_dir + n + '.old')
---
> t = time.strftime('%y%m%d', time.gmtime(os.stat(save_dir + n).st_atime))
> os.popen('mv ' + save_dir + n + " " + save_dir + n + '.' + t)
Jeff