Tuesday, November 10, 2009

backup mysql with batch script + PHP + WinRAR

backup mysql with batch script + PHP + WinRAR


@GOTO START
<?php
### embedded php in bat (batch) script.

$db_nameArr = array('my_db1', 'my_db2');
$db_user = 'test';
$db_pass = 'test';

$now = date('Y-m-d_His');

$curr_dir = dirname(__FILE__);

$db_store_dir =  $curr_dir. '/DB';
$rar_store_dir = $curr_dir . '/RAR';

if (!file_exists($db_store_dir)) {
  mkdir($db_store_dir);
}

if (!file_exists($rar_store_dir)) {
  mkdir($rar_store_dir);
}

mkdir($db_store_dir . '/' . $now);

foreach ($db_nameArr as $db_name) {
  $cmd = 'mysqldump -u ' . $db_user . ' -p' . $db_pass . ' ' . $db_name . ' > ' . $db_store_dir . '/' . $now . '/' . $now . '_' . $db_name . '.sql';
  exec($cmd);
}

//$tarFilePath = $rar_store_dir . '/' . $now;

exec( '"C:\\Program Files\\WinRAR\\WinRAR.exe"' . ' a ' . $rar_store_dir . '/' . $now . ' -r DB/' . $now . '/*');

/*
:START
@d:\uo\www\PHP5.2.9\php.exe %0
@exit
*/
?>

No comments: