File:  [LON-CAPA] / loncom / debugging_tools / archive_coursedata_tables.pl
Revision 1.1: download - view: text, annotated - select for diffs
Thu Dec 30 22:04:29 2004 UTC (19 years, 3 months ago) by matthew
Branches: MAIN
CVS tags: version_2_9_X, version_2_9_99_0, version_2_9_1, version_2_9_0, version_2_8_X, version_2_8_99_1, version_2_8_99_0, version_2_8_2, version_2_8_1, version_2_8_0, version_2_7_X, version_2_7_99_1, version_2_7_99_0, version_2_7_1, version_2_7_0, version_2_6_X, version_2_6_99_1, version_2_6_99_0, version_2_6_3, version_2_6_2, version_2_6_1, version_2_6_0, version_2_5_X, version_2_5_99_1, version_2_5_99_0, version_2_5_2, version_2_5_1, version_2_5_0, version_2_4_X, version_2_4_99_0, version_2_4_2, version_2_4_1, version_2_4_0, version_2_3_X, version_2_3_99_0, version_2_3_2, version_2_3_1, version_2_3_0, version_2_2_X, version_2_2_99_1, version_2_2_99_0, version_2_2_2, version_2_2_1, version_2_2_0, version_2_1_X, version_2_1_99_3, version_2_1_99_2, version_2_1_99_1, version_2_1_99_0, version_2_1_3, version_2_1_2, version_2_1_1, version_2_1_0, version_2_11_0_RC3, version_2_11_0_RC2, version_2_11_0_RC1, version_2_10_X, version_2_10_1, version_2_10_0_RC2, version_2_10_0_RC1, version_2_10_0, version_2_0_X, version_2_0_99_1, version_2_0_2, version_2_0_1, version_2_0_0, version_1_99_3, version_1_99_2, version_1_99_1_tmcc, version_1_99_1, version_1_99_0_tmcc, version_1_99_0, loncapaMITrelate_1, language_hyphenation_merge, language_hyphenation, bz6209-base, bz6209, bz5969, bz2851, PRINT_INCOMPLETE_base, PRINT_INCOMPLETE, HEAD, GCI_3, GCI_2, GCI_1, BZ5971-printing-apage, BZ5434-fox, BZ4492-merge, BZ4492-feature_horizontal_radioresponse, BZ4492-feature_Support_horizontal_radioresponse, BZ4492-Support_horizontal_radioresponse
Small script to backup/restore loncoursedata tables.  Stores tables in
 debug/sql_backups.

#!/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);
    }
}

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>