Skip to content

Instantly share code, notes, and snippets.

@Fil
Forked from anonymous/mysql_backup.php
Created January 2, 2013 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fil/4434273 to your computer and use it in GitHub Desktop.
Save Fil/4434273 to your computer and use it in GitHub Desktop.
#! /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