Created
January 2, 2013 12:29
-
-
Save anonymous/4434266 to your computer and use it in GitHub Desktop.
daily backup of all mysql tables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/php -q | |
<?php | |
define ('MYSQL_ROOT', 'root'); | |
define ('MYSQL_PASS', 'passwd'); | |
# connexion a la base | |
$conn = mysql_connect('localhost', 'root', 'tapioca'); | |
if (!$conn) die ('erreur connexion DB '.mysql_error()); | |
$f = mysql_query("SHOW DATABASES"); | |
if (!$f) die ('erreur SHOW DATABASES '.mysql_error()); | |
$bases = array(); | |
while ($t = mysql_fetch_row($f)) { | |
if ($t[0] !== 'mysql') | |
$bases[] = $t[0]; | |
} | |
mysql_close($conn); | |
foreach ($bases as $base) | |
backup_base($base); | |
exit(0); | |
function backup_base($base) { | |
$conn = mysql_connect('localhost', 'root', 'tapioca'); | |
$f = mysql_query("SHOW TABLES FROM $base"); | |
if (!$f) die ('erreur SHOW TABLES '.mysql_error()); | |
printf ("\n% 20s: ", $base); | |
$tables = array(); | |
while ($t = mysql_fetch_row($f)) { | |
if ($t[0] != 'mysql') | |
$tables[] = $t[0]; | |
} | |
mysql_close($conn); | |
foreach ($tables as $table) | |
backup_table($table, $base); | |
echo "\n"; | |
} | |
function backup_table($table, $base) { | |
# ne pas backuper spip_index*** | |
if (preg_match(',^spip_index,', $table)) { | |
echo "-"; | |
return; | |
} | |
# dumper la table dans le repertoire /var/state/mysql/$base/ | |
@mkdir ("/var/state/mysql/$base"); | |
chdir ("/var/state/mysql/$base") | |
OR die ("erreur chdir /var/state/mysql/$base"); | |
system("mysqldump --opt -u".MYSQL_ROOT.' -p'.MYSQL_PASS." '$base' '$table'| gzip - > /var/state/mysql/$base/$table.gz"); | |
echo "+"; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment