The Guru Of Tech - Gobind

Asterisk Insall, Codec Install, Vicidial Install.

Vicidial, Asterisk, Web-MeetMe, MySQL, OTRS

Vicidial, Asterisk, Web-MeetMe, MySQL, OTRS

Vicidial, Asterisk, Web-MeetMe, MySQL, OTRS

Vicidial, Asterisk, Web-MeetMe, MySQL, OTRS.

Vicidial, Asterisk, Web-MeetMe, MySQL, OTRS

Vicidial, Asterisk, Web-MeetMe, MySQL, OTRS.

Thursday, June 21, 2012

Sending Email From the dialplan

exten => s,1,NoOp()
exten => s,n,System(echo "To: simplesunny@gmail.com" > /opt/etc/init.d/calls)
exten => s,n,System(echo "Subject: [PBX]: Service Down" >> /opt/etc/init.d/calls)
exten => s,n,System(echo "" >> /opt/etc/init.d/calls)
exten => s,n,System(echo "service down at ${STRFTIME(%C%m%d%y%H%M)}" >> /opt/etc/init.d/calls)
exten => s,n,System(sendmail -t -f noc@gmail.com < /opt/etc/init.d/calls)
exten => s,n,Hangup
sub mailSend {
my ($subj, $body) = @_;
my(@da, @day, @mon, $datetime, $expTO, $mail, $head);
@da = localtime(time);
@day = qw(Sun Mon Tue Wed Thu Fri Sat);
@mon = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
$datetime = sprintf "%s, %02d %s %d %02d:%02d:%02d +0900",
($day[$da[6]], $da[3]*1, $mon[$da[4]], $da[5]+1900,
$da[2]*1, $da[1]*1, $da[0]*1);
$body = Jcode->new($body)->h2z->jis;
$head = "Return-Path: <$m_RETPATH>\n";
$expTO = join(",\n ", @m_MAILTO);
$head .= "To: $expTO\n";
$head .= "From: $m_FROM\n";
$head .= "Date: $datetime\n";
$head .= "Content-type: text/plain; charset=iso-2022-jp\n";
$head .= "Subject: $subj\n";
$head .= "\n";
$head = &mimeencode($head);
$mail = "$head$body";
if (open (OUT, "|$QIPATH")) { # qmail-inject
$mail =~ s/%/%%/g;
printf OUT "$mail";
close (OUT);
}
}
Another Way-à

exten => 1234,1,Answer
exten => 1234,2,System(echo "Service Down" | mail -s "SUBJECT" simplesunny@gmail.com)
Exten => alarm,1,System(/usr/local/bin/sendalarm.sh|email at address.com)
Another Way-à
Exten => alarm,1,AGI(sendalarm)
/usr/local/bin/sendalarm
#!/bin/sh
Mail -s "Alarm condition on PBX" $1 < /dev/null

Friday, June 1, 2012

Backup Your MySQL Database Using PHP

One of the most important tasks any developer needs to do often is back up their MySQL database. In many cases, the database is what drives most of the site. While most web hosts do a daily backup of a customer's database, relying on them to make backups and provide them at no cost is risky to say the least. That's why I've created a database backup function that I can call whenever I want -- including nightly CRONs.

The PHP & MySQL Code

backup_tables('localhost','username','password','databasename');
 
 
/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$name,$tables = '*')
{
  
  $link = mysql_connect($host,$user,$pass);
  mysql_select_db($name,$link);
  
  //get all of the tables
  if($tables == '*')
  {
    $tables = array();
    $result = mysql_query('SHOW TABLES');
    while($row = mysql_fetch_row($result))
    {
      $tables[] = $row[0];
    }
  }
  else
  {
    $tables = is_array($tables) ? $tables : explode(',',$tables);
  }
  
  //cycle through
  foreach($tables as $table)
  {
    $result = mysql_query('SELECT * FROM '.$table);
    $num_fields = mysql_num_fields($result);
    
    $return.= 'DROP TABLE '.$table.';';
    $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
    $return.= "\n\n".$row2[1].";\n\n";
    
    for ($i = 0; $i < $num_fields; $i++) 
    {
      while($row = mysql_fetch_row($result))
      {
        $return.= 'INSERT INTO '.$table.' VALUES(';
        for($j=0; $j<$num_fields; $j++) 
        {
          $row[$j] = addslashes($row[$j]);
          $row[$j] = ereg_replace("\n","\\n",$row[$j]);
          if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
          if ($j<($num_fields-1)) { $return.= ','; }
        }
        $return.= ");\n";
      }
    }
    $return.="\n\n\n";
  }
  
  //save file
  $handle = fopen('db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');
  fwrite($handle,$return);
  fclose($handle);
}