FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


PHP Script for backing up MySQL





welshsteve
Hi everyone.

I am building a website for a sports league at the moment and am putting the finishing touches to an admin panel for them. I'd like to have a feature which lets them backup the entire MySQL database to one of the these outputs.

1. On screen (like in phpMyAdmin), so they can then copy and paste into a text file and save somewhere on their computer.

or

2. To a text file. Either: on screen so they can use the "Save As" feature of the browser. Or open up a common dialog to save the file.

I've tried googling it, but the results I've looked through seem half baked, and a lot of the time refer to local installs of pp and mysql, and not shared hosting which these guys are using.

This is what I have managed so far.

config.php
Code:

<?php
$dbuser = "myusername";
$dbpass = "mypassword";
$dbhost = "hostaddress";   
$dbname = "database";
$dbcon = mysql_connect($dbhost, $dbuser, $dbpass)
   or die("Unable to connect to database");
mysql_select_db("$dbname");
?>


mysqldump.php
Code:

<?php
include 'config.php';

$backupFile = $dbname . date("Y-m-d-H-i-s") . '.gz';
$command = "mysqldump --opt -h $dbhost -u $dbuser -p $dbpass $dbname | gzip > $backupFile";
system($command);

?>


But this doesn't work. It creates a gz archive in the same folder as the script on the shared hosting server, but the file inside the archive only contains the following.

Usage: mysqldump [OPTIONS] database [tables]
OR mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
OR mysqldump [OPTIONS] --all-databases [OPTIONS]
For more options, use mysqldump --help



If I can't get it to work I'll just have to write some instructions for them using phpMyAdmin to do it.

Is anybody able to help?
D'Artagnan
the problem is actually on the command you are sending to the server

Code:
mysqldump --opt -h $dbhost -u $dbuser -p $dbpass $dbname | gzip > $backupFile


i'm not 100% sure but try

Code:
mysqldump --opt -h $dbhost -u $dbuser -p$dbpass $dbname | gzip > $backupFile


yes i only removed a space between the -p and the $dbpass, mysqldump expects you to pass the parameter this way and will ask for the password and get confused by the parameter otherwise...
welshsteve
Hi D'Artagnan.

This gives the same result I'm afraid! Sad
jmraker
You posted a strangely similar question about being unable to use mysqldump about 9 to 10 months ago

http://www.frihost.com/forums/vt-129769.html

I gave you an example of how to it all in php without mysqldump.
Related topics
Phoenix free PHP scripts
error in my PHP script
how can i make a php script for enter username and password?
Inserting data to MYSQL with a PHP script
Running a "Hello World" php script
[PHP] Running Cron Jobs Via PHP Script
Simple PHP script not working
PHP Script- How can we install it
I need a VERY simple php script
PHP Script needed
Logging systeem php script
Need php script 50 frih$ if you do
PHP Script Needed for 250 frih$
PHP script to create email addresses/ftp accounts/etc...
How do you Create a New Database using a php script?
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.