0

I want to create backup of my website database in which is in MySQL in another database on regular basis , is it possible to do it using php

Already tried exporting database using php but requirement is something else

  • 2
    Is it possible? Sure. Would you want to? Probably not. There are tools for this. Simplest being mysqldump cli tool which you could run on a cron schedule if you wished. – Jonnix May 30 '19 at 09:13
  • which another database are you planning to choose? It's possible but It will require plenty of work. – harunaga May 30 '19 at 09:16
  • I have a database with name A which contain all the data and a database B which is empty i want to copy all data from database A to database B using PHP specifically – Rajeev Khulbe May 30 '19 at 09:18
  • To add to Jonnix comment that this might be a bad idea: You would need to open two database connections, transferring all data, probably while one database is live, and the connections could go down. You would have to build in a lot of checks to make sure you actually get a good copy. Simply dumping the database, and importing it into the other database is simpler, and can be done with PHP. – KIKO Software May 30 '19 at 09:20
  • You could also consider db replication as a solution. – Jonnix May 30 '19 at 09:21
  • I think you can stop mysql services and copy data folder for backup – Nguyên Ngô Duy May 30 '19 at 09:34
  • That would depend on the engine being used I believe. – Jonnix May 30 '19 at 09:36
  • Possible duplicate of [Automated or regular backup of mysql data](https://stackoverflow.com/questions/38916163/automated-or-regular-backup-of-mysql-data) – Tatranskymedved May 30 '19 at 11:34

2 Answers2

0

I think mysqldump is what you're looking for.

Export database A to SQL file to import to database B:

mysqldump --host=localhost --user=dbauser --password=dbapassword dba_name > /path/to/store/dba.sql

Import database A dump into database B:

cp /path/to/store/dba.sql | mysql --host-localhost --user=dbbuser --password=dbbpassword dbb_name

You can wrap these commands in a call to system() in a PHP script.

alistaircol
  • 1,433
  • 12
  • 22
0
$host_name = "localhost";
$user_name= "root";
$password= "";
$database1 = "database_name";
$database2 = "second_database_name";
$con1 = mysqli_connect($host_name ,$user_name,$password,$database1);
$con2 = mysqli_connect($host_name ,$user_name,$password,$database2);

mysqli_select_db($con1,$database1)
mysqli_select_db($con2,$database2)

$sql = "SELECT id, firstname, lastname FROM users";
$result = mysqli_query($con1, $sql);
$result1 = mysqli_query($con2, $sql);