Clear Squid Cache Secara Otomatis

5:10 PM 0 Comments

Disini perlu dibuat 1 script untuk melakukan pengecekan besar cache terpakai, dan bila telah melewati batasan yang diperbolehkan, maka script berikutnya akan dieksekusi.
touch clear-cache.sh
chmod +x clear-cache.sh
nano clear-cache.sh

Lalu isikan dengan script berikut: (sesuaikan direktori cachenya)

#!/bin/bash

# direktori cache proxy
CACHEDIR=/cache-1

# kapasitas direktori cache proxy (70GB)
CACHEDIRSIZE=75161927680
ONEMB=1048576
ONEGB=1073741824
COUNTMB=`expr $CACHEDIRSIZE / $ONEMB`
COUNTGB=`expr $CACHEDIRSIZE / $ONEGB`

COUNTALOC=`expr $CACHEDIRSIZE / $ONEMB`

# mendapatkan besaran direktori cache saat ini
SIZE=`du -bc $CACHEDIR | grep total | awk '{print $1}'`
SIZEM=`du -bch $CACHEDIR | grep total | awk '{print $1}'`

# jika besaran direktori cache saat ini sama atau lebih besar dari
# kapasitas direktori cache proxy maka hapus cache proxy
if [ $SIZE -ge $CACHEDIRSIZE ]
then

/usr/bin/clear
echo "=================================="
echo "=== Generate Clear Cache Squid ==="
echo "======= jaya2na@gmail.com ========"
echo "=================================="

echo "Cahce Squid Proxy      :" $CACHEDIR
echo "Jumlah cache tersimpan :" $SIZE Byte / $SIZEM
echo "Batas maximum cache    :" $CACHEDIRSIZE Byte / $COUNTALOC MB
echo " * Clear cache squid in proccess ..."

sleep 10

/etc/init.d/squid stop        # stop service squid
rm -fdR $CACHEDIR/*           # hapus cache proxy
squid -z                      # membuat cache direktori
/etc/init.d/squid start       # start service squid

/usr/bin/clear

echo "===================================="
echo "=== Clear Cache Squid Success !! ==="
echo "======== jaya2na@gmail.com ========="
echo "===================================="

else

/usr/bin/clear
echo "================================================"
echo "=== Status Directory Cache Squid Proxy  [OK] ==="
echo "===           jaya2na@gmail.com              ==="
echo "================================================"
echo "Cahce directory squid :" $SIZE Byte / $SIZEM
echo "Alokasi cahce squid   :" $CACHEDIRSIZE Byte / $COUNTMB MB / $COUNTGB GB

fi

Lalu jadwalkan dengan crontab: (mis tiap hari jam 11 siang)
1 11 * * * ./clear-cache.sh