MYSQL Backup from simple text to phpmyadmin - php

I have been seriously searching for an SQL code to backup my database. I use xampp as my local server and also phpmyadmin. I simply want to do something like:
<?php
$conn = mysqli_connect("localhost", "root", " ", "products");
if(!$conn){
die("Unable to connect ".mysql_error());
}else{
$backup = "BACKUP DATABASE products";
$backup_query = mysqli_query($conn, $backup)
}
?>
How do I backup my database and output it in .sql format on the local computer and possibly upload to recover a damaged database?
Thank you so much!

You can use the shell statement "mysqldump" and call it with php like:
<?php
$dbhost = 'localhost';
$dbuser = 'username';
$dbpassword = 'password';
$dbname = 'datenbankname';
$dumpfile = 'backups/' . $dbname . '_' . date("Y-m-d_H-i-s") . '.sql.gz';
passthru("mysqldump --user=$dbuser --password=$dbpassword --host=$dbhost $dbname | gzip -c > $dumpfile");
echo "-- Dump finished -- ";
echo $dumpfile;

Related

How can I export MySQL database with a PHP script?

I need one script that when I open the script.php page for example, that export me one database from MySQL to an SQL document, and I don't know how to do it in PHP, can some one help me with that?
Edited Code:
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpwd = "";
$dbname = "activmanagement";
$dumpfile = $dbname . "_" . date("Y-m-d_H-i-s") . ".sql";
passthru("D:/wamp/bin/mysql/mysql5.6.17/bin/mysqldump --opt --host=$dbhost --user=$dbuser --password=$dbpwd $dbname > $dumpfile");
// report - disable with // if not needed
// must look like "-- Dump completed on ..."
echo "$dumpfile "; passthru("tail -1 $dumpfile");
?>
This line:
passthru("D:/wamp/bin/mysql/mysql5.6.17/bin/mysqldump --opt --host=$dbhost --user=$dbuser --password=$dbpwd $dbname > $dumpfile");
Refer PHP - exec() vs system() vs passthru(). You just need something like this
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'activmanagement';
$sql_backup_file = $dbname . date("Y-m-d-H-i-s") . '.gz';
$dump = "mysqldump --opt -h $dbhost -u $dbuser -p $dbpass $dbname | gzip > $sql_backup_file";
system($dump);
?>

Not able to connect to mysql php/xampp

my I am trying to make login registration page in php.
apache and mysql is up and running.
I am executing the following code
<?php
$db_host = "localhost:777";
$db_username = "root";
$db_pass = "";
$db_name = "login";
mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
mysql_select_db("$db_name") or die ("no database");
?>
I am using localhost:777 because port 80 is being used by skype.
phpMyAdmin is up and running.
The output i am getting is nothing but the above code only.
can anyone help me on this issue?
Use MySQLi or PDO not MySQL.
You're passing the variables as a string using " " in the mysql_connect function
The syntax is as follows: mysqli_connect(host,username,password,dbname,port,socket);
<?php
$db_host = "localhost";
$db_port = "777";
$db_username = "root";
$db_pass = "";
$db_name = "login";
$conn = mysqli_connect($db_host,$db_username,$db_pass,$db_name,$db_port);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Try that and it should work. Please make sure you understand the code above and the error you made. Do not simply copy and paste please you will not learn from it.

How to create a backup of database using php script. I am facing a error

I m trying this code but it shows a error
"Could not take data backup:
Can't create/write to file '\transfer\employees.sql' (Errcode: 2)"
when I click on my button to create a backup file.
<?php
$dbhost = "localhost";
$dbusername = "root";
$dbpassword = "";
$dbdatabase = "ghis_verf";
$conn = mysql_connect($dbhost,$dbusername,$dbpassword);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$table_name = "employees";
$backup_file = "/transfer/employees.sql";
$sql = "SELECT * INTO OUTFILE '$backup_file' FROM $table_name";
mysql_select_db($dbdatabase,$conn) or die("Cannot select database");
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not take data backup: ' . mysql_error());
}
echo "Backedup data successfully\n";
?>
I think you do not have folder /transfer at the root of your web server or does not have permissions to write to it.
Make sure you can perform su -c "touch /transfer/employees.sql" www-data (replace www-data with proper user for you system). If no - check permissions or folder existence.
It may be due insufficient write privilege. If you're using Linux, you may want to create the folder first as a placeholder with sufficient permissions for Apache to write to:
$ cd /path/to/app
$ mkdir transfer
$ chmod 777 transfer
See if the following PHP script works for you:
<?php
$dbhost = "localhost";
$dbusername = "root";
$dbpassword = "";
$dbdatabase = "ghis_verf";
$table_name = "employees";
$backup_file = "transfer/employees.sql";
$cmd = "mysqldump -h {$dbhost} -u {$dbusername} -p '{$dbpassword}' {$dbdatabase} {$table_name} > {$backup_file}";
system($cmd);
?>
Also /transfer is the absolute root path in your file system. Maybe it's something like /var/www/transfer? Or change it to absolute path such as transfer/.
Hope it helps.
Please check the link below to export database via PHP code. Hope this will work for you.
http://phptipmaster.blogspot.com/2013/01/mysql-database-export-via-php-code.html

PHP mysqldump file import

