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