Connecting Tech Pros Worldwide Help | Site Map

Save file in locaation automatically

Familiar Sight
 
Join Date: Jun 2008
Posts: 164
#1: Nov 3 '08
Hi,

I have this script for backing up a database that when it is run the option comes up to save the file. Can anyone show me how I would change this script so that the script automatically saves the file in a specified folder?

[HTML]<?php
set_time_limit(0);
$mysqldump_version="1.02";


$output_messages=array();

$mysql_host= 'localhost';
$mysql_database= '**';
$mysql_username= '**';
$mysql_password= '**';

_mysql_test($mysql_host,$mysql_database, $mysql_username, $mysql_password);



//ob_start("ob_gzhandler");
header('Content-type: text/plain');
header('Content-Disposition: attachment; filename="'.$mysql_host."_".$mysql_database."_".da te('YmdHis').'.sql"');
echo "/*mysqldump.php version $mysqldump_version */\n";
_mysqldump($mysql_database);


function _mysqldump($mysql_database)
{
$sql="show tables;";
$result= mysql_query($sql);
if( $result)
{
while( $row= mysql_fetch_row($result))
{
_mysqldump_table_structure($row[0]);

_mysqldump_table_data($row[0]);

}
}
else
{
echo "/* no tables in $mysql_database */\n";
}
mysql_free_result($result);
}

function _mysqldump_table_structure($table)
{
echo "/* Table structure for table `$table` */\n";
if( isset($_REQUEST['sql_drop_table']))
{
echo "DROP TABLE IF EXISTS `$table`;\n\n";
}
if( isset($_REQUEST['sql_create_table']))
{

$sql="show create table `$table`; ";
$result=mysql_query($sql);
if( $result)
{
if($row= mysql_fetch_assoc($result))
{
echo $row['Create Table'].";\n\n";
}
}
mysql_free_result($result);
}
}

function _mysqldump_table_data($table)
{

$sql="select * from `$table`;";
$result=mysql_query($sql);
if( $result)
{
$num_rows= mysql_num_rows($result);
$num_fields= mysql_num_fields($result);

if( $num_rows > 0)
{
echo "/* dumping data for table `$table` */\n";

$field_type=array();
$i=0;
while( $i < $num_fields)
{
$meta= mysql_fetch_field($result, $i);
array_push($field_type, $meta->type);
$i++;
}

//print_r( $field_type);
echo "insert into `$table` values\n";
$index=0;
while( $row= mysql_fetch_row($result))
{
echo "(";
for( $i=0; $i < $num_fields; $i++)
{
if( is_null( $row[$i]))
echo "null";
else
{
switch( $field_type[$i])
{
case 'int':
echo $row[$i];
break;
case 'string':
case 'blob' :
default:
echo "'".mysql_real_escape_string($row[$i])."'";

}
}
if( $i < $num_fields-1)
echo ",";
}
echo ")";

if( $index < $num_rows-1)
echo ",";
else
echo ";";
echo "\n";

$index++;
}
}
}
mysql_free_result($result);
echo "\n";
}

function _mysql_test($mysql_host,$mysql_database, $mysql_username, $mysql_password)
{
global $output_messages;
$link = mysql_connect($mysql_host, $mysql_username, $mysql_password);
if (!$link)
{
array_push($output_messages, 'Could not connect: ' . mysql_error());
}
else
{
array_push ($output_messages,"Connected with MySQL server:$mysql_username@$mysql_host successfully");

$db_selected = mysql_select_db($mysql_database, $link);
if (!$db_selected)
{
array_push ($output_messages,'Can\'t use $mysql_database : ' . mysql_error());
}
else
array_push ($output_messages,"Connected with MySQL database:$mysql_database successfully");
}

}
?>[/HTML]
dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,095
#2: Nov 3 '08

re: Save file in locaation automatically


A website cannot access your file system. If it was possible to do that, I could give you a website to go to and if I was an evil hacker the website would delete all the files on your computer or automatically implant a virus.

PHP can however save files on the server through the user apache (if it's a linux machine) but if you're just trying to back up your mysql database, you do not need PHP.

You can run a command from the command line, or better yet, create a cron job or scheduled task that will back up your database regularly, like so

mysqldump -uuser -ppassword databaseName > /path/to/file.sql

if you put that line into the a cron job. If you need that file on your own machine, You can FTP and download it or write a script to automatically download it every day or week for you.

Google is your best friend. Do some research and come back with questions.





Dan
Reply