Re: I need to take metadata from a shell script.

From: Thom Brown <thombrown(at)gmail(dot)com>
To: Greg Smith <greg(at)2ndquadrant(dot)com>
Cc: Guillaume Lelarge <guillaume(at)lelarge(dot)info>, Scott Mead <scott(dot)mead(at)enterprisedb(dot)com>, Andy Colson <andy(at)squeakycode(dot)net>, Jaume Calm <jaume(at)isac(dot)cat>, PostgreSQL <pgsql-general(at)postgresql(dot)org>
Subject: Re: I need to take metadata from a shell script.
Date: 2010-05-12 12:29:16
Message-ID: AANLkTilrY6wBWtoMleRLZfal07ClrUDniIbA8b6Ps3_O@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 6 May 2010 21:15, Greg Smith <greg(at)2ndquadrant(dot)com> wrote:

> Guillaume Lelarge wrote:
>
>> And rather than checking the database name, I usually prefer to filter
>> with datallowconn.
>> It'll save template1 and postgres, but the script won't give me an error
>> on template0.
>>
>>
>
> Template databases are labeled as such, so this excludes both template0 and
> template1 without having to hardcode their names:
>
> query="select datname from pg_database where not datistemplate"
>
>
> However, if you can't connect to them, you can't dump them either! So you
> really need both those things:
>
>
> query="select datname from pg_database where not datistemplate and
> datallowconn"
> for line in `psql -At -c "$query" postgres`
>
> do
> pg_dump -f /home/backups/`date +\%Y\%m\%d`/"$line".sql "$line"
> done
>
>
> It's also a good idea to use "-At" instead of just "-t" when writing
> scripts that parse the output from psql, as I modified the above. If you
> look carefully you'll see that without the "-A", there is a space before the
> name of each line output. Doesn't matter in this case, but using the
> default "aligned" mode can cause problems in more complicated scripts that
> output more than one field.
>
>
>
I've actually started using this myself, and have set myself up following in
a cron job (as couldn't decide on a nice location for the script):

mkdir /tmp/`date +\%Y\%m\%d` && pg_dumpall -c | gzip > /tmp/`date
+\%Y\%m\%d`/FULL.sql.gz && query="select datname from pg_database where not
datistemplate and datallowconn;";for line in `psql -U postgres -At -c
"$query" postgres`;do pg_dump -U postgres "$line" | gzip > /tmp/`date
+\%Y\%m\%d`/"$line".sql.gz;done;scp -r /tmp/`date +\%Y\%m\%d`
username(at)my(dot)location:/backups/location/ ; rm -rf /tmp/`date +\%Y\%m\%d`

This creates a directory named in yyyyddmm format in /tmp, then backs up the
whole database cluster into a file called FULL.sql.gz, then each individual
database as databasename.sql.gz, then copies it off to a remote location,
then deletes them from /tmp.

Thom

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Rob Richardson 2010-05-12 12:55:32 Re: Weird unique constraint
Previous Message Grzegorz Jaśkiewicz 2010-05-12 10:39:46 Re: 8.3.7, 'cache lookup failed' for a table