I am trying to auto sync my remote database with my local machine by using php file_get_contents and connecting to remote server with ftp_connect frequent intervals. I am using windows Task Scheduler from my local machine for that.
My php script works perfectly from the browser. But when Task Scheduler Triggers it, the command prompt window comes up showing error messages.
My script is
<?php
set_time_limit(0);
include('functions.php');
$root=$_SERVER['DOCUMENT_ROOT'];
$sitearr=array("indianchristians","mc","evaa","laveena","aita","kaazar","landshop");
$date=Date('Y-m-d H:i:s');
$vals="";
$msg="";
foreach($sitearr as $sa)
{
include('connnew.php');
if($sa=="landshop")
{
$remurl="dwggxtwa_landshoppe.sql";
$localurl="$root/$folder/Myadmin/download/sqlbackup/";
$backupserv=file_get_contents($url."/Myadmin/download/sqlbackup/backupserver.php");
}
else{
$remurl="$db.sql";
$localurl="$root/$folder/admin/download/sqlbackup/";
$backupserv=file_get_contents($url."/admin/download/sqlbackup/backupserver.php");
}
$savedb=$db.".sql";
$localdb=$localurl.$savedb;
$conn = ftp_connect($ftp_server);
$login_result = ftp_login($conn, $ftp_user_name, $ftp_user_pass);
ftp_pasv($conn, true);
if($sa=="landshop"){
ftp_chdir($conn, '/public_html/Myadmin/download/sqlbackup/');
}else{
ftp_chdir($conn, '/admin/download/sqlbackup/');
}
if (ftp_get($conn, $localdb, $remurl, FTP_BINARY))
{
$query=$maindb->query("SHOW TABLES") or die("SHOW TABLES ERROR ".$maindb->error);
while($st=$query->fetch_array()){
$drop=$maindb->query("DROP TABLE $db.$st[0]");
<br>";}
}
$templine = '';
// Read in entire file
$lines = file($localdb);
// Loop through each line
foreach ($lines as $line)
{
// Skip it if it's a comment
if (substr($line, 0, 2) == '--' || $line == '')
continue;
// Add this line to the current segment
$templine .= $line;
// If it has a semicolon at the end, it's the end of the query
if (substr(trim($line), -1, 1) == ';')
{
// Perform the query
if(!empty($templine)){
$maindb->query($templine) or die('Error performing query
\'<strong>' . $templine . '\': ' . $maindb->error . '<br /><br />');
}
// Reset temp variable to empty
$templine = '';
}
}
ftp_close($conn);
$vals.="('$date','$sa'),";
$msg .="Updated $sa<br>";
}
}
if(!empty($vals)){
$vals=rtrim($vals,",");
$date=Date('Y-m-d H:i:s');
$link = mysqli_connect('127.0.0.1','admin','admin') or die ("Error connecting to
mysql : " . mysqli_connect_errno($link).'\r\n');
$connect=new mysqli('127.0.0.1','admin','admin','admin') or die("Database error
:".$connect->error);
$checktab=$connect->query("SELECT * FROM backuptimeline");
if(!$checktab)
{
$createsql = "CREATE TABLE backuptimeline
(
id int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
Date datetime,
site varchar(500)
)";
$createtab=$connect->query($createsql) or die($connect->error);
}
$butlsql="INSERT INTO backuptimeline (Date,site) VALUES $vals";
// echo $butlsql."<br>";
$insbu=$connect->query($butlsql)or die($connect->error);
if($insbu){
//sendEmailAlerts("anitgopinath@gmail.com","anitgopinath@gmail.com","DB
$sa Backed Up","All DB $sa backed up at $date");
$msg.="<br>Timeline Updated<br>";
}
else{
$msg.= "<br>Timeline Not Updated<br>";
}
echo $msg."<br>";
sendEmailAlerts("anitgopinath@gmail.com","anitgopinath@gmail.com","DBs Back Up
Results",$msg);
}else{
echo $msg."<br>";
sendEmailAlerts("anitgopinath@gmail.com","anitgopinath@gmail.com","DBs Back Up
Failed",'DBs Back Up Failed');
}
?>
Task Scheduler entries;
Program / Script in Action field:
"C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\binaries\php\php_runningversion\php.exe"
Arguments field
"C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\Admin\download\autobackup_task_scheduler.php"
Some error Messages
> Warning: ftp_get(/matrimony/admin/download/sqlbackup/rotiraa1_mc.sql): failed to open stream: No such file or directory in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\Admin\download\autobackup_task_scheduler.php on line 35 PHP Warning: ftp_get(): Error opening /matrimony/admin/download/sqlbackup/rotiraa1_mc.sql in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\Admin\download\autobackup_task_scheduler.php on line 35
>
> Warning: ftp_get(): Error opening
> /matrimony/admin/download/sqlbackup/rotiraa1_mc.sql in C:\Program
> Files
> (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\Admin\download\autobackup_task_scheduler.php
> on line 35 PHP Warning: file_get_contents(): SSL operation failed
> with code 1. OpenSSL Error messages: error:1407742E:SSL
> routines:SSL23_GET_SERVER_HELLO:tlsv1 alert protocol version in
> C:\Program Files
> (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\Admin\download\autobackup_task_scheduler.php
> on line 21
>
> Warning: file_get_contents(): SSL operation failed with code 1.
> OpenSSL Error messages: error:1407742E:SSL
> routines:SSL23_GET_SERVER_HELLO:tlsv1 alert protocol version in
> C:\Program Files
> (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\Admin\download\autobackup_task_scheduler.php
> on line 21 PHP Warning: file_get_contents(): Failed to enable crypto
> in C:\Program Files
> (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\Admin\download\autobackup_task_scheduler.php
> on line 21
>
> Warning: file_get_contents(): Failed to enable crypto in C:\Program
> Files
> (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\Admin\download\autobackup_task_scheduler.php
> on line 21 PHP Warning:
> file_get_contents(https://www.evaa-ethnix.com/admin/download/sqlbackup/backupserver.php):
> failed to open stream: operation failed in C:\Program Files
> (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\Admin\download\autobackup_task_scheduler.php
> on line 21
>
> Warning:
> file_get_contents(https://www.evaa-ethnix.com/admin/download/sqlbackup/backupserver.php):
> f
I am not able identify whether it is a path issue or something else. I have been breaking my for a long time on this.
Any help will be greatly appreciated.
What I have tried:
I have tried using a batch file (.bat) too. I put it in system32 folder and tried. Still same errors appear.