#!/bin/bash dir=/home/webadmin/twiki USAGE="daily_cleanup This script is to be run daily under the login of the apache web server or root to do various maintainance operations on TWiki installations: * truncate the debug.txt & warning.txt file that can grow huge, and are only needed to temporarily debug * remove topics & attachments older than N days in some webs (Trash, Tmp) * Note that it does not remove Trash from today, only yesterday and beyond USAGE: [options] [wiki-dir] Options: -n do nothing, just prints what whould be done wiki-dir by default is $dir " set -a #set -xv unset CDPATH # avoid cd messages doit=true #doit=false while test -z "${1##-*}" -a -n "$1";do case "$1" in -n) doit=false;; *) echo "$USAGE"; exit 1;; esac;shift done if test -d "$1"; then dir="$1"; fi # local wiki data dir cd $dir/data # truncate debug.txt & warning.txt #>debug.txt; chmod a+rw debug.txt 2>/dev/null #>warning.txt; chmod a+rw warning.txt 2>/dev/null system_web=_default #cd _default #system_topics=' ' #for i in `find . -name \*.txt`; do system_topics="$system_topics $i"; done #system_topics="$system_topics " #echo "The following system topics will be excluded from the sweep: '".$system_topics."'" clean_web () { web=$1 days=$2 if test -d data/$web; then # topics cd $dir/data/$web for fileInWeb in `find . -type f -name \*.txt -mtime +$days`; do if [ -f $dir/data/$system_web/$fileInWeb ]; then echo "Ignoring system topic ". $fileInWeb else remove_topic "$fileInWeb" fi done # corresponding attachments if test -d ../../pub/$web; then cd ../../pub/$web for topic_and_attachment in `find . -type f -mtime +$days`; do topic_and_attachment="${topic_and_attachment#./}" if [ -f $dir/pub/$system_web/$topic_and_attachment ]; then echo "Ignoring as $topic_and_attachment is a system attachment" else remove_attachment ../../data/$web/"${topic_and_attachment%%/*}.txt" "${topic_and_attachment#*/}" "$topic_and_attachment" fi done fi # now delete from the topic any references to attachments that have been deleted cd $dir/data/$web grep '^%META:FILEATTACHMENT[{]' *.txt| while read line; do topic="${line%%:*}" file="${line#*\"}"; file="${file%%\" *}" if test ! -e "../../pub/$web/${topic%.txt}/$file"; then remove_attachment $topic "$file" fi done fi } # suppose we are in data/web, argument is file name remove_topic () { if $doit; then rm -rf "$1"* rm -rf ../../pub/$web/${1%.txt} else echo "$web: Removing topic ${1#./} & attachments" fi } # create a new topic file that does not mention the attachments # $1= topic.txt $2= relative file name $3= complete path of file (optional) remove_attachment () { topic="$1"; file="$2"; path="$3" if $doit; then mv $topic .topic fgrep -v "META:FILEATTACHMENT{name=\"$file\"" <.topic >$topic chmod a+rw $topic 2>/dev/null rm -f .topic if test -n "$path"; then rm -rf "$path"; fi else echo "$web: Removing attachment $file from topic $topic" fi } cd $dir clean_web Trash 0 cd $dir clean_web Tmp 100