943,923 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 548
  • PHP RSS
Nov 30th, 2008
0

backup from MySQL database

Expand Post »
hi friends, i want to get backup from MySQL DB by use php code.
Last edited by almualim; Nov 30th, 2008 at 3:05 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
almualim is offline Offline
28 posts
since May 2008
Nov 30th, 2008
0

Re: backup from MySQL database

Hello There, I had found this script of the web, though I don't remember where now, anyway here's what I use.

PHP Syntax (Toggle Plain Text)
  1. <?
  2. define('SERVER', 'localhost'); //Your server host
  3. define('USRNAME', 'root'); //Your DB user name
  4. define('PASS', 'Your Pass'); //Your db Password
  5. define('DBNAME', 'db_name'); //You db name
  6.  
  7.  
  8. $ccyymmdd = date("Ymd");
  9. $file = fopen("backups/backup".$ccyymmdd.".sql","w");
  10. $line_count = create_backup_sql($file);
  11. fclose($file);
  12. $message = "Backup Complete<br />lines written: ".$line_count;
  13.  
  14. function create_backup_sql($file) {
  15. $line_count = 0;
  16. $db_connection = db_connect();
  17. mysql_select_db (db_name()) or exit();
  18. $tables = mysql_list_tables(db_name());
  19. $sql_string = NULL;
  20. while ($table = mysql_fetch_array($tables)) {
  21. $table_name = $table[0];
  22. $sql_string = "TRUNCATE TABLE $table_name";
  23. $table_query = mysql_query("SELECT * FROM `$table_name`");
  24. $num_fields = mysql_num_fields($table_query);
  25. while ($fetch_row = mysql_fetch_array($table_query)) {
  26. $sql_string .= "INSERT INTO $table_name VALUES(";
  27. $first = TRUE;
  28. for ($field_count=1;$field_count<=$num_fields;$field_count++){
  29. if (TRUE == $first) {
  30. $sql_string .= "'".mysql_real_escape_string($fetch_row[($field_count - 1)])."'";
  31. $first = FALSE;
  32. } else {
  33. $sql_string .= ", '".mysql_real_escape_string($fetch_row[($field_count - 1)])."'";
  34. }
  35. }
  36. $sql_string .= ");";
  37. if ($sql_string != ""){
  38. $line_count = write_backup_sql($file,$sql_string,$line_count);
  39. }
  40. $sql_string = NULL;
  41. }
  42. }
  43. return $line_count;
  44. }
  45.  
  46. function write_backup_sql($file, $string_in, $line_count) {
  47. fwrite($file, $string_in);
  48. return ++$line_count;
  49. }
  50.  
  51. function db_name() {
  52. return (DBNAME);
  53. }
  54.  
  55. function db_connect() {
  56. $db_connection = mysql_connect(SERVER, USRNAME, PASS);
  57. return $db_connection;
  58. }
  59. ?>

This script will write a backup file named with the date and a prefix of backup in your backups folder. I hope this helps. If needed I also have a restore from backup script.
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
PomonaGrange is offline Offline
67 posts
since Jun 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: php: abstract classes (whats the point behind them)
Next Thread in PHP Forum Timeline: php e-mail problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC