自动备份的php脚本Auto backup, Ver 1.3
- <?php
- /*
- * Backup script on server.
- *
- * Runs on the server, called by Cron. Connects to the mySQL
- * database and creates a backup file of the whole database.
- * Saves to file in current directory.
- *
- * @author Cow <cow@invisionize.com>
- * @version 0.2
- * @date 18/08/2004
- * @package Backup Server
- */
-
- set_time_limit(0);
-
- $dbserver = "localhost";
- $dbuser = "dbusername";
- $dbpass = "dbpassword";
- $dbname = "dbname";
- $file = "backup.sql.gz";
- $gzip = TRUE;
- $silent = TRUE;
-
- function write($contents) {
- if ($GLOBALS['gzip']) {
- gzwrite($GLOBALS['fp'], $contents);
- } else {
- fwrite($GLOBALS['fp'], $contents);
- }
- }
-
- mysql_connect ($dbserver, $dbuser, $dbpass);
- mysql_select_db($dbname);
-
- if ($gzip) {
- $fp = gzopen($file, "w");
- } else {
- $fp = fopen($file, "w");
- }
-
- $tables = mysql_query ("SHOW TABLES");
- while ($i = mysql_fetch_array($tables)) {
- $i = $i['Tables_in_'.$dbname];
-
- if (!$silent) {
- echo "Backing up table ".$i."/n";
- }
-
- // Create DB code
- $create = mysql_fetch_array(mysql_query ("SHOW CREATE TABLE ".$i));
-
- write($create['Create Table'].";/n/n");
-
- // DB Table content itself
- $sql = mysql_query ("SELECT * FROM ".$i);
- if (mysql_num_rows($sql)) {
- while ($row = mysql_fetch_row($sql)) {
- foreach ($row as $j => $k) {
- $row[$j] = "'".mysql_escape_string($k)."'";
- }
-
- write("INSERT INTO $i VALUES(".implode(",", $row).");/n");
- }
- }
- }
-
- $gzip ? gzclose($fp) : fclose ($fp);
-
- // Fini
- ?>
<script type="text/javascript">document.write('