Schulung, Beratung und Entwicklung

Glück IT

Gesellschaft für IT Beratung, Schulung und Entwicklung

NEWS

Content Management System ®CMAssist

PostgreSQL Backup Skript

Mit pg_dumpall kann man einfach ein komplettes Backup aller PostgreSQL Datenbank machen. Mit pg_dump nur eine Datenbank, dafür aber beispielsweise im TAR Format so das man gezielt beispielsweise eine Tabelle wieder herstellen kann. Schön wäre eine Skript welches beide Mechanismen vereint.

Nachfolgendes Skript, ist eine Synergie zwischen pg_dump und pg_dumpall. Es legt ein Backup aller von PostgreSQL verwalteten Datenbanken, im TAR Format, mit hilfe von pg_dump an.

#!/bin/bash

BACKUP_DIR="/var/pg_backup"

PGHOST="localhost"
PGUSER="postgres"
PGDUMP="/usr/local/postgresql/8.1/bin/pg_dump"
PSQL="/usr/local/postgresql/8.1/bin/psql"

function pg_backup_database
{
  DB=$1
  $PGDUMP -C -Ft $DB | gzip -9 > $BACKUP_DIR/$DB.tar.gz
}

if [ -n "$1" ]; then
    pg_backup_database $1
else
    DB_LIST=`$PSQL -l -t |/usr/bin/cut -d'|' -f1 |/bin/sed -e 's/ //g'`
    for DB in $DB_LIST
    do
        if [ "$DB" != "template0" ] && [ "$DB" != "template1" ]; then
            echo `date +'%F %T'` PG Backup Datenbank $DB
            pg_backup_database $DB
        fi
    done
fi

Gegebenenfalls müssen noch die Pfade der Programme für Ihre Distribution angepasst werden. Dieses Skript läuft ohne Anpassung auf Debian Etch.