Execute a database backup query from PHP file[ad code=4 align=center]
Below is an example of using SELECT INTO OUTFILE query for creating table backup :
include ‘config.php’;
include ‘opendb.php’;$tableName = ‘mypet’;
$backupFile = ‘backup/mypet.sql’;
$query = “SELECT * INTO OUTFILE ‘$backupFile’ FROM $tableName”;
$result = mysql_query($query);include ‘closedb.php’;
To restore the backup you just need to run LOAD DATA INFILE query like this :
include ‘config.php’;
include ‘opendb.php’;$tableName = ‘mypet’;
$backupFile = ‘mypet.sql’;
$query = “LOAD DATA INFILE ‘backupFile’ INTO TABLE $tableName”;
$result = mysql_query($query);include ‘closedb.php’;
It’s a good idea to name the backup file as tablename.sql so you’ll know from which table the backup file is
Run mysqldump using system() function
The system() function is used to execute an external program. Because MySQL already have built in tool for creating MySQL database backup (mysqldump) let’s use it from our PHP script
include ‘config.php’;
include ‘opendb.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);include ‘closedb.php’;
original article http://www.php-mysql-tutorial.com/wikis/mysql-tutorials/using-php-to-backup-mysql-databases.aspx