PDA

View Full Version : پشتیبان گیری از سایت _ back up



pooyesh
شنبه 10 دی 1384, 18:58 عصر
با سلام
من می خواهم از درون cms خودم از بانک اطلاعات سایت back up بگیرم
چگونه این کار را انجام دهم
ممنون

moohssenn
یک شنبه 11 دی 1384, 00:16 صبح
اول اینکه بگو cms چی هستش ، آخه بعضی cms خودشون این امکانو دارن .
با یه نرم افزار به دیتابیست وصل شو و این کارو انجام بده . phpmyadmin خوبه با اون میتونی این کارو انجام بدی بعدشم تو سیپنل اکثر هاستینگها این امکان هست

pooyesh
یک شنبه 11 دی 1384, 09:19 صبح
تشکر
ولی این رو خودم هم می دونستم که از طریق phpmyadmin می شه این کار رو کرد
ولی منظورم از درون cms است که خودم نوشتم وسایتم رو مدیریت می کنم می خواستم این کا رو انجا بدم

pooyesh
یک شنبه 11 دی 1384, 20:44 عصر
کسی نمودنه یعنی کسی تا حالا این کار رو نکرده؟
آیا فقط از طریق cpanel یا phpmyadmin می شه این کار رو کرد؟

cpuman
سه شنبه 23 اسفند 1384, 17:08 عصر
اینم کد برای پشتیبان گیری از دیتابیس ها



<?php
global $conn; //Share Conection to mysql

//
// Set VERBOSE to 1 for debugging info..
//
define("VERBOSE", 0);

//
// Increase maximum execution time, but don't complain about it if it isn't
// allowed.
//
@set_time_limit(1200);

// -----------------------
// The following functions are adapted from phpMyAdmin and upgrade_20.php
//
function gzip_PrintFourChars($Val)
{
for ($i = 0; $i < 4; $i ++)
{
$return .= chr($Val % 256);
$Val = floor($Val / 256);
}
return $return;
}

//
// This function returns the "CREATE TABLE" syntax for mysql dbms...
//
function get_table_def_mysql($table, $crlf)
{
global $drop, $conn;

$schema_create = "";
$field_query = "SHOW FIELDS FROM $table";
$key_query = "SHOW KEYS FROM $table";

//
// If the user has selected to drop existing tables when doing a restore.
// Then we add the statement to drop the tables....
//
if ($drop == 1)
{
$schema_create .= "DROP TABLE IF EXISTS $table;$crlf";
}

$schema_create .= "CREATE TABLE $table($crlf";

//
// Ok lets grab the fields...
//
$result = $conn->Execute($field_query);
if(!$result)
{
die( "Failed in get_table_def (show fields)");
}

$result->Move(0);
while (!$result->EOF)
{
$schema_create .= ' ' . $result->fields['Field'] . ' ' . $result->fields['Type'];

if(!empty($result->fields['Default']))
{
$schema_create .= ' DEFAULT \'' . $result->fields['Default'] . '\'';
}

if($result->fields['Null'] != "YES")
{
$schema_create .= ' NOT NULL';
}

if($result->fields['Extra'] != "")
{
$schema_create .= ' ' . $result->fields['Extra'];
}

$schema_create .= ",$crlf";
$result->MoveNext();
}
//
// Drop the last ',$crlf' off ;)
//
$schema_create = ereg_replace(',' . $crlf . '$', "", $schema_create);

//
// Get any Indexed fields from the database...
//
$result = $conn->Execute($key_query);
if(!$result)
{
die( "FAILED IN get_table_def (show keys)");
}

$result->Move(0);
while (!$result->EOF)
{
$kname = $result->fields['Key_name'];

if(($kname != 'PRIMARY') && ($result->fields['Non_unique'] == 0))
{
$kname = "UNIQUE|$kname";
}

if(!is_array($index[$kname]))
{
$index[$kname] = array();
}

$index[$kname][] = $result->fields['Column_name'];
$result->MoveNext();
}

while(list($x, $columns) = @each($index))
{
$schema_create .= ", $crlf";

if($x == 'PRIMARY')
{
$schema_create .= ' PRIMARY KEY (' . implode($columns, ', ') . ')';
}
elseif (substr($x,0,6) == 'UNIQUE')
{
$schema_create .= ' UNIQUE ' . substr($x,7) . ' (' . implode($columns, ', ') . ')';
}
else
{
$schema_create .= " KEY $x (" . implode($columns, ', ') . ')';
}
}

$schema_create .= "$crlf);";

if(get_magic_quotes_runtime())
{
return(stripslashes($schema_create));
}
else
{
return($schema_create);
}

} // End get_table_def_mysql

//
// This function is for getting the data from a mysql table.
//

