#!/bin/sh # # /usr/local/bin/wp-content-cache.sh # # Copyright 2012 - Jean-Sebastien Morisset - http://surniaulula.com/ # # This script is free software; you can redistribute it and/or modify it under # the terms of the GNU General Public License as published by the Free Software # Foundation; either version 3 of the License, or (at your option) any later # version. # # This script is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more # details at http://www.gnu.org/licenses/. base_dir="/export/www/*" wp_content="/wordpress/wp-content" # read command line opts while : do for arg in "$@" do case $arg in -h|--help) echo -e "\npurpose: display size and number of files in WP cache folders prime and optionally flush the OS disk cache.\n" echo -e " syntax: $0 [-h|--help] [-f|--flush] {sitedirs}\n" printf "%15s: %s\n" "--flush" "sync and drop the OS disk cache before priming." printf "%15s: %s\n\n" "{sitedirs}" "optional path to website(s) base folder (default is $base_dir)." exit 0 ;; -f|--flush) flush="1" shift 1 ;; -*) echo "error: unrecognized command line argument." exit 1 ;; *) args[$(( i++ ))]="$1" shift 1 ;; esac continue 2 done break done # reset $1, $2, etc. with left-over parameters set -- "${args[@]}" # set default sitedir(s) if none specified [ -z "$@" ] && set -- $base_dir if [ -n "$flush" ] then echo "(sync'ing disks)" sync echo "(droping caches)" if [ -w /proc/sys/vm/drop_caches ] then # # (excerpt from http://www.bramschoenmakers.nl/node/310) # # Erase the page cache. This part of the disk cache contains actual file contents. # # echo 1 >/proc/sys/vm/drop_caches # # Erase the inode and dentry caches (short: dcache). An inode is the data # structure used by the kernel to represent a file. It does not contain the # file's data, only attributes like the name, dates and permissions. A dentry # is a data structure to represent a file path, which maps to an actual file. # # echo 2 >/proc/sys/vm/drop_caches # # Erase both the page cache, as the inode cache and dcache. # echo 3 >/proc/sys/vm/drop_caches else echo "error: cannot write to /proc/sys/vm/drop_caches file!" fi fi time { for website in "$@" do if [ -d "$website$wp_content" ] then echo "`basename $website`:" # find any directory named "cache" anywhere under wp-content, or "*-cache" directly # under wp-content (for "widget-cache" and others). find "$website$wp_content/" \( -name "cache" -o -regex "^$website$wp_content/[^/]*-cache" \) \ -type d -print | while read cache_dir do printf "\t%-65s %6s %6s files\n" "`echo \"$cache_dir\"|sed \"s!$website/!!\"`" \ "`du -sh "$cache_dir/"|cut -f 1`" "`find "$cache_dir/" -type f -print|wc -l`" find "$cache_dir/" -type f -exec cat {} \; >/dev/null done fi done }