keenetic-configs/scripts/update-files.sh

103 lines
4.1 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/sh
# ====== КОНФИГУРАЦИЯ ======
# Формат: "URL|ЛОКАЛЬНЫЙ_ПУТЬ"
# Добавляйте сколько угодно пар
FILES="
https://gitea.vchikalkin.dev/admin/keenetic-configs/raw/branch/master/opt/etc/nfqws/user.list|/opt/etc/nfqws/user.list
https://gitea.vchikalkin.dev/admin/keenetic-configs/raw/branch/master/opt/etc/nfqws/ipset.list|/opt/etc/nfqws/ipset.list
https://gitea.vchikalkin.dev/admin/keenetic-configs/raw/branch/master/opt/etc/nfqws/nfqws.conf|/opt/etc/nfqws/nfqws.conf
https://gitea.vchikalkin.dev/admin/keenetic-configs/raw/branch/master/opt/etc/xkeen/ip_exclude.lst|/opt/etc/xkeen/ip_exclude.lst
https://gitea.vchikalkin.dev/admin/keenetic-configs/raw/branch/master/opt/etc/xkeen/port_exclude.lst|/opt/etc/xkeen/port_exclude.lst
https://gitea.vchikalkin.dev/admin/keenetic-configs/raw/branch/master/opt/etc/xkeen/port_proxying.lst|/opt/etc/xkeen/port_proxying.lst
https://gitea.vchikalkin.dev/admin/keenetic-configs/raw/branch/master/opt/etc/xray/configs/01_log.json|/opt/etc/xray/configs/01_log.json
https://gitea.vchikalkin.dev/admin/keenetic-configs/raw/branch/master/opt/etc/xray/configs/02_dns.json|/opt/etc/xray/configs/02_dns.json
https://gitea.vchikalkin.dev/admin/keenetic-configs/raw/branch/master/opt/etc/xray/configs/03_inbounds.json|/opt/etc/xray/configs/03_inbounds.json
https://gitea.vchikalkin.dev/admin/keenetic-configs/raw/branch/master/opt/etc/xray/configs/04_outbounds.json|/opt/etc/xray/configs/04_outbounds.json
https://gitea.vchikalkin.dev/admin/keenetic-configs/raw/branch/master/opt/etc/xray/configs/05_routing.json|/opt/etc/xray/configs/05_routing.json
https://gitea.vchikalkin.dev/admin/keenetic-configs/raw/branch/master/opt/etc/xray/configs/06_policy.json|/opt/etc/xray/configs/06_policy.json
https://gitea.vchikalkin.dev/admin/keenetic-configs/raw/branch/master/opt/etc/xray/configs/07_observatory.json|/opt/etc/xray/configs/07_observatory.json
"
RESTART_CMD="/opt/etc/init.d/rc.unslung restart"
LOG="/opt/var/log/update_files.log"
TMP_DIR="/opt/tmp"
# ====== ЛОГИКА ======
mkdir -p "$TMP_DIR"
CHANGED=0
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')
for entry in $FILES; do
# Пропускаем пустые строки
[ -z "$entry" ] && continue
URL=$(echo "$entry" | cut -d'|' -f1)
LOCAL=$(echo "$entry" | cut -d'|' -f2)
[ -z "$URL" ] || [ -z "$LOCAL" ] && continue
TMP_FILE="$TMP_DIR/update_$(echo "$LOCAL" | md5sum | cut -d' ' -f1).tmp"
# Скачиваем
wget -q -O "$TMP_FILE" "$URL" 2>/dev/null
if [ $? -ne 0 ]; then
echo "$TIMESTAMP [ERROR] Не удалось скачать: $URL" >> "$LOG"
rm -f "$TMP_FILE"
continue
fi
# Проверяем что файл не пустой
if [ ! -s "$TMP_FILE" ]; then
echo "$TIMESTAMP [WARN] Пустой файл: $URL" >> "$LOG"
rm -f "$TMP_FILE"
continue
fi
# Сравниваем с текущим
NEED_UPDATE=0
if [ ! -f "$LOCAL" ]; then
# Файла ещё нет
NEED_UPDATE=1
else
# Сравниваем контрольные суммы
OLD_SUM=$(md5sum "$LOCAL" 2>/dev/null | cut -d' ' -f1)
NEW_SUM=$(md5sum "$TMP_FILE" | cut -d' ' -f1)
if [ "$OLD_SUM" != "$NEW_SUM" ]; then
NEED_UPDATE=1
fi
fi
if [ "$NEED_UPDATE" -eq 1 ]; then
# Создаём директорию если нет
mkdir -p "$(dirname "$LOCAL")"
cp "$TMP_FILE" "$LOCAL"
CHANGED=1
echo "$TIMESTAMP [UPDATED] $LOCAL <- $URL" >> "$LOG"
fi
rm -f "$TMP_FILE"
done
# Перезапуск только если что-то изменилось
if [ "$CHANGED" -eq 1 ]; then
echo "$TIMESTAMP [RESTART] Перезапуск Entware..." >> "$LOG"
$RESTART_CMD >> "$LOG" 2>&1
echo "$TIMESTAMP [RESTART] Готово" >> "$LOG"
else
echo "$TIMESTAMP [OK] Изменений нет" >> "$LOG"
fi
# Ротация лога (оставляем последние 200 строк)
if [ -f "$LOG" ]; then
LINES=$(wc -l < "$LOG")
if [ "$LINES" -gt 200 ]; then
tail -100 "$LOG" > "$LOG.tmp"
mv "$LOG.tmp" "$LOG"
fi
fi