I want backup my local database and import that database from live website. I'm tried something. please see below..
My Backup code
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = 'password';
$dbname = "database";
$backupfile ='database.sql';
$backupdir = dirname(__FILE__);
$source = $backupdir.'/'.$backupfile;
system("mysqldump -h $dbhost -u $dbuser --password='$dbpass' $dbname > $backupfile");
?>
<form action="http://www.example.com/restore_database.php" method="post">
<input type="text" name="backup_file" value="<?php echo $source; ?>"/>
<input type="submit" />
</form>
my restore_database.php (example.com/restore_database.php)
<?php
$dbhost = "localhost";
$dbuser = "username";
$dbpass = 'password';
$dbname = "database";
$filename = $_POST['backup_file'];
mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to MySQL server: ' . mysql_error());
mysql_select_db($dbname) or die('Error selecting MySQL database: ' . mysql_error());
$templine = '';
$lines = file($filename);
foreach ($lines as $line) {
if (substr($line, 0, 2) == '--' || $line == '')
continue;
$templine .= $line;
if (substr(trim($line), -1, 1) == ';') {
mysql_query($templine) or print('Error performing query \'<strong>' . $templine . '\': ' . mysql_error() . '<br /><br />');
$templine = '';
}
}
?>
In my localhost , I'm successfully tested this. for my live site I think file path not detect correctly. I have no idea about this. please help me. Thanks.
assuming that the live site lay on the ip 134.122.12.109
and you are using UNIX
on your local host create these files and ~/backupdir directory
backup.sh
#!/bin/bash
mysqldump -uusername -pPassword database > ~/backupdir/backupFile.sql
restore.sh
#!/bin.bash
mysql -h 134.122.12.109 -uliveSiteUsername -pLiveSitePassword database < ~/backupdir/backupFile.sql
now you can backup your localhost by running ./backup.sh
and you can restore the live site by using ./restore.sh
hope that demonstrated the idea.
responding to your comment
let the restore_database.php be
<?php
$dbhost = "localhost";
$dbuser = "username";
$dbpass = 'password';
$dbname = "database";
$filename = $_POST['backup_file'];
system("mysql -h $dbhost -u $dbuser --password='$dbpass' $dbname < $filename");
?>

PHP script to restore a database into another empty database

This question might seem a duplicate, However i tried all the examples to restore my database but none seems to work for me.
I am trying to run a script that will restore my backup sql file to new database. I tried these lines to restore my database, but none seem to work.
$mysql_host = 'localhost';
$mysql_username = 'my_username';
$mysql_password = 'somepassword';
$db_name = 'test_db';
$source = 'C:/wamp/www/my_folder/test_db.sql';
$conn = mysql_connect( $mysql_host, $mysql_username, $mysql_password ) or die('Error connecting to MySQL server: ' . mysql_error());
mysql_query("CREATE DATABASE $db_name", $conn ) or die('Error connecting to MySQL server: ' . mysql_error());
restore_my_database( $mysql_host, $mysql_username, $mysql_password, $db_name, $source );
function restore_my_database( $mysql_host, $mysql_username, $mysql_password, $db_name, $source ) {
exec("mysql --opt -h $mysql_host -u $mysql_username -p $mysql_password $db_name < $source");
}
I also tried in function restore_my_database following lines
$command = "mysqldump --opt -h $mysql_host -u $mysql_username -p $mysql_password $db_name > $source";
system($command);
Upto database create, the code is working fine, but restore is not working.
Can any one help me with the php restore code to restore my database. Thanks in advance
you can achieve it by below code, first get content from .sql file and then execute the query
$source = 'C:/wamp/www/my_folder/test_db.sql';
$conn = mysql_connect( $mysql_host, $mysql_username, $mysql_password ) or die('Error connecting to MySQL server: ' . mysql_error());
mysql_query("CREATE DATABASE $db_name", $conn ) or die('Error connecting to MySQL server: ' . mysql_error());
mysql_select_db($db_name);
$qry = file_get_contents($source);
mysql_query($qry, $conn);
and also increase max execution time, memory limit in php.ini file
-- you have to create a schema:
mysql
mysql> create database [your schema name];
mysql> grant all privileges on [your schema name].* to [your username]#localhost identified by '[your password]';
-- create sql dump on your computer:
mysqldump [your schema name] > [your schema name].sql
-- and import it to the new DB:
mysql [your schema name] < [your schema name].sql;
Since you are tagging this with "phpmyadmin", try with phpMyAdmin to create your database then import your .sql file.
//recupero il nome del file .sql
$file = "C:/wamp/www/my_folder/test_db.sql";
//definisco il nome del database
$db = 'name_db';
//effettuo la connessione al databse
$conn = mysqli_connect($mysql_host, $mysql_username, $mysql_password) or die('Errore di connessione al server MySQL: ' . mysqli_error());
//seleziono il database
mysqli_select_db($conn,$db);
//leggo il contenuto del file .sql
$qry = file_get_contents($file);
//divido tutte le query presenti nel file .sql
$query = explode(";",$qry);
//conto quante query esistono
$arrlenght = count($query);
//ciclo tutte le query del file
for($i=0;$i<$arrlenght;$i++){
mysqli_query($conn, $query[$i]);
}

Categories