function get_table_content_mysql($table, $handler)
{
global $conn;

// Grab the data from the table.
if (!($result = $conn->Execute("SELECT * FROM $table")))
{
die( "Failed in get_table_content (select *)");
}

// Loop through the resulting rows and build the sql statement.
if (!$result->EOF)
{
$handler("\n#\n# Table Data for $table\n#\n");
$field_names = array();


// Grab the list of field names.
$num_fields = $result->FieldCount();
$table_list = '(';
for ($j = 0; $j < $num_fields; $j++)
{
$field_names[$j] = $result->FetchField($j);
$table_list .= (($j > 0) ? ', ' : '') . $field_names[$j]->name;

}
$table_list .= ')';

$result->Move(0);
do
{
// Start building the SQL statement.
$schema_insert = "INSERT INTO $table $table_list VALUES(";

// Loop through the rows and fill in data for each column
for ($j = 0; $j < $num_fields; $j++)
{
$schema_insert .= ($j > 0) ? ', ' : '';

if(!isset($result->fields[$field_names[$j]->name]))
{
//
// If there is no data for the column set it to null.
// There was a problem here with an extra space causing the
// sql file not to reimport if the last column was null in
// any table. Should be fixed now :) JLH
//
$schema_insert .= 'NULL';
}
elseif ($result->fields[$field_names[$j]->name] != '')
{
$schema_insert .= '\'' . addslashes($result->fields[$field_names[$j]->name]) . '\'';
}
else
{
$schema_insert .= '\'\'';
}
}

$schema_insert .= ');';

// Go ahead and send the insert statement to the handler function.
$handler(trim($schema_insert));
$result->MoveNext();

}
while (!$result->EOF);
}

return(true);
}

function output_table_content($content)
{
global $tempfile;

//fwrite($tempfile, $content . "\n");
//$backup_sql .= $content . "\n";
echo $content ."\n";
return;
}
//
// End Functions
// -------------


//
// Begin program proper
//
$error = false;

//Here is Table Names
$tables = array('members','sessions');

$gzipcompress = 0;

$drop = 1;

header("Pragma: no-cache");
$do_gzip_compress = FALSE;
if( $gzipcompress )
{
$phpver = phpversion();

if($phpver >= "4.0")
{
if(extension_loaded("zlib"))
{
$do_gzip_compress = TRUE;
}
}
}
if($do_gzip_compress)
{
@ob_start();
@ob_implicit_flush(0);
header("HTTP/1.1 200 OK");
header("Status: 200 OK");
header("Pragma: public");
header("Content-Type: application/x-gzip; name=\"backup.sql.gz\"");
header("Content-disposition: attachment; filename=backup.sql.gz");
}
else if (!$do_gzip_compress)
{
header("HTTP/1.1 200 OK");
header("Status: 200 OK");
header("Pragma: public");
header("Content-Type: application/octet-stream");
header("Content-disposition: attachment; filename=backup.sql");
}

//
// Build the sql script file...
//
echo "#\n";
echo "# Backup Script\n";
echo "# Dump of tables for ".DB_DATABASE."\n";
echo "#\n# DATE : " . gmdate("d-m-Y H:i:s", time()) . " GMT\n";
echo "#\n";

for($i = 0; $i < count($tables); $i++)
{
$table_name = $tables[$i];

$table_def_function = "get_table_def_mysql";
$table_content_function = "get_table_content_mysql";

if($backup_type != 'data')
{
echo "#\n# TABLE: " . $table_prefix . $table_name . "\n#\n";
echo $table_def_function($table_prefix . $table_name, "\n") . "\n";
}

if($backup_type != 'structure')
{
$table_content_function($table_prefix . $table_name, "output_table_content");
}
}

if($do_gzip_compress)
{
$Size = ob_get_length();
$Crc = crc32(ob_get_contents());
$contents = gzcompress(ob_get_contents());
ob_end_clean();
echo "\x1f\x8b\x08\x00\x00\x00\x00\x00".substr($contents, 0, strlen($contents) - 4).gzip_PrintFourChars($Crc).gzip_PrintFourChars($ Size);
}

?>

alizadehsoha
دوشنبه 10 شهریور 1393, 19:58 عصر
backup_tables('localhost','username','password','b log');


/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$name,$tables = '*')
{

$link = mysql_connect($host,$user,$pass);
mysql_select_db($name,$link);

//get all of the tables
if($tables == '*')
{
$tables = array();
$result = mysql_query('SHOW TABLES');
while($row = mysql_fetch_row($result))
{
$tables[] = $row[0];
}
}
else
{
$tables = is_array($tables) ? $tables : explode(',',$tables);
}

//cycle through
foreach($tables as $table)
{
$result = mysql_query('SELECT * FROM '.$table);
$num_fields = mysql_num_fields($result);

$return.= 'DROP TABLE '.$table.';';
$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
$return.= "\n\n".$row2[1].";\n\n";

for ($i = 0; $i < $num_fields; $i++)
{
while($row = mysql_fetch_row($result))
{
$return.= 'INSERT INTO '.$table.' VALUES(';
for($j=0; $j<$num_fields; $j++)
{
$row[$j] = addslashes($row[$j]);
$row[$j] = ereg_replace("\n","\\n",$row[$j]);
if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
if ($j<($num_fields-1)) { $return.= ','; }
}
$return.= ");\n";
}
}
$return.="\n\n\n";
}

//save file
$handle = fopen('db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');
fwrite($handle,$return);
fclose($handle);
}