kropek4767 Początkujący
Dołączył: 20 Sty 2013 Posty: 5
|
Wysłany: Sob Mar 09, 2013 5:49 pm Temat postu: |
|
|
Ja za pomocą tego kodu zrzucam zawartość bazy do pliku, nie wiem czy się w twoim przypadku sprawdzi. Wersje starsze niż 2 dni są automatycznie usuwane.
Kod: | //Konfiguracja
$cfg['rozszerzenie'] = '.sql';
$cfg['sciezka'] = 'J:Kopie zaposawe DB\\';
$cfg['host'] = 'localhost';
$cfg['login'] = '***';
$cfg['pass'] = '***';
$cfg['db'] = 'nazwa_bazy_danych';
//Funkcje
function data_zapisu($plik,$maska,$rozszerzenie)
{
return str_replace('_'.$maska,null,basename($plik,$rozszerzenie));
}
function usun_stare_kopie($data,$sciezka,$maska,$rozszerzenie)
{
if(strtotime($data) < strtotime(date('Y-m-d').'-1 day'))
{
unlink ($sciezka.$data.'_'.$maska.$rozszerzenie);
}
}
class DbDump
{
public $drop = TRUE;
private $db;
private $sql;
private $fields = array();
public function __construct($host = FALSE, $login = FALSE, $pass = FALSE, $db = FALSE)
{
@mysql_connect($host, $login, $pass)
or die('Blad przy polaczeniu z baza danych');
@mysql_select_db($db)
or die('Blad przy wyoborze bazy danych');
mysql_query('SET NAMES utf8');
$this->db = $db;
}
private function get_tables()
{
$query = mysql_query('SHOW TABLES');
$tables = array();
while($row = mysql_fetch_row($query))
{
$tables[] = $row[0];
}
return $tables;
}
private function show_create_table($table)
{
$query = mysql_query("SHOW CREATE TABLE `{$table}`");
$row = mysql_fetch_row($query);
return $row[1];
}
private function fields_info($table)
{
$query = mysql_query("SELECT * FROM `{$table}`");
$this->fields = array();
while($field = mysql_fetch_field($query))
{
$this->fields[$field->name] = in_array(strtolower($field->type), array('tinyint', 'smallint', 'mediumint', 'int', 'bigint')) ? TRUE : FALSE;
}
return TRUE;
}
private function field_str()
{
$tmp = array();
foreach($this->fields as $field=>$val)
{
$tmp[] = '`'.$field.'`';
}
return implode(', ', $tmp);
}
private function get_rows($table)
{
$query = mysql_query("SELECT * FROM {$table}");
$rows = array();
while($row = mysql_fetch_assoc($query))
{
$rows[] = $row;
}
return $rows;
}
private function row_str($rows)
{
$tmp = array();
foreach($rows as $row=>$val)
{
if($val === NULL)
{
$tmp[] = 'NULL';
}
elseif($this->fields[$row] == TRUE)
{
$tmp[] = $val;
}
else
{
$tmp[] = '"'.mysql_real_escape_string($val).'"';
}
}
return implode(', ', $tmp);
}
public function create()
{
$this->sql = "#\n# Zrzut bazy danych `{$this->db}`\n#\n\n";
foreach($this->get_tables() as $table)
{
$this->sql .= "#\n# Struktura tabeli `{$table}`\n#\n\n";
if($this->drop == TRUE)
{
$this->sql .= "DROP TABLE IF EXISTS `{$table}`;\n";
}
$this->sql .= $this->show_create_table($table).";\n\n";
$this->sql .= "#\n# Zrzut danych tabeli `{$table}`\n#\n\n";
$this->fields_info($table);
$field_str = $this->field_str();
foreach($this->get_rows($table) as $row)
{
$row_str = $this->row_str($row);
$this->sql .= "INSERT INTO `{$table}` ({$field_str}) VALUES ({$row_str});\n";
}
$this->sql .= "\n\n";
}
return $this->sql;
}
}
// Funkcje KONIEC
// Zrzucamy dane z bazy
$dumper = new DbDump($cfg['host'], $cfg['login'], $cfg['pass'], $cfg['db']);
$wynik = $dumper->create();
//Zapisujemy dane do pliku
$fn = fopen($cfg['sciezka'].date('Y-m-d').'_'.$cfg['db'].$cfg['rozszerzenie'], 'x');
fwrite($fn, $wynik);
fclose($fn);
//Usuwamy starsze pliki niż jeden dzień
$pliki = glob($cfg['sciezka'].'*'.$cfg['db'].$cfg['rozszerzenie']);
$i = 0;
foreach ($pliki as $plik)
{
$daty[$i] = data_zapisu($plik,$cfg['db'],$cfg['rozszerzenie']);
$i++;
}
$i = 0;
foreach ($daty as $data)
{
usun_stare_kopie($data,$cfg['sciezka'],$cfg['db'],$cfg['rozszerzenie']);
$i++;
}
?> |
|
|