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

VPS 定時自動備份終極指南:rsync + 腳本實現異地增量備份與 MySQL 熱備

編輯:關於伺服器

半年丟失 5 次站點數據 (硬盤故障、供應商跑路),RAID 10 並非銀彈。**異地增量備份是唯一底線**。本文提供生產環境驗證的「拉式備份」架構:備份端 B 定時拉取業務端 A 數據,含文件 rsync、MySQL 熱備、排除緩存、自動清理舊備份、雙重備份策略。

架構拓撲

  • VPS A (業務端):運行 rsync daemon (端口 873),唯讀暴露 /var/www 與 MySQL 備份目錄。
  • VPS B (備份端):crontab 定時觸發 rsync 客戶端拉取,存儲至本地 /var/rsync/。
  • 可選 VPS C:再拉一份 B 的數據,實現三地三中心。

VPS A 部署 (rsync Server 端)

1. 安裝與啟動

yum -y install rsync
echo 'rsync --daemon' >> /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local

2. 密碼文件 (僅 Server 端需)

echo 'backup_user:StrongPassw0rd!' > /etc/rsyncd.scrt
chmod 600 /etc/rsyncd.scrt

3. 配置文件 /etc/rsyncd.conf

uid = root
gid = root
use chroot = no
read only = yes
max connections = 10
port = 873
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log format = %t %a %m %f %b
timeout = 300

[www]
path = /var/www/
comment = Web Root
ignore errors
read only = yes
list = no
auth users = backup_user
secrets file = /etc/rsyncd.scrt
exclude from = /etc/rsync_exclude.txt
hosts allow = <備份端B的IP>
hosts deny = *

4. 排除文件 /etc/rsync_exclude.txt (相對路徑)

site1/runtime/cache/
site1/runtime/logs/
site2/storage/framework/cache/
*.tmp
*.log

5. 重啟腳本 /root/rsyncd_restart.sh

#!/bin/bash
kill -9 $(cat /var/run/rsyncd.pid 2>/dev/null)
rm -f /var/run/rsyncd.pid /var/run/rsync.lock
rsync --daemon

6. MySQL 熱備腳本 /root/mysql_backup.sh (每日 00:00)

#!/bin/bash
# === 配置區 ===
mysql_user="backup_user"
mysql_password="DbPassw0rd!"
mysql_host="localhost"
mysql_port="3306"
mysql_charset="utf8mb4"
backup_db_arr=("db_site1" "db_site2")  # 多庫用空格分隔
backup_location="/var/www/mysql_backup"
expire_backup_delete="ON"
expire_days=3
# === 邏輯區 ===
backup_time=$(date +%Y%m%d%H%M)
backup_Ymd=$(date +%Y-%m-%d)
backup_dir="${backup_location}/${backup_Ymd}"
mkdir -p "${backup_dir}"

for dbname in "${backup_db_arr[@]}"; do
    mysqldump -h"${mysql_host}" -P"${mysql_port}" -u"${mysql_user}" -p"${mysql_password}" \
        --default-character-set="${mysql_charset}" --single-transaction --routines --triggers \
        "${dbname}" | gzip > "${backup_dir}/${dbname}-${backup_time}.sql.gz"
    # 可在此處加入上傳至對象存儲 (OSS/S3) 邏輯
done

# 清理舊備份
if [[ "${expire_backup_delete}" == "ON" ]]; then
    find "${backup_location}" -type f -mtime +${expire_days} -name "*.gz" -delete
    find "${backup_location}" -type d -empty -delete
fi
chmod +x /root/mysql_backup.sh
echo '0 0 * * * /root/mysql_backup.sh' >> /var/spool/cron/root

VPS B 部署 (rsync Client 端)

1. 安裝 rsync (無需 daemon)

yum -y install rsync

2. 密碼文件 (客戶端專用,權限 400)

echo 'StrongPassw0rd!' > /etc/rsync.pass
chmod 400 /etc/rsync.pass

3. 測試同步

mkdir -p /var/rsync/
rsync -avzP --delete --password-file=/etc/rsync.pass \
    backup_user@::www /var/rsync/www/
  • -a 歸檔模式 (保留權限/時間/軟鏈接)
  • -z 壓縮傳輸
  • -P 顯示進度/斷點續傳
  • --delete 刪除目標端源端已無的文件 (保持鏡像一致)
  • ::www 對應 Server 端 `[www]` 模塊

4. 定時任務 (每日 00:30,錯峰避開 MySQL 備份高峰)

echo '30 0 * * * rsync -avzP --delete --password-file=/etc/rsync.pass backup_user@::www /var/rsync/www/ >/dev/null 2>&1' >> /var/spool/cron/root

驗證與監控

  • 備份次日檢查 /var/rsync/www/ 目錄結構、文件大小、MySQL .gz 文件可解壓。
  • 設置備份失敗郵件/Telegram 告警 (在腳本中判斷返回碼非 0 發送通知)。
  • 每季度演練 **全量恢復演練** (在測試機恢復文件+導入 SQL),驗證備份可用性。
核心原則:**備份未測試 = 沒備份**。異地、增量、加密、自動清理、雙重備份、定期演練 —— 六字真言落地才是真安全。
Copyright © Windows教程網 All Rights Reserved