Files
pannello/script/partitioning.pl
2015-10-08 11:00:52 +02:00

64 lines
2.1 KiB
Perl
Executable File

#!/usr/bin/perl
$|=1;
use DBI;
use Switch;
use FindBin qw($Bin);
use POSIX qw/strftime/;
use Time::Local;
our $messaggi = 0;
while( $_ = shift @ARGV ) {
$messaggi = 1 if /^\-v$/;
}
open CONFFILE, "< $Bin/../php/config.php" or die ("Manca file di configurazione\n");
while (<CONFFILE>) {
if(/^\$db_data_server = "(\S+)"/ ) {
$db_host = $1;
}
if(/^\$db_data_name = "(\S+)"/ ) {
$db_name = $1;
}
if(/^\$db_data_user = "(\S+)"/ ) {
$db_user = $1;
}
if(/^\$db_data_pwd = "(\S+)"/ ) {
$db_pass = $1;
}
}
close CONFFILE;
printf "Inizio attivita' %s\n",strftime('%d-%m-%Y %H:%M:%S',localtime) if ($messaggi);
my $dbmysql = DBI->connect("DBI:mysql:;host=$db_host", "$db_user", "$db_pass") or die ($DBI::errstr);
$sts = $dbmysql->prepare("use $db_name");
$sts->execute ();
$part = "ALTER TABLE dati_traffico PARTITION BY RANGE ( dataunix ) (\n";
#$query = "SELECT DATE_FORMAT(data, \"%Y%m%d\") AS giorno, DATE_FORMAT(data, \"%Y-%m-%d\") AS giorno2 FROM dati_traffico GROUP BY giorno ORDER BY giorno";
$query = "SELECT DATE_FORMAT(data, \"%Y%m%d\") AS giorno, DATE_FORMAT(data, \"%Y\") AS a, DATE_FORMAT(data, \"%m\") AS m, DATE_FORMAT(data, \"%d\") AS g FROM dati_traffico GROUP BY giorno ORDER BY giorno";
$sts = $dbmysql->prepare($query);
$sts->execute ();
while (my $dato = $sts->fetchrow_hashref) {
my $giorno = $$dato{'giorno'};
my $a = $$dato{'a'};
my $m = $$dato{'m'};
my $g = $$dato{'g'};
# my $giorno2 = $$dato{'giorno2'};
# print "$giorno\n";
my $timest = timelocal (59, 59, 23, $g, $m-1, $a);
# $part .= "PARTITION p$giorno VALUES LESS THAN ( UNIX_TIMESTAMP('$giorno2 23:59:59') ),\n";
$part .= "PARTITION p$giorno VALUES LESS THAN ( $timest ),\n";
}
$part .= "PARTITION p99999999 VALUES LESS THAN (MAXVALUE)\n";
$part .= ");\n";
print $part;
printf "Inizio partizionamento %s\n",strftime('%d-%m-%Y %H:%M:%S',localtime) if ($messaggi);
$sts = $dbmysql->prepare($part);
$sts->execute ();
printf "Termine attivita' %s\n",strftime('%d-%m-%Y %H:%M:%S',localtime) if ($messaggi);