[LON-CAPA-cvs] cvs: loncom /debugging_tools archive_coursedata_tables.pl

matthew lon-capa-cvs@mail.lon-capa.org
Thu, 30 Dec 2004 22:04:29 -0000


matthew		Thu Dec 30 17:04:29 2004 EDT

  Added files:                 
    /loncom/debugging_tools	archive_coursedata_tables.pl 
  Log:
  Small script to backup/restore loncoursedata tables.  Stores tables in
   debug/sql_backups.
  
  

Index: loncom/debugging_tools/archive_coursedata_tables.pl
+++ loncom/debugging_tools/archive_coursedata_tables.pl
#!/usr/bin/perl -w
#
# The LearningOnline Network
#
# $Id: archive_coursedata_tables.pl,v 1.1 2004/12/30 22:04:29 matthew Exp $
#
# Copyright Michigan State University Board of Trustees
#
# This file is part of the LearningOnline Network with CAPA (LON-CAPA).
#
# LON-CAPA is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# LON-CAPA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LON-CAPA; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# /home/httpd/html/adm/gpl.txt
#
# http://www.lon-capa.org/
#
use strict;
use Getopt::Long();
use lib '/home/httpd/lib/perl/';
use LONCAPA::Configuration();

#
# Determine parameters
my ($help,$restore,$version);
&Getopt::Long::GetOptions("help"          => \$help,
                          "restore"       => \$restore);
if ($help || ! scalar(@ARGV)) {
    print<<USAGE;
archive_class.pl
   Store the MySQL tables associated with a given class to a backup file.
   The gzipped backup file contains the MySQL commands needed to recreate the 
   coursedata tables.
Parameters:
   course       Multiple allowed    LON-CAPA course id
   restore      optional            if present, restore the tables from the
                                    backup file, overwriting the current data.
   help         optional            Print this help and exit
Examples:
  archive_coursedata_tables.pl sfu_39251c2fbb33f47sful3 msu_12340987139587msul4
  archive_coursedata_tables.pl -restore sfu_39251c2fbb33f47sful3 
USAGE
    exit;
}
##
my %perlvar = %{&LONCAPA::Configuration::read_conf('loncapa.conf')};
my $targetdir = $perlvar{'lonDaemons'}.'/debug/sql_backups';
if (! -d $targetdir) {
    if (! mkdir($targetdir)) {
        print "Unable to create directory to store backups.".$/.
            $targetdir.$/.
            "Aborting".$/;
        exit;
    }
}

while (my $course = shift(@ARGV)) {
    my $targetfile = $targetdir.'/'.$course.'_coursedata.sql.gz';
    #
    my @tables;
    foreach my $suffix ('symb','part','student','performance','parameters',
                        'partdata','responsedata','timestampdata','weight') {
        push(@tables,$course.'_'.$suffix);
    }
    #
    if ($restore) {
        print "Restoring from ".$targetfile.$/;
        &load_backup_tables($targetfile);
    } else {
        print "Backing up to ".$targetfile.$/;
        &backup_tables($targetfile,\@tables);
    }
}
exit;

##############################################################
##############################################################
sub backup_tables {
    my ($gz_sql_filename,$tables) = @_;
    my $command = qq{mysqldump --opt loncapa };
    foreach my $table (@$tables) {
        $command .= $table.' ';
    }
    $command .= '| gzip >'.$gz_sql_filename;
    system($command);
}

##
## Load in mysqldumped files
##
sub load_backup_tables {
    my ($gz_sql_filename) = @_;
    if (-s $gz_sql_filename) {
        my $command='gzip -dc '.$gz_sql_filename.' | mysql --database=loncapa';
        system($command);
    }
}