Files
bird_config/update.sh
2025-11-01 21:58:50 +08:00

72 lines
2.0 KiB
Bash
Raw 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.
#!/bin/bash
# 配置参数请替换为实际URL
URL="https://git.nia.ink/brnet/bird_config/raw/branch/master/bird/function/unet.conf" # 替换为你的$url
TARGET_FILE="/etc/bird/function/unet.conf"
BACKUP_FILE="${TARGET_FILE}.bak.$(date +%Y%m%d%H%M%S)" # 带时间戳的备份文件
LOG_FILE="/var/log/update_unet_conf.log"
# 日志函数
log() {
echo "[$(date +%Y%m%d%H%M%S)] $1" >> "$LOG_FILE"
}
# 检查root权限
if [ "$(id -u)" -ne 0 ]; then
log "错误必须使用root权限运行脚本请用sudo"
echo "错误必须使用root权限运行脚本请用sudo"
exit 1
fi
# 检查wget是否安装
if ! command -v wget &> /dev/null; then
log "错误未安装wget请先执行 'sudo apt install wget' 或 'sudo yum install wget' 安装"
echo "错误未安装wget请先安装"
exit 1
fi
# 检查目标文件目录是否存在
if [ ! -d "$(dirname "$TARGET_FILE")" ]; then
log "错误:目标目录 $(dirname "$TARGET_FILE") 不存在"
echo "错误:目标目录不存在"
exit 1
fi
# 备份原文件
log "开始备份原文件到 $BACKUP_FILE"
if cp "$TARGET_FILE" "$BACKUP_FILE"; then
log "备份成功"
else
log "错误:备份失败,终止操作"
echo "错误:备份失败"
exit 1
fi
# 下载并覆盖文件
log "开始从 $URL 下载文件"
if wget -q -O "$TARGET_FILE" "$URL"; then # -q 静默模式,-O 指定输出文件
log "下载成功,已覆盖 $TARGET_FILE"
else
log "错误:下载失败,恢复原文件"
echo "错误:下载失败,正在恢复原文件..."
mv "$BACKUP_FILE" "$TARGET_FILE" # 恢复备份
exit 1
fi
# 验证文件内容(检查是否为空或乱码)
if [ ! -s "$TARGET_FILE" ]; then # -s 检查文件非空
log "错误:下载的文件为空,恢复原文件"
echo "错误:文件为空,正在恢复原文件..."
mv "$BACKUP_FILE" "$TARGET_FILE"
exit 1
fi
# 可选重启bird服务并检查状态
log "重载bird服务"
birdc c
log "脚本执行完毕"
cat $TARGET_FILE
cd
rm update.sh