minor adjustment to program output.
[sgn-devtools.git] / test_load_dbs.pl
blob97e2f02d5a16495919f5bfa742e20c08f5400dd5
1 #!/usr/bin/perl
3 =head1 NAME
5 test_load_dbs.pl - test load dbs on a redundant server, to be run by cron
7 =head1 DESCRIPTION
9 test_load_dbs.pl test loads the databases specified in a conf file, test_load_dbs.conf.
11 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.
13 =head1 AUTHOR
15 Lukas Mueller <lam87@cornell.edu>
17 =cut
19 use strict;
21 my $conf = shift || "/root/test_load_dbs.conf";
23 open(my $F, "<", $conf);
25 while (<$F>) {
26 chomp;
27 my ($dbdump, $dbname) = split /\s+/;
29 print "Processing $dbname from dump file $dbdump\n";
30 if (! -e $dbdump || -s $dbdump == 0) {
31 print "The file $dbdump does not exist of is empty. NOT TEST LOADING $dbdump!!!\n";
32 next;
34 else {
35 backup_db($dbdump, $dbname);
37 print "Completed $dbname.\n\n\n";
40 print "Completed conf file $conf\n. Have a nice day!\n\n";
42 sub backup_db {
43 my $dbdump = shift;
44 my $dbname = shift;
46 # test if database exists
49 $ENV{DBDUMP} = $dbdump;
50 $ENV{DBNAME} = $dbname;
52 ##system('echo DBDUMP = $DBDUMP');
53 ##system('echo DBNAME = $DBNAME');
55 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
56 # below. But I found that caused login/sudo confusion and failed. now
57 # the createdb is simply a seperate step
58 # print "Create new database for the daily build of $dbdump...\n";
59 system('sudo -u postgres createdb -E SQL_ASCII $DBNAME');
60 ###
62 print "Loading database from dump (this may take a while...)\n";
63 # reload the database from file given as parameter
64 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; } ');