Setting up a Cron Job from cPanel for Database backup with php script

Discussion in 'Programming' started by tejasluvs, Jun 29, 2010.

  1. #1
    I want a PHP cron script to be set up with cPanel cron jobs that would take backup of MySQL database.

    Regards,
     
    tejasluvs, Jun 29, 2010 IP
  2. silv3r.m00n

    silv3r.m00n Active Member

    Messages:
    53
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    63
    #2
    Execute the mysql dump command in your php script.
    example :

    function backup() {
    //PREPARE THE FILENAME USING DATA TIME
    $suffix = date('Y-m-d-h-i-s-a');;
    $filename = "backups/backup-$suffix.zip";

    //CHECK WHETHER backups FOLDER EXISTS AND IS WRITEABLE
    if(! is_writable('backups'))
    {
    die('Backups folder not writable. Please check that backups folder exists and is writable');
    }

    //Execute the command to create backup sql file
    exec("mysqldump --user={$this->username} --password={$this->password} --quick --add-drop-table --add-locks --extended-insert --lock-tables --all {$this->db} > backups/backup.sql");

    //Now zip that file
    $zip = new ZipArchive();

    if ($zip->open($filename, ZIPARCHIVE::CREATE) !== TRUE) {
    exit("cannot open <$filename>\n");
    }
    $zip->addFile("backups/backup.sql" , "backup.sql");
    $zip->close();

    #Now delete the .sql file
    if(! unlink("backups/backup.sql"))
    {
    die('backups/backup.sql temp file was not deleted. Please check');
    }

    #Return the path to the zip backup file
    return $filename;
    }

    Now set cron to run this php script at desired intervals
     
    silv3r.m00n, Jun 30, 2010 IP
  3. events

    events Active Member

    Messages:
    391
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    73
    #3
    events, Jul 17, 2010 IP