如何在Postgres中替换pg_dumpall的--D标志?

时间:2015-06-29 19:19:00

标签: postgresql database-backups pg-dump

我尝试使用this answer创建PostgreSQL备份脚本作为我脚本的基础。脚本是:

#! /bin/bash

# backup-postgresql.sh
# by Craig Sanders 
# this script is public domain.  feel free to use or modify as you like.

DUMPALL="/usr/bin/pg_dumpall"
PGDUMP="/usr/bin/pg_dump"
PSQL="/usr/bin/psql"

# directory to save backups in, must be rwx by postgres user
BASE_DIR="/var/backups/postgres"
YMD=$(date "+%Y-%m-%d")
DIR="$BASE_DIR/$YMD"
mkdir -p $DIR
cd $DIR

# get list of databases in system , exclude the tempate dbs
DBS=$($PSQL -l -t | egrep -v 'template[01]' | awk '{print $1}')

# first dump entire postgres database, including pg_shadow etc.
$DUMPALL -D | gzip -9 > "$DIR/db.out.gz"

# next dump globals (roles and tablespaces) only
$DUMPALL -g | gzip -9 > "$DIR/globals.gz"

# now loop through each individual database and backup the schema and data separately
for database in $DBS; do
    SCHEMA=$DIR/$database.schema.gz
    DATA=$DIR/$database.data.gz

    # export data from postgres databases to plain text
    $PGDUMP -C -c -s $database | gzip -9 > $SCHEMA

    # dump data
    $PGDUMP -a $database | gzip -9 > $DATA
done

该行:

$DUMPALL -D | gzip -9 > "$DIR/db.out.gz"

正在返回此错误:

  

psql:致命:角色" root"不存在   /usr/lib/postgresql/9.3/bin/pg_dumpall:无效选项 - ' D'

当我查看PostgreSQL docs时,似乎不再是-D选项了。更新的命令应该是什么样的?

1 个答案:

答案 0 :(得分:1)

这是我最终用于定期备份PostgreSQL数据库的修改过的脚本:

#! /bin/bash

# backup-postgresql.sh
# by Craig Sanders 
# this script is public domain.  feel free to use or modify as you like.

DUMPALL="/usr/bin/pg_dumpall"
PGDUMP="/usr/bin/pg_dump"
PSQL="/usr/bin/psql"

# directory to save backups in, must be rwx by postgres user
BASE_DIR="/var/backups/postgres"
YMD=$(date "+%Y-%m-%d")
DIR="$BASE_DIR/$YMD"
mkdir -p $DIR
cd $DIR

# get list of databases in system , exclude the tempate dbs
DBS=$($PSQL -l -t | egrep -v 'template[01]' | awk '{print $1}' | egrep -v '^\|' | egrep -v '^$')

# first dump entire postgres database, including pg_shadow etc.
$DUMPALL -c -f "$DIR/db.out"

# next dump globals (roles and tablespaces) only
$DUMPALL -g -f "$DIR/globals"

# now loop through each individual database and backup the schema and data separately
for database in $DBS; do
    SCHEMA=$DIR/$database.schema
    DATA=$DIR/$database.data

    # export data from postgres databases to plain text
    $PGDUMP -C -c -s $database -f $SCHEMA

    # dump data
    $PGDUMP -a $database -f $DATA
done

# delete backup files older than 30 days
OLD=$(find $BASE_DIR -type d -mtime +30)
if [ -n "$OLD" ] ; then
        echo deleting old backup files: $OLD
        echo $OLD | xargs rm -rfv
fi