codebang
Posts: 74
|
| Posted: 01/24/2009, 4:52 AM |
|
hello....
Well, first of all i want to thank everybody,for solving my last navigation problem.
Well i want to ask u if there is a way to export my whole database using CCS, by the click of a button.
as i will need the whole database.
second of all...how can i import it back to my original databse, with the new and old records....
Any ideas...
Thank u..
Salah Hafiz
|
damian
Posts: 838
|
| Posted: 01/27/2009, 4:40 AM |
|
here you go -
this creates a backup on loading the page and creates a link to the backup
please note it creates a new backup file every time its run so you might want to do a cleanup every now and again or you can modify to overwrite each time
create a folder called dbbackup and ensure you give full permissions
use notepad to create a file called exportdb.php and paste the following into it:
<html><head>
<title>mySQL Database Backup</title></head>
<body>
<a href="dbbackup/
<?php
backup_my_tables('localhost','username','password,'database');
/* backup the db OR just a table */
function backup_my_tables($host,$user,$pass,$name,$tables = '*')
{
$myback = time();
echo $myback;
$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('dbbackup/'.$myback.'.sql','w+');
fwrite($handle,$return);
fclose($handle);
}
?>
.sql">Download New Backup</a>
</body>
</html>
replace the variables from:
backup_my_tables('localhost','username','password,'database');
im sure you can work out how to get it into ccs and make the page restricted.... it took me long enough to serach this code, test it, modify etc.... over to you for the next step - please post your final solution if you run with this.
credit: most of the work was done here: http://davidwalsh.name/backup-mysql-database-php
and i just changed a couple of things to suit me....
_________________
if you found this post useful take the time to help someone else.... :)
|