0

I try to run a cron job to do a backup to my database.

The problem that the cron job is execute and i receive an email that the cron job is executing but the problem is i don't have a backup file. But if i run the page on the browser without the cron job i have a back up file. Also i have a button if i click this button i receive a backup file. That mean my code work.

Cron job file:

<?php
include("includes/connect.php");
include("includes/functions.php");
include("includes/backup.php");
?>

Backup Function and this function is in functions.php

<?php
function Export_Database($host,$user,$pass,$name,$tables=false,$backup_name=false)
    {
        $mysqli = new mysqli($host,$user,$pass,$name); 
        $mysqli->select_db($name); 
        $mysqli->query("SET NAMES 'utf8'");

        $queryTables    = $mysqli->query('SHOW TABLES'); 
        while($row = $queryTables->fetch_row()) 
        { 
            $target_tables[] = $row[0]; 
        }   
        if($tables !== false) 
        { 
            $target_tables = array_intersect( $target_tables, $tables); 
        }
        foreach($target_tables as $table)
        {
            $result         =   $mysqli->query('SELECT * FROM '.$table);  
            $fields_amount  =   $result->field_count;  
            $rows_num=$mysqli->affected_rows;     
            $res            =   $mysqli->query('SHOW CREATE TABLE '.$table); 
            $TableMLine     =   $res->fetch_row();
            $content        = (!isset($content) ?  '' : $content) . "\n\n".$TableMLine[1].";\n\n";

            for ($i = 0, $st_counter = 0; $i < $fields_amount;   $i++, $st_counter=0) 
            {
                while($row = $result->fetch_row())  
                { //when started (and every after 100 command cycle):
                    if ($st_counter%100 == 0 || $st_counter == 0 )  
                    {
                            $content .= "\nINSERT INTO ".$table." VALUES";
                    }
                    $content .= "\n(";
                    for($j=0; $j<$fields_amount; $j++)  
                    { 
                        $row[$j] = str_replace("\n","\\n", addslashes($row[$j]) ); 
                        if (isset($row[$j]))
                        {
                            $content .= '"'.$row[$j].'"' ; 
                        }
                        else 
                        {   
                            $content .= '""';
                        }     
                        if ($j<($fields_amount-1))
                        {
                                $content.= ',';
                        }      
                    }
                    $content .=")";
                    //every after 100 command cycle [or at last line] ....p.s. but should be inserted 1 cycle eariler
                    if ( (($st_counter+1)%100==0 && $st_counter!=0) || $st_counter+1==$rows_num) 
                    {   
                        $content .= ";";
                    } 
                    else 
                    {
                        $content .= ",";
                    } 
                    $st_counter=$st_counter+1;
                }
            } $content .="\n\n\n";
        }

     $fp = fopen($_SERVER['DOCUMENT_ROOT']."/backup"."/mybackup-".date('d-m-Y')."-".date('H:i:s').".sql","wb");
fwrite($fp,$content);
fclose($fp);
 exit();
    }?>

Backup page:

<?php
    $mysqlUserName      = "***";
    $mysqlPassword      = "";
    $mysqlHostName      = "****";
    $DbName             = "****";
    $backup_name        = "mybackup.sql";

     $tables = array();
$showTable = "SHOW TABLES from $DbName";
$getData = mysqli_query($conn, $showTable);
while ($row = mysqli_fetch_row($getData)) {
   $tables[] = $row;
}
    Export_Database($mysqlHostName,$mysqlUserName,$mysqlPassword,$DbName,  $tables=false, $backup_name=false );
?>

How can i solve this problem to run the code when the cron job is execute??!!

1
  • View cron log file /var/log/cron Commented Sep 13, 2017 at 6:09

1 Answer 1

3

Why don't you use mysqldump?

As it says on the documentation:
4.5.4 mysqldump — A Database Backup Program

[cron-times] mysqldump -u <dbuser> --password <pw> [--host <host] --all-databases > /opt/mysql.bkp/`date`
Sign up to request clarification or add additional context in comments.

8 Comments

B i use this in a cron job my backup file is saved in a backup folder i have a host on godaddy
You can pipe the output to a file of choice. Simply do > /my/path/to/file You can also use the shell functions of your cron-shell to get the current date. Have this to read about how to change the cron shell: unix.stackexchange.com/questions/94456/…
B i did this usr/bin/mysqldump -u dbusername -p 'dbpass' dbname>/backup/mybackup.sql backup is the folder name mybackup.sql is the file name how to put a datetime for this name ??!!
You can do mysqldump [...] > /backup/$(date +%d%m%Y-%H%M%S).sql. This will generate an file name such as in php "backup/" . date("dmY-His") . ".sql". For further info about the date shell tool consult the man page.
B i receive this error /usr/local/cpanel/bin/jailshell: -c: line 0: unexpected EOF while looking for matching `)' /usr/local/cpanel/bin/jailshell: -c: line 1: syntax error: unexpected end of file
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.