Files
server-deploy/vps-xray/backup.sh

63 lines
1.8 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
set -euo pipefail
# ============================================
# Xray 备份脚本
# 备份内容Xray 配置 + .env 凭据 + 网络调优
# 定时执行: crontab -e → 0 3 * * 0 /opt/vps-xray/backup.sh
# ============================================
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
# 加载配置
if [ -f .env ]; then
sed -i 's/\r$//' .env
set -a; source .env; set +a
fi
BACKUP_DIR="${BACKUP_DIR:-/var/backups/xray}"
DATE=$(date +%Y%m%d_%H%M%S)
KEEP_DAYS=30
log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*"; }
mkdir -p "$BACKUP_DIR"
# 1. 备份 Xray 配置
log "正在备份 Xray 配置..."
if [ -f /usr/local/etc/xray/config.json ]; then
tar czf "${BACKUP_DIR}/xray_config_${DATE}.tar.gz" \
-C /usr/local/etc xray/
log "Xray 配置备份完成: xray_config_${DATE}.tar.gz"
else
log "警告: Xray 配置文件不存在,跳过"
fi
# 2. 备份部署配置(.env + 脚本)
log "正在备份部署配置..."
tar czf "${BACKUP_DIR}/deploy_config_${DATE}.tar.gz" \
-C "$SCRIPT_DIR" \
$(ls .env deploy.sh backup.sh uninstall.sh 2>/dev/null)
log "部署配置备份完成: deploy_config_${DATE}.tar.gz"
# 3. 备份网络调优配置
if [ -f /etc/sysctl.d/99-xray-turbo.conf ]; then
log "正在备份网络调优配置..."
cp /etc/sysctl.d/99-xray-turbo.conf "${BACKUP_DIR}/sysctl_${DATE}.conf"
log "网络调优备份完成: sysctl_${DATE}.conf"
fi
# 4. 清理过期备份
log "清理 ${KEEP_DAYS} 天前的备份..."
deleted=$(find "$BACKUP_DIR" -type f -mtime +${KEEP_DAYS} -print -delete | wc -l)
log "已清理 ${deleted} 个过期文件"
# 5. 输出备份摘要
echo ""
log "===== 备份完成 ====="
log "备份目录: ${BACKUP_DIR}/"
ls -lh "${BACKUP_DIR}/"*"${DATE}"* 2>/dev/null || true
echo ""
log "总备份空间占用: $(du -sh "${BACKUP_DIR}" | cut -f1)"