Jump to content

Possible to improve this little optimize/back-up script?


strago

Recommended Posts

I'm making a simple script that optimizes all the databases twice, and then backs them up, so I can let crontab do all the work daily and all I have to do is download them.

<?
$datestamp = date("m-d-Y-h-i-s");      // Current date and time to put on filename of backup file in format of MM-DD-YYYY--hour-minute-second

$optimize = "mysqlcheck -u root -pPASSWORD --auto-repair --check --optimize --all-databases";
$optimizeresult = passthru($optimize);
//Make sure it's completely optimized.
$optimizeresult2 = passthru($optimize);

$command = "mysqldump -u root -pPASSWORD database > /FULL_PATH/back-ups/database-$datestamp.sql";
$result2 = passthru($command);

$command2 = "mysqldump -u root -pPASSWORD database2 > /FULL_PATH/back-ups/database2-$datestamp.sql";
$result3 = passthru($command2);

$command3 = "mysqldump -u root -pPASSWORD database3 > /FULL_PATH/back-ups/database3-$datestamp.sql";
$result4 = passthru($command3);

?>

 

Is there any way to combine the three back-ups in to one command? So it creates a file for each database. Is there any thing that should be added to the optimize code? Like some anti-lock and unlock tables code?

I found your answer pretty easily with google... http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_databases

 

<?
$datestamp = date("m-d-Y-h-i-s");      // Current date and time to put on filename of backup file in format of MM-DD-YYYY--hour-minute-second

$optimize = "mysqlcheck -u root -pPASSWORD --auto-repair --check --optimize --all-databases";
$optimizeresult = passthru($optimize);
//Make sure it's completely optimized.
$optimizeresult2 = passthru($optimize);

$command = "mysqldump -u root -pPASSWORD --databases database database2 database3 > /FULL_PATH/back-ups/databases-$datestamp.sql";
$result2 = passthru($command);
?>

 

That should help :)

Ok then I would advise doing a loop.

 

<?
$datestamp = date("m-d-Y-h-i-s");      // Current date and time to put on filename of backup file in format of MM-DD-YYYY--hour-minute-second

$optimize = "mysqlcheck -u root -pPASSWORD --auto-repair --check --optimize --all-databases";
$optimizeresult = passthru($optimize);
//Make sure it's completely optimized.
$optimizeresult2 = passthru($optimize);

$i = 0;
while($i<3) {
   $command[$i] = "mysqldump -u root -pPASSWORD database".$i=0?:$i+1." > /FULL_PATH/back-ups/database".$i=0?:$i+1."-$datestamp.sql";
   $result[$i] = passthru($command[$i]);
   $i++;
}
?>

 

I didn't test it but it should be close.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.