pg_dumpall shell script in bash shell

pg_dumpall shell script in bash shell

Post by Fuzzygot » Wed, 20 Dec 2006 20:24:52

I am pretty much stumped atm and am hoping someone can shed some light
for me. I am
attempting to write a set of automated BASH Shell Scripts that tie in
with PostgreSQL and
PHP to manage our backups. There are a few teething troubles the main
one is that when
psql calls out the names of the databases to feed them into a shell
script array it feeds them
in as one value EG:

dbname1 dbname2 dbname3

etc and they are all stored within the array value $dbname[0] when I
want dbname1 stored
in $dbname[0] and dbname2 stored in $dbname[1] etc

In the original version I just defined all the databases manually in
the shell script as

DATABASES=(dbname1 dbname2 dbname3) and so on, this is actually still
running and
and pulls each dbname out as a member of a seperate array value as I'd
expected the stored
results of the psql query to do.

I am sure that i am making a stupid mistake somewhere
(which could be attempting to write something like this with my very
limited shell, psql and
bash skills) any help would be appreciated on this front. Any other
consructive comments
about my what I have written are welcome I am sure there will be many
(not to mention a
few shell scripters spinning in their graves) things to point out.

Many Thanks
David Phillips

# These are the variables for use with the backup.
# Details to connect to PSQL
CONNECT_PSQL="psql -d dbname -h serverName -U myname"

# The username to login to Postgres
# Password for the backup user
# Options for the Data backup
DATA_OPTIONS="-a --disable-triggers -v"
# Options for the Schema backup
SCHEMA_OPTIONS="-s --disable-triggers -v"
# The current host/server
# The current database the schemas are backed up from
CURRENT_DB="serverName "
# Email to send summery and log too

# dir path of the backups
# The logfile for the backup
# looping varuiable


echo -e "\n -- Begin The Backup Procedure $(date) -- \n"

DBS="$(psql -t -h marge -U $USER_AUTH $CURRENT_DB -c "SELECT dbname
FROM phillipsd.bu_database WHERE structure = 't'" | sed -e 's/\ //g' )"
for db in ${DBS[@]}

echo ${DBS[$LOOPVAR]}
echo ${DBS[0][0]}



pg_dumpall shell script in bash shell

Post by Chris F.A. » Thu, 21 Dec 2006 04:55:21

n 2006-12-19, Fuzzygoth wrote:

That does nothing, as you wipe it out in the next assignment.

If you had created an array, you would use:

for db in "${DBS[@]}"

However, you didn't create an array.

To create an array, use:

DBS=( $(psql -t -h marge -U $USER_AUTH $CURRENT_DB -c "SELECT dbname FROM phillipsd.bu_database WHERE structure = 't'" | sed -e 's/\ //g' ) )

Bash does not have two-dimensional arrays.

Chris F.A. Johnson, author | <>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence


pg_dumpall shell script in bash shell

Post by Fuzzygot » Thu, 21 Dec 2006 17:52:44

Thanks for your help, I would have had no idea how to specify the psql
an array, now i can get on to the rest of it :)

thanks again :)
David P