#!/usr/bin/perl

use strict;
use warnings;

use DBI;

require '/usr2/people/kkeller/Genomics/util/backupTablesList.pl';

# $dbuser must have reload privilege
my $dbuser='genomics';
# fill in $dbuser's password (maybe try to get from stdin
# in the future?)
my $dbpass='';

my $now=time;
my $backupdir="/usr2/people/gtl/genomics_test/backup-$now";

umask 0;
mkdir $backupdir;

my $dbh=DBI->connect('dbi:mysql:genomics_test',$dbuser,$dbpass)
	or die "couldn't connect";

my $sql='FLUSH TABLES';
# need to grant reload to genomics
$dbh->do($sql) or die "couldn't flush";

my $alltables=$dbh->selectcol_arrayref("SHOW TABLES") or die "couldn't get table list from server";

my $tables=backupTablesList() || $alltables;
my $excludeTables=excludeTablesList();

for my $table (@$tables)
{
	next if ( grep ( { $_ eq $table } @$excludeTables) );
	my $sqlbackup="backup table $table to '$backupdir'";
	print "$sqlbackup\n";
	$dbh->do($sqlbackup)
		or warn "couldn't backup $table: $!";
}

$dbh->disconnect;

print "backup of genomics_test completed successfully.\n";

