head	1.4;
access;
symbols;
locks; strict;
comment	@# @;


1.4
date	2001.07.10.22.14.01;	author stack;	state Exp;
branches;
next	1.3;

1.3
date	2001.07.10.21.53.48;	author stack;	state Exp;
branches;
next	1.2;

1.2
date	2001.07.10.19.15.02;	author ms;	state Exp;
branches;
next	1.1;

1.1
date	2001.07.10.16.36.42;	author stack;	state Exp;
branches;
next	;


desc
@@


1.4
log
@Clean up usage message.
@
text
@#!/usr/bin/perl -w
##
## Looks in the database pointed to by the sourcecast env.sh file for issues 
## and the tigris database.  Makes a dump of all it finds into a directory 
## named for this script w/ a date suffix in the '$::sandbox/tmp' directory.
##
## Be careful running this script; It will interfere w/ a running sites' 
## performance.  Use w/ caution.
##
## $Id: dump_tigris_db.pl,v 1.3 2001/07/10 21:53:48 stack Exp $
## 

## Constructor.
##
## Test that the SANDBOX env.sh has been sourced and that the sandbox
## makes sense. 
##
BEGIN
{
    if( ! $ENV{SANDBOX} )
    {
        die "ERROR: \$ENV{SANDBOX} undefined. Have you sourced env.sh?\n";
    }

    if( ! -d "$ENV{SANDBOX}" 
        || ! -f "$ENV{SANDBOX}/scripts/env.sh"
        || ! -x "$ENV{SANDBOX}/scripts/env_check" )
    {
        die "ERROR: $ENV{SANDBOX} does not point to a valid tigris install.\n";
    }

    my $error = `$ENV{SANDBOX}/scripts/env_check`;
    die "ERROR: $ENV{SANDBOX}/scripts/env_check failed: $error." if( $? );
}

## Destructor
##
# END { }


use Getopt::Long;
use File::Basename;
use File::Copy;
use Cwd;
use English;
use Carp;
use strict;

## Global variables
##
$::scriptName = basename( $0 );
$::sandbox = $ENV{SANDBOX};


## Program main
##
main( );

sub main
{
    my ($dumpDir, $DryRun) = cmdline_args( );
    unless( defined( $dumpDir ) )
    {
        my $tmpDir = makeTmpDirectory( );
        $dumpDir = "$tmpDir/dump.$$";
    }
    makeDirectory( $dumpDir );
    print "Dumping $ENV{DATABASE_HOST} databases to $dumpDir\n";

    # Get list of issues dbs.
    # TODO: Get rid of the pipe.
    #
    my $getIssuesDbsCommand = "echo 'show databases like \"issues_%\"'"
        . " | mysql --silent -u $ENV{DATABASE_ADMIN_USER}"
        . " -p$ENV{DATABASE_ADMIN_PASSWORD} --host=$ENV{DATABASE_HOST}"
        . " --port=$ENV{DATABASE_PORT}";
    my @@issues_dbs = split( /\n/, `$getIssuesDbsCommand` );
    
    # Dump each db in turn.
    #
    my $total_dump_size = 0;
    DBS: foreach my $db ( @@issues_dbs, $ENV{DATABASE_NAME} )
    {
        my ( $file_size, $file_contents ) = getDBDump( $db );
        $total_dump_size += $file_size;
        print "Dump file size for db '$db' is $file_size bytes\n";
        unless( $DryRun ) 
        {
            my $dbDumpFile = "$dumpDir/$db.sql";
            writeDBDump( $file_contents, $dbDumpFile );
            print "Dump file for db $db written\n";
        } 
    }
    print "Total size for all DB dumps is $total_dump_size bytes\n";
}

## Write db dump contents.
##
sub writeDBDump
{
    my ( $dbDumpContents, $dbDumpFile ) = @@_;
    if( open( DUMPOUT, ">$dbDumpFile" ) ) 
    {
        print DUMPOUT $dbDumpContents;
        close( DUMPOUT );
    } 
    else 
    {
        print STDERR "Unable to open file $dbDumpFile for writing: $!\n";
        exit 1;
    }
}

