her er hele det script jeg tidligere har lavet, og som jeg bruger til at lave en hurtig backup.
<?
/*******************************************************************
*
* DESCRIPTION: This file generates a dump of all the databases and tables on localhost
*
* The output is in two formats: Clear text (csv) and all the files in a zipped archive that
* can be opened in Windows using Winzip.
*
* NOTE: this script will only run on a Linux server or a windows server with "zip" installed as a prompt accessible
* utility.
*
* The motivation for this script came about when I had to send CVS files to the same person every so often - this was an annoying and manual task
* that I performed using phpmyadmin (
www.phpmyadmin.net)
*
* I have choosen the zip format as it works on both the Windows and Linux platform. Another reason for the Zip format is most of the end users
* are working form a Windows platform and when they recieve gz files an extra action has to be performed in order to open the file and found that
* this confused the end user more than I cared to deal with.
*
* This script also requires the mime_mail classe written by Sascha Schumann <sascha@schumann.cx> and modified by Modified by Tobias Ratschiller <tobias@dnet.it>
* in order to send the files to an e-mail acount
*
* AUTHOR: Peter Binderup (peter@binderup.dk)
*
* HISTORY:
*
* DATE:29-07-2002
*
*******************************************************************/
?>
<?
//CONFIGURATION PART BEGIN
//the database connection
$link = @mysql_pconnect("localhost", "USERNAME", "PASSWORD") or exit();
//this is the path to where the script dumps the files - the directory "dump_files" has to reside in the same folder as the script
$path=dirname($_SERVER["SCRIPT_FILENAME"])."/dump_files/";
//this is the web path (from the web root)
$www_path=dirname($_SERVER["PHP_SELF"]) . "/dump_files/";
//lets define the path to the folders where we are going to fump the files
$current_hour=date("H");
//this is subfolder representing the current hour
$dump_path_time=$path.$current_hour."/";
//CONFIGURATION PART END
/*
*Function Name:generate_database_table_view
*
*Parameters: none
*
*Description: this function creates two global arrays $databases and $database_tables
* - the first contains a list of all the databasen on the defined mysql connection
* - the latter contains a multidimensional array containing the databases and the tables below each of these.
*
*Returns: input to the two global arrays $databases and $database_tables
*
*/
function generate_database_table_view(){
global $link;
global $databases;
global $database_tables;
$result = mysql_list_dbs($link);
while ($row = mysql_fetch_row($result)) {
$databases[]=$row[0];
}
for ($i=0; $i<sizeof($databases); $i++) {
$result2=mysql_list_tables($databases[$i]);
while ($row=mysql_fetch_row($result2)) {
$database_tables[$i][]=$row[0];
}
}
}
/*
*Function Name: generate_csv
*
*Parameters: $database_array_no (ie. a position in the $databases array)
*
*Description: this function does all the "dirty" work of creating the csv files for a given database.
*
* The way it works is by creating a folder named by the current hour (ie. script runs 15:31 a folder named 15 will be created)
* If a folder/file already exists it will be deleted/overwritten.
*
*Returns: nothing
*
*/
function generate_csv($database_array_no){
global $link;
global $databases;
global $database_tables;
global $dump_path_time;
$database=$databases[$database_array_no];
$dump_path = $dump_path_time .$database ."/";
$exists1=file_exists($dump_path);
if (!$exists1) {
$oldumask=umask(0);
@mkdir($dump_path_time, 0777);
umask($oldumask);
}
$exists2=file_exists($dump_path);
if (!$exists2) {
$oldumask=umask(0);
@mkdir($dump_path, 0777);
umask($oldumask);
}
for ($i=0; $i<sizeof($database_tables[$database_array_no]); $i++) {
$table_name=$database_tables[$database_array_no][$i];
$filename=$dump_path . $table_name . ".csv";
@unlink($filename); //if a file by the same name already exists in the folder it will be deleted in order to make room for the new...
$sql_statement="SELECT * INTO OUTFILE '$filename' FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\n' FROM $table_name";
mysql_select_db("$database", $link) or exit();
$result=mysql_query($sql_statement, $link);
//lets add column description to the csv file
csv_add_fieldnames($database, $table_name, $filename);
}
}
/*
*Function Name:csv_add_fieldnames
*
*Parameters: $database, $table, $filename
*
*Description: this functions adds the columnname to the top of each csv file
*
*Returns: nothing
*
*/
function csv_add_fieldnames($database, $table, $filename){
global $link;
$fields = mysql_list_fields("$database", "$table", $link);
$columns = mysql_num_fields($fields);
for ($i = 0; $i < $columns; $i++) {
$new_file_content.=("\"" . mysql_field_name($fields, $i));
if ($i==($columns-1)) {
$new_file_content.="\"\n";
} else {
$new_file_content.="\",";
}
}
//lets open the original file and read its content into $new_file_content
$fp_org=fopen($filename, "r");
$new_file_content .= fread($fp_org, filesize($filename));
fclose($fp_org);
//lets open the original file and overwrite it with the new content
$fp_new=fopen($filename, "w");
fwrite($fp_new, $new_file_content);
fclose($fp_new);
}
/*
*Function Name:generate_mysqldump
*
*Parameters: none
*
*Description:
*
*Returns:
*
*/
function generate_mysqldump(){
global $link;
global $databases;
global $dump_path_time;
for ($i=0; $i<sizeof($databases); $i++) {
$dump_path = $dump_path_time . $databases[$i] ."/";
$file_path=$dump_path . $databases[$i] . ".sql";
exec("mysqldump --opt --user USERNAME -pPASSWORD $databases[$i] > $file_path");
}
}
/*
*Function Name:generate_zip
*
*Parameters: none
*
*Description: this function generates a zip file in the $hour folder
*
*Returns: returns a zipfile to the destinatio path below
*
*/
function generate_zip(){
global $current_hour;
global $path;
$zip_path=$path."zip_folders";
if (!file_exists($zip_path)) {
$oldumask=umask(0);
@mkdir($zip_path, 0777);
umask($oldumask);
}
$source_path=$path . $current_hour;
$destination_file_csv=$path . "zip_folders/csv_"."$current_hour.zip";
@unlink($destination_file_csv); //old file is deleted if it already exists
$destination_file_mysqldump=$path . "zip_folders/mysqldump_"."$current_hour.zip";
@unlink($destination_file_mysqldump); //old file is deleted if it already exists
if (stristr($_SERVER['SERVER_SOFTWARE'], "Win32")){
//WINDOWS with WinZip (commandline) installed and added to path - a reboot of the host machine might be necessary
exec("wzzip.exe -rp $destination_file_csv $source_path/*.csv");
exec("wzzip.exe -rp $destination_file_mysqldump $source_path/*.sql");
} else {
//LINUX with Zip installed
passthru("zip -rq $destination_file_csv $source_path");
passthru("zip -dq $destination_file_csv *.sql");
passthru("zip -rq $destination_file_mysqldump $source_path");
passthru("zip -dq $destination_file_mysqldump *.csv");
}
}
//FUNCTIONS END
//ok lets begin creating the output
generate_database_table_view();
for ($i=0; $i<sizeof($databases); $i++) {
generate_csv($i);
}
generate_mysqldump();
generate_zip();
?>
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_openBrWindow(theURL,winName,features) { //v2.0
window.open(theURL,winName,features);
}
//-->
</script>
<table width="613" border="1" cellpadding="0" cellspacing="0">
<tr>
<td colspan="2">Access the individual CVS files here for this current dump</td>
<td width="74"><a href="<? echo $www_path.$current_hour; ?>">[browse]</a></td>
<td width="112"> </td>
</tr>
<tr>
<td colspan="4"> </td>
</tr>
<tr>
<td width="17"> </td>
<td width="382">Download all the <strong>CSV</strong> (Excel) files as one big ZIP
file</td>
<td><a href="<? echo $www_path."zip_folders/csv_".$current_hour.".zip"; ?>">[download]</a></td>
<td><a href="#" onClick="MM_openBrWindow('<? echo "/mysql_csv_dump/send_files.php?file_path=$DOCUMENT_ROOT/mysql_csv_dump/dump_files/zip_folders/&filename=csv_$current_hour.zip"; ?>','Mail','width=450,height=400')">[send
as email]</a></td>
</tr>
<tr>
<td> </td>
<td>Download all the <strong>MySQL dump</strong> files as one big ZIP file</td>
<td><a href="<? echo $www_path."zip_folders/mysqldump_".$current_hour.".zip"; ?>">[download]</a>
</td>
<td><a href="#" onClick="MM_openBrWindow('<? echo "/mysql_csv_dump/send_files.php?file_path=$DOCUMENT_ROOT/mysql_csv_dump/dump_files/zip_folders/&filename=mysqldump_$current_hour.zip"; ?>','Mail','width=450,height=400')">[send
as email]</a></td>
</tr>
<tr>
<td colspan="4"> </td>
</tr>
<tr>
<td colspan="4"><span style="font-size: smaller; font-weight: bolder; color: #A9A9A9; font-style: italic;">Script
executed at <? echo date("Y-m-d H:i:s");?></span>
</td>
</tr>
</table>
<P> </P>