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

#!/bin/sh
# 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
USER_AUTH="userlog"
# Password for the backup user
PASS_AUTH=""
# 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
CURRENT_HOST="marge"
# The current database the schemas are backed up from
CURRENT_DB="serverName "
# Email to send summery and log too
EMAIL=" XXXX@XXXXX.COM "

# dir path of the backups
SERVERPATH="backup"
# The logfile for the backup
LOGFILE="backup_log.txt"
# looping varuiable
LOOPVAR="0"

DBS="0"

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[@]}
do

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


BACKUP_PATH=${SERVERPATH}/${DBS[$LOOPVAR]}/${DBS[$LOOPVAR]}_db.sql
echo $BACKUP_PATH

pg_dumpall ${SCEMA_OPTIONS} -h ${CURRENT_HOST} -U ${USER_AUTH}
| $BACKUP_PATH
LOOPVAR=$(( LOOPVAR +1 ))
done
&> "$LOGFILE"
 
 
 

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 | <http://cfaj.freeshell.org>
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
into
an array, now i can get on to the rest of it :)

thanks again :)
David P