Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> 電腦軟體教學 >> 伺服器技術 >> 關於伺服器

Windows Server 2003 + MySQL 每日自動備份、壓縮、清理 5 天前舊檔完整腳本

編輯:關於伺服器

在 Windows Server 2003 + Apache + PHP + MySQL 環境下,利用 PHP 腳本呼叫 mysqldump 與 WinRAR 實現:每日全庫備份 → 壓縮加密 → 自動刪除 5 天前舊檔。零成本、無第三方排程軟體,純原生任務排程器即可跑滿。

環境假設

  • PHP 安裝目錄:D:\php
  • MySQL 安裝目錄:D:\mysql
  • 備份儲存目錄:D:\database_backup (需手動建立)
  • MySQL 帳號:root (需具備所有資料庫 SELECT、LOCK TABLES、FILE 權限)

準備 WinRAR 免安裝版

  1. 在 D:\php 下建立 WinRAR 資料夾
  2. 從 WinRAR 安裝目錄複製 WinRAR.exe 與 RARReg.KEY (註冊金鑰) 到此資料夾

建立 PHP 備份腳本

檔案:D:\php\mysql_backup.php

<?php
/**
 * MySQL 每日備份腳本
 * 需 CLI 模式執行:php.exe mysql_backup.php
 */
$store_folder = 'D:\\database_backup';   // 雙反斜線或單正斜線皆可
$db_user      = 'root';
$db_pass      = '';                       // 有密碼請填入

$time   = time();
$nowdir = $store_folder . '\\' . date('Ymd', $time);
$rarFile = $nowdir . '.rar';

// 避免重複執行
if (file_exists($rarFile)) {
    die("Backup file {$rarFile} already exists.\n");
}

// 建立當日資料夾
@mkdir($nowdir, 0755, true);

// 連線 MySQL 列舉所有資料庫
$link = mysql_connect('localhost', $db_user, $db_pass);
if (!$link) die('MySQL connect failed: ' . mysql_error());

$dbs = mysql_list_dbs($link);
while ($row = mysql_fetch_array($dbs)) {
    $dbName = $row['Database'];
    $dumpFile = "{$nowdir}\\{$dbName}.sql";

    // 組裝 mysqldump 指令
    $cmd = sprintf(
        '"%s\..\mysql\bin\mysqldump.exe" --opt %s -u%s %s > "%s"',
        __FILE__,
        $dbName,
        $db_user,
        $db_pass ? "-p{$db_pass}" : '',
        $dumpFile
    );

    echo "Dumping database `{$dbName}`...\n";
    system($cmd, $ret);
    if ($ret !== 0) {
        echo "Warning: mysqldump returned code {$ret} for {$dbName}\n";
    }
}
mysql_close($link);

// 壓縮加密 (AES-256,需 WinRAR 4.0+)
echo "\nCompressing with WinRAR...\n";
$rarCmd = sprintf(
    '"%s\WinRAR\WinRAR.exe" a -ep1 -r -o+ -m5 -hpYourStrongPassword "%s" "%s"',
    __FILE__,
    $rarFile,
    $nowdir
);
system($rarCmd, $ret);
if ($ret !== 0) die("WinRAR compression failed with code {$ret}\n");

// 刪除 5 天前舊備份
$oldFile = $store_folder . '\\' . date('Ymd', $time - 5 * 86400) . '.rar';
if (file_exists($oldFile)) {
    @unlink($oldFile);
    echo "Deleted old backup: {$oldFile}\n";
}

echo "\nBackup completed successfully!\n";
?>

建立批次啟動檔

檔案:D:\php\mysql_backup.bat

@echo off
D:
cd \php
php.exe mysql_backup.php

註冊 Windows 任務排程器

  1. Win+R → control schedtasks →「新增排程工作」
  2. 瀏覽選擇 D:\php\mysql_backup.bat
  3. 執行頻率:每天 → 時間:08:00 (流量低峰,避開 05:00-07:00 可能的資料庫維護窗口)
  4. 輸入執行身分:具備 MySQL 權限的帳號 (如 Administrator 或專用服務帳號)
  5. 完成

驗證與維護

  • 手動雙擊 mysql_backup.bat 觀察 Console 輸出,確認無錯誤
  • 備份完成後,至 D:\database_backup 檢查 YYYYMMDD.rar 存在且可解壓
  • 定期測試還原:mysql -u root -p dbname < dbname.sql
  • 若資料庫增長超過單檔 2 GB,建議改用 --single-transaction --quick 參數並分庫壓縮

進階強化 (可選)

  • 異地備份:任務結束後呼叫 robocopy 或 rclone 推送至 NAS/FTP/S3
  • 加密金鑰管理:將 -hpYourStrongPassword 移至環境變數或保管庫,避免明文寫在腳本
  • 監控告警:腳本回傳非 0 時,透過 blat.exe 或 PowerShell Send-MailMessage 發送失敗通知
  • 升級 PHP:PHP 5.x 已停更,建議遷移至 PHP 8.x + mysqli/PDO,mysql_* 函式庫已移除

⚠️ 本腳本使用已棄用的 mysql_* 擴展,僅適用於 PHP 5.x 存量環境。新專案請改用 mysqli 或 PDO_MySQL 並啟用預處理語句。

Copyright © Windows教程網 All Rights Reserved