| | |
backup from MySQL database
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jun 2008
Posts: 62
Reputation:
Solved Threads: 3
Hello There, I had found this script of the web, though I don't remember where now, anyway here's what I use.
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.
PHP Syntax (Toggle Plain Text)
<? define('SERVER', 'localhost'); //Your server host define('USRNAME', 'root'); //Your DB user name define('PASS', 'Your Pass'); //Your db Password define('DBNAME', 'db_name'); //You db name $ccyymmdd = date("Ymd"); $file = fopen("backups/backup".$ccyymmdd.".sql","w"); $line_count = create_backup_sql($file); fclose($file); $message = "Backup Complete<br />lines written: ".$line_count; function create_backup_sql($file) { $line_count = 0; $db_connection = db_connect(); mysql_select_db (db_name()) or exit(); $tables = mysql_list_tables(db_name()); $sql_string = NULL; while ($table = mysql_fetch_array($tables)) { $table_name = $table[0]; $sql_string = "TRUNCATE TABLE $table_name"; $table_query = mysql_query("SELECT * FROM `$table_name`"); $num_fields = mysql_num_fields($table_query); while ($fetch_row = mysql_fetch_array($table_query)) { $sql_string .= "INSERT INTO $table_name VALUES("; $first = TRUE; for ($field_count=1;$field_count<=$num_fields;$field_count++){ if (TRUE == $first) { $sql_string .= "'".mysql_real_escape_string($fetch_row[($field_count - 1)])."'"; $first = FALSE; } else { $sql_string .= ", '".mysql_real_escape_string($fetch_row[($field_count - 1)])."'"; } } $sql_string .= ");"; if ($sql_string != ""){ $line_count = write_backup_sql($file,$sql_string,$line_count); } $sql_string = NULL; } } return $line_count; } function write_backup_sql($file, $string_in, $line_count) { fwrite($file, $string_in); return ++$line_count; } function db_name() { return (DBNAME); } function db_connect() { $db_connection = mysql_connect(SERVER, USRNAME, PASS); return $db_connection; } ?>
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.
![]() |
Similar Threads
- MySQL Database Backups (MySQL)
- URGENT: UBBThreads MySQL database merge (MySQL)
- Backing up a MySQL database (MySQL)
Other Threads in the PHP Forum
- Previous Thread: php: abstract classes (whats the point behind them)
- Next Thread: php e-mail problem
| Thread Tools | Search this Thread |
apache api array auto beginner binary broken cache cakephp checkbox class cms code codingproblem cron curl customizableitems database date display dynamic echo email error errorlog file files filter folder form format forms forum function functions gc_maxlifetime global google headmethod href htaccess html image include insert ip javascript joomla limit link login mail malfunctioning memmory memory menu method mlm multiple mysql nodes oop parameter parsing paypal pdf php phpmysql popup query radio random recursion recursiveloop remote script search select server sessions snippet source space sql static survey syntax system table trouble tutorial up-to-date update upload url validator variable video web youtube





