a bitmoji of Dre Wilson.

Create and Schedule Automatic MySQL Database Backups In PHP on Windows 10


In this video, I’ll show you how to create a PHP script that will backup your MySQL databases. Then I’ll show you how to run the script automatically when Windows 10 starts by creating a task in Task Scheduler.

Example Code

$databases = ['test'];
    $user = 'dre';
    $pass = 'manna16';
    $host = 'localhost';

    date_default_timezone_set("America/Chicago");
    
    if(!file_exists("C:/Users/Dre/Documents/Databases/")) {
      mkdir("C:/Users/Dre/Documents/Databases/");
    }

    foreach ($databases as $database) {
        if(!file_exists("C:/Users/Dre/Documents/Databases/$database")) {
            mkdir("C:/Users/Dre/Documents/Databases/$database");
          }

        $filename = $database."_".date('F_d_Y').'@'.date('g_ia').uniqid("_", false);
        $folder = "C:/Users/Dre/Documents/Databases/" . $filename. '.sql';

        exec("C:/xampp/mysql/bin/mysqldump --user={$user} --password={$pass}
        --host={$host} {$database}
        --result-file={$folder}", $output);
    } 
	
    print_r($output);

Comments