| | |
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 |
advanced alerts apache api archive array autosuggest beginner binary broken cakephp checkbox class clients cms code cron curl database date datepart display dynamic echo email emptydisplayvalue eregi error execute explodefunction file files folder form forms function functions google hack head href htaccess html if...loop image include insert ip javasciptvalidation javascript joomla keywords library limit link login mail matching menu mlm multiple mysql object oop password paypal pdf php phpincludeissue query radio random recursion recursive remote script search searchbox server sessions shot smarty source space speed sql syntax system table tutorial update upload url validator variable vbulletin video web website youtube





