0

new to php and mysql. i would like to build a system that will allow me to save to two databases at the same time. i know how to send data from my interface(i use html for the interface then use php to link with the mysql to design the database).i want to know if it is possible to have a real time back up for my database.

I basically have to make sure that my data is backed up some how so i thought i would use two databases that are fully copies of each other. if there is an easier way of doing this, please point me toward it.

2
Contributors
1
Reply
3
Views
5 Years
Discussion Span
Last Post by broj1
0

On Linux (maybe on Windows too) you can use mysqldump command to backup mySql database in a SQL script. Optionaly the script can be compressed (gzipped in this example).

// username, password, mysql host, database name
$dbuser = 'yourMysqlUsername';
$dbpw = 'yourMysqlPassword';
$dbhost = 'yourMysqlHost';
$dbname = 'yourMysqlDatabaseName';

// valid path to a directory where the database will be backed up
$backupPath = 'some valid path like /var/www/db_backup/';

// the name of the backup file
$backupFile = $backupPath . $dbname . '_' . date("Y-m-d-H-i-s");

// command for dumping database
$dbdumpcmd = "mysqldump --opt -v -h $dbhost -u $dbuser -p'$dbpw' $dbname";

// Option 1: a command to backup in a SQL file
$command = "$dbdumpcmd > $backupFile.sql";

// Option 2: a command to backup in a gzipped SQL file
$command = "$dbdumpcmd | gzip > $backupFile.gz";

// execute the command
system($command);
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.