Help using MySQLdump in PHP for a non-expert
I am trying to set up an ultra simple database backup. I don't want it
zipped or anything. I am basically just trying to figure out how to use
mysqldump on a php page, because I have spent the last hour+ reading web
sites that were way beyond my level.
Does anyone have an ultra simple php routine that does this. I really want
bare minimum, so I can figure it out.
Thanks
Re: Help using MySQLdump in PHP for a non-expert
Message-ID: <FJ6dnWNqjeeUQajbnZ2dnUVZ_tqnnZ2d [at] comcast.com> from Kevin
Marks contained the following:
>Does anyone have an ultra simple php routine that does this. I really want
>bare minimum, so I can figure it out.
Here's what I use.
<?php
//backup function
function datadump ($table,$dir) {
if(!file_exists($dir.$table."_".date('d-M-Y').".sql")){
$result .= "-- Dump of $table \n";
$result .= "-- Dump DATE : " . date("d-M-Y") ."\n\n";
$query = mysql_query("select * from $table");
$num_fields = [at] mysql_num_fields($query);
$numrow = mysql_num_rows($query);
while($row=mysql_fetch_array($query)) {
$result .= "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])) $result .= "'$row[$j]'" ; else $result
..= "''";
if ($j<($num_fields-1)) $result .= ", ";
}
$result .= ");\n";
}
//save as a file
$filename = $dir.$table."_".date('d-M-Y').".sql";
if (!$handle = fopen($filename, 'w')) {
echo "Cannot open file ($filename)";
exit;
}
// Write to our opened file.
if (fwrite($handle, $result) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
fclose($handle);
//return $result . "\n\n\n";
echo "Backup <a
href='".$dir.$table."_".date('d-M-Y').".sql
'>".$table."_".date('d-M-Y').".sql</a> created<br>";
}
else{
echo "<a href='".$dir.$table."_".date('d-M-Y').".sql
'>".$table."_".date('d-M-Y').".sql </a><br>";
}
}
?>
--
Geoff Berrow 0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011
Re: Help using MySQLdump in PHP for a non-expert
Actually I was looking to use mysqldump for the entire database backup. I
will look this over though and try to figure it out.
"Geoff Berrow" <blthecat [at] ckdog.co.uk> wrote in message
news:5fob33pempotqgjshfg7ieit559bqvqgco [at] 4ax.com...
> Message-ID: <FJ6dnWNqjeeUQajbnZ2dnUVZ_tqnnZ2d [at] comcast.com> from Kevin
> Marks contained the following:
>
>>Does anyone have an ultra simple php routine that does this. I really
>>want
>>bare minimum, so I can figure it out.
> Here's what I use.
>
> <?php
> //backup function
> function datadump ($table,$dir) {
> if(!file_exists($dir.$table."_".date('d-M-Y').".sql")){
> $result .= "-- Dump of $table \n";
> $result .= "-- Dump DATE : " . date("d-M-Y") ."\n\n";
>
> $query = mysql_query("select * from $table");
> $num_fields = [at] mysql_num_fields($query);
> $numrow = mysql_num_rows($query);
>
> while($row=mysql_fetch_array($query)) {
> $result .= "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])) $result .= "'$row[$j]'" ; else $result
> .= "''";
> if ($j<($num_fields-1)) $result .= ", ";
> }
> $result .= ");\n";
> }
> //save as a file
> $filename = $dir.$table."_".date('d-M-Y').".sql";
> if (!$handle = fopen($filename, 'w')) {
> echo "Cannot open file ($filename)";
> exit;
> }
> // Write to our opened file.
> if (fwrite($handle, $result) === FALSE) {
> echo "Cannot write to file ($filename)";
> exit;
> }
> fclose($handle);
> //return $result . "\n\n\n";
> echo "Backup <a
> href='".$dir.$table."_".date('d-M-Y').".sql
> '>".$table."_".date('d-M-Y').".sql</a> created<br>";
> }
> else{
> echo "<a href='".$dir.$table."_".date('d-M-Y').".sql
> '>".$table."_".date('d-M-Y').".sql </a><br>";
> }
> }
>
>
> ?>
> --
> Geoff Berrow 0110001001101100010000000110
> 001101101011011001000110111101100111001011
> 100110001101101111001011100111010101101011
Re: Help using MySQLdump in PHP for a non-expert
I'd suggest downloading MySQLDumper, which does it all quite nicely,
including "offsite backups", once you get past the occasional
slightly confusing English.
You can find it at www.mysqldumper.de
Regards
Mark
Re: Help using MySQLdump in PHP for a non-expert
"Kevin Marks" <kevin [at] westportwa.com> schreef in bericht
news:Ou6dnaTWdJhhaKjbnZ2dnUVZ_jednZ2d [at] comcast.com...
> Actually I was looking to use mysqldump for the entire database backup. I
> will look this over though and try to figure it out.
>
<?php
$x = system('mysqldump --help');
?>
Re: Help using MySQLdump in PHP for a non-expert
On Apr 30, 2:57 pm, "Kevin Marks" <k... [at] westportwa.com> wrote:
> Actually I was looking to use mysqldump for the entire database backup. I
> will look this over though and try to figure it out.
is the idea to let your users be in charge of dumping their own
database?
Do you also provide them with a way to upload and restore this?
could you allow them access to an install of phpmyadmin, locked to
their user, that way they can backup in both directions?
what if they dont do it manually, they will still come back to you if
it blows up!
I use cron and copy the dump to the users sftp space.
if the database gets large, and they dump at inconvenient times... not
to mention an unresponsive page while this is happening.
the line in the crontab:
#every night at 5 past midnight local time
0 5 * * * "/path/to/mysql/bin/mysqldump" -uUsername -pPassword
databasename > "/path/to/users/sftp/space/backup/databasename.sql"
Re: Help using MySQLdump in PHP for a non-expert
Post removed (X-No-Archive: yes)