## Get database dump.
##
## Returns length and contents of db dump.
##
sub getDBDump
{
    my ( $db ) = @@_;
    my $file_size = 0;
    my $file_contents = "";
    my $dumpDbCommand = "mysqldump -u $ENV{DATABASE_ADMIN_USER}"
        . " -p$ENV{DATABASE_ADMIN_PASSWORD} --host=$ENV{DATABASE_HOST}" 
        . " --port=$ENV{DATABASE_PORT} --opt $db"; 
    unless( open( DUMPIN, " $dumpDbCommand |" ) ) 
    {
        print STDERR "Unable to fork \"$dumpDbCommand\": $!\n";
        exit 1;
    }
    else
    {
        while( <DUMPIN> ) 
        {
            $file_size += length( $_ );
            $file_contents .= $_;
        }

        # Check the output of the mysqldump command to make sure
        # that it was actually OK.
        #
        unless( close( DUMPIN ) ) 
        {
            print STDERR "Error running $dumpDbCommand: $!\n";
            exit 1;
        }
    }
    return ( $file_size, $file_contents );
}

## Make temporary directory for use by this script while running.
## 
## Returns name of directory we made.
##
sub makeTmpDirectory
{
    $::scriptName =~ m|(.*)\.[^\.]*$|; # Strip suffix.
    my $baseName = "${1}_output";
    makeDirectory( "$::sandbox/tmp" );
    return makeDirectory( "$::sandbox/tmp/$baseName" );
}

## Make a directory or fail in the attempt.
## Return name of directory we made (Convenience).
##
sub makeDirectory
{
    my( $directory ) = @@_;
    -d $directory 
        or mkdir( $directory, 0777 ) 
        or die "Failed making dir $directory: $!";
    return $directory;
}

## Process command-line arguments.
## Does not return if user asked for help. 
## Returns cmd-line processing results.
##
sub cmdline_args
{
    my $optHelp = undef;
    my $optDir = undef;
    my $optDryRun = undef;

    my $result = GetOptions( "h" => \$optHelp,
                                "help" => \$optHelp,
                                "n" => \$optDryRun,
                                "dry-run" => \$optDryRun,
                                "d=s" => \$optDir,
                                "dir=s" => \$optDir );        

    $result || die "GetOptions error.";

    if( $#ARGV > -1 )
    {
        print "ERROR: Unexpected arguments: @@ARGV.\n";
        usage( );
    }

    usage( ) if( defined( $optHelp ) );

    return ( $optDir , $optDryRun );
}


## Usage
##
sub usage
{
    
    print <<"SCRIPT_USAGE";
Usage: $::scriptName [-h|--help] [-n,--dry-run] [-d DUMP_DIR|--dir=DUMP_DIR]

Options:
 -h,--help                  Prints this message
 -n,--dry-run               Preview-only, calculate total space required.
 -d DUMP_DIR,--dir=DUMP_DIR Where to dump database files to.
                            Default is under SANDBOX/tmp.

SCRIPT_USAGE
    
    exit( 0 );
}
@


1.3
log
@Added a 'dry-run' option that prints out size of the db dump.
Submitted by: Andrew Ryan
@
text
@d10 1
a10 1
## $Id: dump_tigris_db.pl,v 1.2 2001/07/10 19:15:02 ms Exp $
d212 1
a212 1
Usage: $::scriptName [-h|--help] [-d DUMP_DIR|--dir=DUMP_DIR]
d216 1
a216 1
 -n,--dry-run               Preview-only, calculate total space required
@


1.2
log
@tidbit change to error message
@
text
@d10 1
a10 1
## $Id: dump_tigris_db.pl,v 1.1 2001/07/10 16:36:42 stack Exp $
d13 1
a13 1
## Contructor.
d61 1
a61 1
    my $dumpDir = cmdline_args( );
d79 45
a123 1
    my $dumpCommandPrefix = "mysqldump -u $ENV{DATABASE_ADMIN_USER}"
d125 7
a131 2
        . " --port=$ENV{DATABASE_PORT} --opt "; 
    DBS: foreach my $db ( @@issues_dbs, $ENV{DATABASE_NAME} )
d133 8
a140 1
        # TODO: Fix so we get rid of the redirect of output (Get rid of backticks).
d142 5
a146 1
        `$dumpCommandPrefix $db > $dumpDir/$db.sql`;
d148 1
d183 1
d187 2
d202 1
a202 1
    return ( $optDir );
d216 1
@


1.1
log
@Convert '.sh' script to '.pl'.  Add in ability to choose where you want
the database dumped to.
@
text
@d10 1
a10 1
## $Id: dump_tigris_db.sh,v 1.4 2001/06/01 16:53:22 stack Exp $
d22 1
a22 1
        die "ERROR: \$ENV{SANDBOX} undefined. Have you sourced env.sh.\n";
@

