backup from MySQL database

Reply

Join Date: May 2008
Posts: 28
Reputation: almualim is an unknown quantity at this point 
Solved Threads: 0
almualim almualim is offline Offline
Light Poster

backup from MySQL database

 
0
  #1
Nov 30th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 62
Reputation: PomonaGrange is an unknown quantity at this point 
Solved Threads: 3
PomonaGrange PomonaGrange is offline Offline
Junior Poster in Training

Re: backup from MySQL database

 
0
  #2
Nov 30th, 2008
Hello There, I had found this script of the web, though I don't remember where now, anyway here's what I use.

  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.
There are alot of people smarter than me, BUT at least I try to be of some help. Of coarse I'm not perfect, I need help too.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC