Ich speichere Telefonanrufe im OGG-Format und die entsprechenden Informationen in der MySQL-Datenbank. Kürzlich, nachdem ich Asterisk neu installiert und die Konfigurationsdatei für die MySQL-Verbindung überschrieben hatte, waren alle Daten für anderthalb Monate verloren. Zum Glück wurden die Informationen parallel in eine Log-Datei geschrieben, sodass ich die fehlenden Daten für diesen Zeitraum mit grep extrahieren konnte. Ich schrieb ein PHP-Skript, das daraus einen MySQL-Dump erstellte.
<?php
$first_qwery="INSERT INTO `cdr` (`calldate`, `linenum`, `clid`, `src`, `dst`, `dcontext`, `channel`, `dstchannel`, `lastapp`, `lastdata`, `duration`, `billsec`, `disposition`, `amaflags`, `accountcode`, `userfield`, `uniqueid`) VALUES ";
$all_stroki=$first_qwery;
read_file("sep_master.txt");
write_file($all_stroki, "ok.sql");
// Funktion zum Schreiben einer großen Zeichenkette in die Dump-Datei
function write_file($all_stroki, $file_name){
$h = fopen($file_name,"a"); ;
if(fwrite($h,$all_stroki))
echo "Das Hinzufügen des Textes war erfolgreich";
else echo "Beim Hinzufügen der Daten ist ein Fehler aufgetreten";
fclose($h);
}
// Datei in ein Array einlesen, jede Zeile des Arrays an die Funktion zum Laden in MySQL übergeben
function read_file($file_name){
$file_array = file($file_name);
foreach ($file_array as $stroka){
$stroka2= str_replace('"','',$stroka);
$stroka3= str_replace("'",'',$stroka2);
//echo $stroka2;
$stroka_to_array=explode(',',$stroka3);
//if ($stroka_to_array[17]="") continue;
//echo $stroka_to_array[17];
addtomysql($stroka_to_array);
//print_r ($stroka_to_array);
}
}
// Funktion zum Schreiben einer Zeile in MySQL
function addtomysql($stroka_array){
//print_r ($stroka_array);# Ausgabe am Ende der Datei
global $all_stroki;
$i=", ";
$ii="', '";
$count_array=count($stroka_array);
$unik=$stroka_array[$count_array-2];
$next_query="('".$stroka_array[11]."','', '".$stroka_array[4].$ii.$stroka_array[1].$ii.$stroka_array[2]."', '', '"
.$stroka_array[6].$ii.$stroka_array[5].$ii.$stroka_array[7].$ii.$stroka_array[8]."', 777, 777, 'ANSWERED',3 ,'' ,'', '".$unik."'), ";
$all_stroki=$all_stroki.$next_query;
}
//$next_query=$stroka_array[10].", '', ".$stroka_array[4].$i.$stroka_array[1].$i.$stroka_array[2].", '', ".$stroka_array[6].$i.$stroka_array[5].$i.$stroka_array[7].$i.$stroka_array[8].$i.$stroka_array[13].$i.$stroka_array[14].$i.$stroka_array[15].$i.$stroka_array[9].", '', ".", '', ".$stroka_array[17].$i.
?>
Und am Ende bleibt noch mysql -p asterisk < ok.sql
