$servername = “localhost”;
$username = “root”;
$password = “passw0rd”;
$dbname = “asterisk”;
$te_code = “tenant-name”;
$secure_file_priv = “/var/lib/mysql-files/”;
$temp_file = “middle”;
$temp_dest_path = “/home/aws-vasco/”;
// path_date = 2020/11/20
$path_date = “”;
$command = “rm -rf ” . $temp_dest_path . “;”;
$command .= “mkdir ” . $temp_dest_path;
system($command);
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die(“Connection asterisk failed: ” . $conn->connect_error);
}
$sql = “select re_id, DATE_FORMAT(re_date,’%Y-%m-%d’) as calldate, “;
$sql .= “DATE_FORMAT(re_date,’%Y%m%d-%H%i’) as calldate_plus_time, re_format, re_uniqueid, re_name “;
$sql .= “from re_recordings “;
$sql .= “where “;
$sql .= “re_te_id = (select te_id from te_tenants where te_code = ‘” . $te_code . “‘) “;
$sql .= “and “;
$sql .= “re_size > 44 “;
$sql .= “and “;
$sql .= “date(re_date) = DATE_ADD(CURDATE(), INTERVAL -1 DAY) “;
print $sql . “\n”;
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$i = 0;
while($row = $result->fetch_assoc()) {
if($i == 0) $path_date = create_path_by_date($row[“calldate”]);
print “calltime: ” . $row[“calldate_plus_time”]. “re_id: ” . $row[“re_id”]. ” – uniqueid: ” . $row[“re_uniqueid”] . “\n”;
$cdr_result = get_file_name($row[“re_uniqueid”], $row[“calldate_plus_time”]);
if(!empty($cdr_result)) {
dump_file($row[“re_id”], $row[“re_uniqueid”], $cdr_result[0], $cdr_result[1], “mp3”);
} else {
print “********************\n”;
}
$i++;
}
} else {
echo “0 results”;
}
$conn->close();
$command = “/usr/local/bin/aws s3 sync /home/aws-vasco s3://vasco –profile aws-vasco”;
system($command);
/***********************
get path, file_name
***********************/
function get_file_name($uniqueid, $calldate_plus_time) {
return array(substr($calldate_plus_time, 0, 10), $calldate_plus_time.”-“.$uniqueid);
}
/******************
create directory yyyy/mm/dd recursively
******************/
function create_path_by_date($date) {
global $temp_dest_path;
print “****” .$date.”******\n”;
$path_date = str_replace(‘-‘, ‘/’, $date);
$command = “cd “. $temp_dest_path .”;”;
$command .= “mkdir -p ” . $path_date . “;”;
print “****” .$command.”******\n”;
system($command);
return $path_date;
}
/***********************
dump recording file
***********************/
function dump_file($re_id, $re_uniqueid, $path, $file_name, $file_format) {
global $secure_file_priv, $temp_file, $conn;
global $temp_dest_path, $path_date;
print $file_name . “======\n”;
$dump_sql = “select re_data into dumpfile ‘” . $secure_file_priv . $temp_file .”‘ “;
$dump_sql .= “from re_recordings “;
$dump_sql .= “where re_id = ‘” . $re_id . “‘”;
print $dump_sql . “\n”;
$conn->query($dump_sql);
// move recording files to /home/tenant
$command = “cd “. $temp_dest_path . $path_date .”;”;
$command .= “mv ” . $secure_file_priv . $temp_file . ” ” . $temp_dest_path . $path_date . “/” . $file_name . “.” . $file_format .”;”;
print $command . “\n”;
system($command);
/*
if ($re_id == 3909375) {
exit;
}
*/
}
?>
- the recording audio in mysql table, used “select into dumpfile”
- the aws is used, it used profile to separate multi aws account credential
- file name my need include the caller – callee info
select re_data from re_recordings where re_id = '3914895'