--- loncom/metadata_database/parse_activity_log.pl 2004/08/25 15:55:17 1.4 +++ loncom/metadata_database/parse_activity_log.pl 2004/12/16 15:03:29 1.6 @@ -2,7 +2,7 @@ # # The LearningOnline Network # -# $Id: parse_activity_log.pl,v 1.4 2004/08/25 15:55:17 matthew Exp $ +# $Id: parse_activity_log.pl,v 1.6 2004/12/16 15:03:29 matthew Exp $ # # Copyright Michigan State University Board of Trustees # @@ -44,12 +44,13 @@ use lonmysql(); use Time::HiRes(); use Getopt::Long(); use IO::File; - +use File::Copy; # # Determine parameters -my ($help,$course,$domain,$drop,$file,$time_run,$nocleanup,$log); +my ($help,$course,$domain,$drop,$file,$time_run,$nocleanup,$log,$backup); &Getopt::Long::GetOptions( "course=s" => \$course, "domain=s" => \$domain, + "backup" => \$backup, "help" => \$help, "logfile=s" => \$file, "timerun" => \$time_run, @@ -64,6 +65,8 @@ Process a lon-capa activity log into a d Parameters: course Required domain Optional + backup optional if present, backup the activity log file + before processing it drop optional if present, drop all course specific activity log tables. file optional Specify the file to parse, including path @@ -114,15 +117,16 @@ if ($log) { ## my $sourcefilename; # activity log data my $newfilename; # $sourcefilename will be renamed to this -my $sql_filename; # the mysql backup data file name. +my $gz_sql_filename; # the gzipped mysql backup data file name. my $error_filename; # Errors in parsing the activity log will be written here if ($file) { $sourcefilename = $file; } else { $sourcefilename = &get_filename($course,$domain); } -$sql_filename = $sourcefilename; +my $sql_filename = $sourcefilename; $sql_filename =~ s|[^/]*$|activity.log.sql|; +$gz_sql_filename = $sql_filename.'.gz'; $error_filename = $sourcefilename; $error_filename =~ s|[^/]*$|activity.log.errors|; $logthis->('Beginning logging '.time); @@ -140,6 +144,7 @@ if (-e $newfilename) { if (-e $sourcefilename) { $logthis->('renaming '.$sourcefilename.' to '.$newfilename); rename($sourcefilename,$newfilename); + Copy($newfilename,$newfilename.'.'.time) if ($backup); $logthis->("renamed $sourcefilename to $newfilename"); } else { my $command = 'touch '.$newfilename; @@ -270,7 +275,7 @@ $logthis->('SQL connection is up'); if ($drop) { &drop_tables(); $logthis->('dropped tables'); } -if (-s $sql_filename) { +if (-s $gz_sql_filename) { # if ANY one of the tables does not exist, load the tables from the # backup. my @Current_Tables = &Apache::lonmysql::tables_in_db(); @@ -285,7 +290,7 @@ if (-s $sql_filename) { foreach my $table (@Activity_Table,@ID_Tables) { if (! $Found{$table->{'id'}}) { $time_this->(); - &load_backup_tables($sql_filename); + &load_backup_tables($gz_sql_filename); $time_this->('load backup tables'); last; } @@ -324,7 +329,7 @@ if (-s $newfilename) { } elsif ($result > 0) { $time_this->(); $logthis->('process_courselog returned '.$result.' backup up tables'); - &backup_tables($sql_filename); + &backup_tables($gz_sql_filename); $time_this->('write backup tables'); } } @@ -575,14 +580,14 @@ sub outputtimes { ## Use mysqldump to store backups of the tables ## sub backup_tables { - my ($sql_filename) = @_; + my ($gz_sql_filename) = @_; my $command = qq{mysqldump --opt loncapa }; foreach my $table (@ID_Tables,@Activity_Table) { my $tablename = $table->{'id'}; $command .= $tablename.' '; } - $command .= '>'.$sql_filename; + $command .= '| gzip >'.$gz_sql_filename; $logthis->($command); system($command); } @@ -591,13 +596,15 @@ sub backup_tables { ## Load in mysqldumped files ## sub load_backup_tables { - my ($sql_filename) = @_; - return undef if (! -e $sql_filename); - # Check for .my.cnf - my $command = 'mysql -e "SOURCE '.$sql_filename.'" loncapa'; - $logthis->('loading previously saved sql table'.$/.$command); - system($command); - $logthis->('finished loading old data'); + my ($gz_sql_filename) = @_; + if (-s $gz_sql_filename) { + &logthis('loading data from gzipped sql file'); + my $command='gzip -dc activity.log.sql.gz | mysql --database=loncapa'; + system($command); + $logthis->('finished loading gzipped data');; + } else { + return undef; + } } ##