Script loopt wel handmatig maar niet via cronjob server

Status
Niet open voor verdere reacties.

Atwist

Terugkerende gebruiker
Lid geworden
24 jan 2006
Berichten
1.076
Besturingssysteem
Wndows 11
Office versie
2016
Hallo.

Ik probeer mijn database back-up te automatiseren via onderstaand PHP script, handmatig loopt het allemaal goed maar als het het via mijn cronjob server probeer te starten dan werkt het niet.
Hoe kan ik dit oplossen?
Mijn PHP ervaring is gering tot matig. :o

Code:
<?php

##################### 
//CONFIGURATIONS  
#####################
// Define the name of the backup directory
define('data', './forum' ) ; 
// Define  Database Credentials
define('HOST', 'lokalhost' ) ; 
define('USER', 'test1' ) ; 
define('PASSWORD', '123456' ) ; 
define('DB_NAME', 'test2' ) ; 
/*
Define the filename for the sql file
*/
$fileName = 'mysqlbackup--' . date('d-m-Y') . '@'.date('h.i.s').'.sql' ; 
// Set execution time limit
if(function_exists('max_execution_time')) {
if( ini_get('max_execution_time') > 0 ) 	set_time_limit(0) ;
}

###########################  

//END  OF  CONFIGURATIONS  

###########################

// Check if directory is already created and has the proper permissions
if (!file_exists(BACKUP_DIR)) mkdir(BACKUP_DIR , 0700) ;
if (!is_writable(BACKUP_DIR)) chmod(BACKUP_DIR , 0700) ; 

// Create an ".htaccess" file , it will restrict direct accss to the backup-directory . 
$content = 'deny from all' ; 
$file = new SplFileObject(BACKUP_DIR . '/.htaccess', "w") ;
$file->fwrite($content) ;

$mysqli = new mysqli(HOST , USER , PASSWORD , DB_NAME) ;
if (mysqli_connect_errno())
{
   printf("Connect failed: %s", mysqli_connect_error());
   exit();
}
 // Introduction information
 $return .= "--\n";
$return .= "-- A Mysql Backup System \n";
$return .= "--\n";
$return .= '-- Export created: ' . date("Y/m/d") . ' on ' . date("h:i") . "\n\n\n";
$return = "--\n";
$return .= "-- Database : " . DB_NAME . "\n";
$return .= "--\n";
$return .= "-- --------------------------------------------------\n";
$return .= "-- ---------------------------------------------------\n";
$return .= 'SET AUTOCOMMIT = 0 ;' ."\n" ;
$return .= 'SET FOREIGN_KEY_CHECKS=0 ;' ."\n" ;
$tables = array() ; 
// Exploring what tables this database has
$result = $mysqli->query('SHOW TABLES' ) ; 
// Cycle through "$result" and put content into an array
while ($row = $result->fetch_row()) 
{
$tables[] = $row[0] ;
}
// Cycle through each  table
 foreach($tables as $table)
 { 
// Get content of each table
$result = $mysqli->query('SELECT * FROM '. $table) ; 
// Get number of fields (columns) of each table
$num_fields = $mysqli->field_count  ;
// Add table information
$return .= "--\n" ;
$return .= '-- Tabel structure for table `' . $table . '`' . "\n" ;
$return .= "--\n" ;
$return.= 'DROP TABLE  IF EXISTS `'.$table.'`;' . "\n" ; 
// Get the table-shema
$shema = $mysqli->query('SHOW CREATE TABLE '.$table) ;
// Extract table shema 
$tableshema = $shema->fetch_row() ; 
// Append table-shema into code
$return.= $tableshema[1].";" . "\n\n" ; 
// Cycle through each table-row
while($rowdata = $result->fetch_row()) 
{ 
// Prepare code that will insert data into table 
$return .= 'INSERT INTO `'.$table .'`  VALUES ( '  ;
// Extract data of each row 
for($i=0; $i<$num_fields; $i++)
{
$return .= '"'.$rowdata[$i] . "\"," ;
 }
 // Let's remove the last comma 
 $return = substr("$return", 0, -1) ; 
 $return .= ");" ."\n" ;
 } 
 $return .= "\n\n" ; 
}
// Close the connection
$mysqli->close() ;
$return .= 'SET FOREIGN_KEY_CHECKS = 1 ; '  . "\n" ; 
$return .= 'COMMIT ; '  . "\n" ;
$return .= 'SET AUTOCOMMIT = 1 ; ' . "\n"  ; 
//$file = file_put_contents($fileName , $return) ; 
$zip = new ZipArchive() ;
$resOpen = $zip->open(BACKUP_DIR . '/' .$fileName.".zip" , ZIPARCHIVE::CREATE) ;
if( $resOpen ){
$zip->addFromString( $fileName , "$return" ) ;
    }
$zip->close() ;
$fileSize = get_file_size_unit(filesize(BACKUP_DIR . "/". $fileName . '.zip')) ; 
$message = <<<msg
  <h2>BACKUP  completed ,</h2>
 
  the archive has the name of  : <b>  $fileName  </b> and it's file-size is :   $fileSize  .

 This zip archive can't be accessed via a web browser , as it's stored into a protected directory . 

  It's highly recomended to transfer this backup to another filesystem , use your favorite FTP client to download the archieve . 
msg;
echo $message ; 

// Function to append proper Unit after file-size . 
function get_file_size_unit($file_size){
switch (true) {
    case ($file_size/1024 < 1) :
        return intval($file_size ) ." Bytes" ;
        break;
    case ($file_size/1024 >= 1 && $file_size/(1024*1024) < 1)  :
        return intval($file_size/1024) ." KB" ;
        break;
	default:
	return intval($file_size/(1024*1024)) ." MB" ;
}
}
 
Begin eens met het controleren van de paden, dikke kans dat daar het probleem in zit.

Paden zijn voor een cronjob bijna altijd anders als voor een regulier PHP-script.
 
Begin eens met het controleren van de paden, dikke kans dat daar het probleem in zit.

Paden zijn voor een cronjob bijna altijd anders als voor een regulier PHP-script.

Deze snap ik niet.
:confused:
 
Laat eerst eens zien hoe die cronjob ingang er nu uitziet.
Standaard draai je (bash, cshell etc.) scripts in cron, niet een .php bestand, dus moet je uitwijken naar andere manieren om tóch het .php script te laten uitvoeren.
Ik vond hier de volgende tip om als cron taak in te voeren (natuurlijk localhost/test.php aanpassen aan jouw situatie en wget moet als package zijn geïnstalleerd op de computer waar de cronjob staat):
wget -q -O /dev/null 'http://localhost/test.php'

Tijs.
 
tja een script dat ik niet meer nodig had omdat elke hosting het ondersteund

nu weet dat het een combinatie van while sleep() en die() is die op zijn beurt
een ander script aanroept met een naam
nu moet je altijd altijd kunnen uitzetten door een file erbij te plaatsen indien je die wil killen
nu voorzie ook een log en try catch om er voor te zorgen dat je onvoorziene fouten kan opsporen
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan