This script has been deprecated in favour of Cella.

Pgbackup is a PostgreSQL database server backup BASH script. The script is very basic, you can either call it and grab all databases, or call it specifying one database. The output directory (BACKUP_DIR), user name (PGUSER) and paths to the PostgreSQL commands must be specified in the script, sorry.

Using pgbackup

The pgbackup script can be called in two different ways:

  # pgbackup
  # pgbackup [database_name]

The first call will backup all database on the local PostgreSQL server, template0 and template1 databases are not archived. The second method will backup only the specified database. The backup files are named [database_name].pgd, blobs are dumped into this file as well.

The Script

Below is the script, you can copy and paste it into your favourite editor, and save it. The code has been wrapped at 76 characters so it shows up nicely here, hopefully it prints OK too.

#!/bin/bash

BACKUP_DIR="/home/edoceo/bak"

PGHOST="/tmp"
PGUSER="postgres"

function pg_backup_database
{
  DB=$1
  /usr/bin/pg_dump -bv -f $BACKUP_DIR/$DB.pgd -Fc $DB"
}

if [ -n "$1" ]; then
  pg_backup_database $1
else
  DB_LIST=`/usr/bin/psql -l -t |/bin/cut -d'|' -f1 |/bin/sed -e 's/ //g'`
  for DB in $DB_LIST
  do
    if [ "$DB" != "template0" ] && [ "$DB" != "template1" ]; then
      pg_backup_database $DB
    fi
  done
fi