These scripts were just written using vi and uses the bourne shell.
It relies on oid2name that comes with the distribution of postgresql and you'll obviously need to change the paths so I've just used YOURPATH so you can modify it easily yourself. I use the first script to get a snapshot of the database cluster just before a backup of the server is done so that I know exactly what date and time it was done and so i know that it has not been changed during the backup time. I call it like this from the cron:
min hr * * * /YOURPATH/dumpdb.sh >> /logs/cron.log 2>&1
#!/bin/sh
#
# Purpose: This script creates a dump of the full database cluster
# into one file and then proceed to dump individual databases
# into one directory - /PATH/dumpdir
#
# create variables
USER="postgres"
NAMECMD="/YOURPATH/oid2name -U $USER"
TMPDUMP="/YOURPATH/dump.tmp"
DUMPDAT="/YOURPATH/dump.dat"
DUMPDIR="/YOURPATH/dumpdir"
PGPASSWORD="*****" # You'll need to change this
PATH=$PATH:/YOURPATH/bin
export PGPASSWORD PATH
This next script would only need to be used if you're databases became corrupted and you needed to get rid of them all before using the output file from pg_dumpall to do a full restore. You may find another use.
echo "Are you sure you want to drop all databases? [y/n]: \c"
read ANSWER
if [ $ANSWER != "y" ] && [ $ANSWER != "ye" ] && [ $ANSWER != "yes" ] && [ $ANSWER != "Y" ] && [ $ANSWER != "YE" ] && [ $ANSWER != "YES" ]; then
echo ""
echo "Exiting, no databases have been affected."
exit 0
fi
# Get database names and put into .tmp file
$NAMECMD | cut -b10- > $TMPDUMP
# Get rid of unwanted stuff in .dat file by filtering out.
while read LineIn; do
CH=`echo $LineIn | cut -c1`
if [ "$CH" != "" ] && [ "$CH" != "-" ]; then
if [ "$LineIn" != "template0" ] && [ "$LineIn" != "template1" ]
&& [ "$LineIn" != "ases:" ]; then
echo $LineIn >> $DUMPDAT
fi
fi
done<$TMPDUMP
# Drop all the databases
if [ -f $DUMPDAT ]; then
echo ""
echo "Continuing to drop all databases..."
while read LineIn; do
echo "Dropping database - $LineIn..."
dropdb -U postgres $LineIn
done<$DUMPDAT
else
echo ""
echo "No databases exist, exiting..."
exit 0
fi
So there you have it. I hope you find these helpful. I don't really talk to other Postgres users so I'm not too sure how useful they would be.
As you can see you'll have to edit it slightly but only the variables at the top of each script. Thinking about it I should have seperated it up into functions or procs... that'lll be my next task.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.