Windows2003 下 MySQL 資料庫每天自動備份_php技巧

來源:互聯網
上載者:User
終於讓伺服器每天早上備份一次 MySQL 資料庫並自動打包,同時刪除 5 天前的備份檔案。分享如下:

    1. 環境: windows server 2003 + Apache 2.0 + PHP5 + MySQL 4.0.26 .

    2. 假設 PHP 安裝目錄為 D:/php ,MySQL 安裝目錄為 D:/mysql.

    3. 在 D:/php 下建立目錄 WinRAR, 把你 winrar 安裝目錄下的 WinRAR.exe 和 RARReg.KEY 複製到 D:/php/WinRAR .

    4. D:/php 下建立檔案 mysql_backup.php:

    /*/////////////////////////
    #FileName: mysql_backup.php #Author: faisun #Website: http://www.softpure.com
    ////////////////////////*/
    //儲存目錄,路徑要用反斜線。您需要手動建立它。
     $store_folder = 'D:\databse_backup';
    //使用者名稱和密碼
    //該帳號須有操作[所有]的資料庫及FILE的許可權
    //否則有些資料庫不能備份
    $db_username = “root“;
    $db_password = ““;
    $time=time();
    $nowdir = “$store_folder\\“.date(“Ymd“,$time).““;
    if(file_exists(“$nowdir.rar“))
      die(“File exists.\n“);
    @mkdir($nowdir);
    mysql_connect(“localhost“,“$db_username“,“$db_password“);
    $query=mysql_list_dbs();
    while($result=mysql_fetch_array($query))
    { system (dirname(__FILE__).'\..\mysql\bin\mysqldump --opt '.“$result[Database]-u{$db_username} “.($db_password?“-p{$db_password}“:““).“ 〉$nowdir\\$result[Database].sql“);
    echo “dumping database `$result[Database]`...\n“;
    }
    echo “\nWinrar loading...\n“;
    system( dirname(__FILE__).“\\WinRAR\\WinRAR.exe a -ep1 -r -o+ -m5 -df \“$nowdir.rar\“ “$nowdir\“ “ );
    //刪除 5 天前的檔案
    @unlink(“$store_folder\\“.date(“Ymd“,$time-86400*5).“.rar“);
    echo “\nOK!\n“; 〉

    5. D:/php 下建立檔案 mysql_backup.bat,內容只有一句:

    php.exe mysql_backup.php

    6. 雙擊該 bat 檔案運行,如果能備份了,OK,下一步新增工作計劃.

    7. 把 D:/php/mysql_backup 添加到任務計劃,時間選每天. 根據伺服器的監測結果,每天早上 5-8 時為流量低峰期. 由於 5-7 時有些資料庫的清理工作,可以把時間定在了早上 8 點整。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.