From a311650def099a0bf90ec358209ea591ee68a753 Mon Sep 17 00:00:00 2001 From: Lukas Mueller Date: Wed, 11 Jan 2012 12:49:50 -0500 Subject: [PATCH] an improved database test load script. Test loads all databases specified in a conf file. --- test-load-db.sh | 19 ++++++++++++++++ test_load_dbs.pl | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100755 test-load-db.sh create mode 100755 test_load_dbs.pl diff --git a/test-load-db.sh b/test-load-db.sh new file mode 100755 index 0000000..77b73de --- /dev/null +++ b/test-load-db.sh @@ -0,0 +1,19 @@ +#!/bin/sh + +# test if database exists +echo "Checking if database exists..." +sudo -u postgres psql -l | grep -q cxgn_daily_build && sudo -u postgres dropdb -U postgres cxgn_daily_build + +### deanx - Oct 02 2007 - originally the createdb statement was part of the DB load +# below. But I found that caused login/sudo confusion and failed. now +# the createdb is simply a seperate step +echo "Create new database for the daily build..." +sudo -u postgres createdb -E SQL_ASCII cxgn_daily_build +### + +echo "Loading database from dump (this may take a while...)" +# reload the database from file given as parameter +{ zcat -f "$1" ; echo COMMIT ; } | + sudo -u postgres psql -U postgres --echo-all --variable AUTOCOMMIT=off --variable ON_ERROR_STOP=t \ + --dbname cxgn_daily_build > ${2:-/tmp/cxgn_daily_build_creation_log} 2>&1 || \ + { dropdb -U postgres cxgn_daily_build; echo Database load failed! > /dev/stderr ; exit 1; } diff --git a/test_load_dbs.pl b/test_load_dbs.pl new file mode 100755 index 0000000..7125de8 --- /dev/null +++ b/test_load_dbs.pl @@ -0,0 +1,69 @@ +#!/usr/bin/perl + +=head1 NAME + +test_load_dbs.pl - test load dbs on a redundant server, to be run by cron + +=head1 DESCRIPTION + +test_load_dbs.pl test loads the databases specified in a conf file, test_load_dbs.conf. + +The conf file contains two columns of paths to database dumps to be restored and the name of the database to be given in the redundant server. + +=head1 AUTHOR + +Lukas Mueller + +=cut + +use strict; + +my $conf = shift || "/root/test_load_dbs.conf"; + +open(my $F, "<", $conf); + +while (<$F>) { + chomp; + my ($dbdump, $dbname) = split /\s+/; + + if (! -e $dbdump || -s $dbdump == 0) { + print "The file $dbdump does not exist of is empty. NOT TEST LOADING $dbdump!!!\n"; + next; + } + else { + backup_db($dbdump, $dbname); + } + +} + + +sub backup_db { + my $dbdump = shift; + my $dbname = shift; + +# test if database exists + + + $ENV{DBDUMP} = $dbdump; + $ENV{DBNAME} = $dbname; + + ##system('echo DBDUMP = $DBDUMP'); + ##system('echo DBNAME = $DBNAME'); + + print "Checking if database $dbname exists, and removing it...\n"; + + system('sudo -u postgres psql -l | grep -q $DBNAME && sudo -u postgres dropdb -U postgres $DBNAME'); + +### deanx - Oct 02 2007 - originally the createdb statement was part of the DB load +# below. But I found that caused login/sudo confusion and failed. now +# the createdb is simply a seperate step + print "Create new database for the daily build of $dbdump...\n"; + system('sudo -u postgres createdb -E SQL_ASCII $DBNAME'); +### + + print "Loading database from dump (this may take a while...)\n"; +# reload the database from file given as parameter + system('time { zcat -f "$DBDUMP" ; echo COMMIT ; } | sudo -u postgres psql -U postgres --echo-all --variable AUTOCOMMIT=off --variable ON_ERROR_STOP=t --dbname $DBNAME > ${2:-/tmp/build_creation_log\_$DBNAME} 2>&1 || { sudo -u postgres dropdb -U postgres $DBNAME; echo Database load failed! > /dev/stderr ; exit 1; } '); + + print "Done with db $dbname.\n"; +} -- 2.11.4.GIT