I record phone calls in ogg format and store the call detail records (CDR) in a MySQL database. Recently, after reinstalling Asterisk and accidentally overwriting the MySQL connection configuration file, all data was lost for half a month. Fortunately, the information was simultaneously written to a log file, so I used grep to extract the missing data from that log. I then wrote a PHP script to convert it into a MySQL dump.
<?php
$first_query="INSERT INTO `cdr` (`calldate`, `linenum`, `clid`, `src`, `dst`, `dcontext`, `channel`, `dstchannel`, `lastapp`, `lastdata`, `duration`, `billsec`, `disposition`, `amaflags`, `accountcode`, `userfield`, `uniqueid`) VALUES ";
$all_strings=$first_query;
read_file("sep_master.txt");
write_file($all_strings, "ok.sql");
// Function to write one large string to the dump file
function write_file($all_strings, $file_name){
$h = fopen($file_name,"a");
if(fwrite($h,$all_strings))
echo "Text addition successful";
else echo "An error occurred while adding data";
fclose($h);
}
// Load the file into an array, passing each line to the MySQL upload function
function read_file($file_name){
$file_array = file($file_name);
foreach ($file_array as $line){
$line2= str_replace('"','',$line);
$line3= str_replace("'",'',$line2);
//echo $line2;
$line_to_array=explode(',',$line3);
//if ($line_to_array[17]="") continue;
//echo $line_to_array[17];
addtomysql($line_to_array);
//print_r ($line_to_array);
}
}
// Function to write a line to MySQL
function addtomysql($line_array){
//print_r ($line_array);# Output at the end of the file
global $all_strings;
$i=", ";
$ii="', '";
$count_array=count($line_array);
$unik=$line_array[$count_array-2];
$next_query="('".$line_array[11]."','', '".$line_array[4].$ii.$line_array[1].$ii.$line_array[2]."', '', '"
.$line_array[6].$ii.$line_array[5].$ii.$line_array[7].$ii.$line_array[8]."', 777, 777, 'ANSWERED',3 ,'' ,'', '".$unik."'), ";
$all_strings=$all_strings.$next_query;
}
//$next_query=$line_array[10].", '', ".$line_array[4].$i.$line_array[1].$i.$line_array[2].", '', ".$line_array[6].$i.$line_array[5].$i.$line_array[7].$i.$line_array[8].$i.$line_array[13].$i.$line_array[14].$i.$line_array[15].$i.$line_array[9].", '', ".", '', ".$line_array[17].$i.
?>
Finally, run mysql -p asterisk < ok.sql
