#!/bin/bash # rotate-user-maildrop-logs version 0.1 # Copyright (c) 2009 Lamont R. Peterson # P.O. Box 1043, Centerville, Utah, U.S.A. 84014-5043 # # This program is free software. You can redistribute it, or modify it, # or both, under the terms of the GNU General Public License version 3 # (or any later version) as published by the Free Software Foundation. # # This program 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. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . NUMBER_TO_KEEP="5" GZIPPED=".gz" # Set to "" if you don't want the files gzipped, ".gz" if you do. # Loop through all users who have a home directory under /home/. for i in $(ls /home/); do # Does this user have a ~/.mail/.maildrop.log file? if [ -e /home/${i}/mail/.maildrop.log ]; then # Calculate the remailing space until the file will be "full". BYTES_UNTIL_FULL=$(echo "51200000 - $(ls -l /home/${i}/mail/.maildrop.log | sed 's/ */ /g' | cut -d" " -f5)" | bc -l) # Is the user's ~/mail/.maildrop.log file almost full? if [ "${BYTES_UNTIL_FULL}" -lt "51200" ]; then # Does a "too old to keep" file exist? if [ -e /home/${i}/mail/.maildrop.log.${NUMBER_TO_KEEP}${GZIPPED} ]; then # Remove oldest copy that should no longer be kept. rm /home/${i}/mail/.maildrop.log.${NUMBER_TO_KEEP}${GZIPPED} fi # Loop through any old copies from the limit number to 1. for j in $(seq $NUMBER_TO_KEEP -1 1); do # Does the old rotated file exist? if [ -e /home/${i}/mail/.maildrop.log.${j}${GZIPPED} ]; then # Rotate old copy. mv /home/${i}/mail/.maildrop.log.${j}${GZIPPED} /home/${i}/mail/.maildrop.log.$((${j} + 1))${GZIPPED} fi done # Rotate the nearly full file. mv /home/${i}/mail/.maildrop.log /home/${i}/mail/.maildrop.log.1 # Are we gzipp'ing the rotated files? if [ "${GZIPPED}" = ".gz" ]; then gzip /home/${i}/mail/.maildrop.log.1 fi # Create the proper new file. touch /home/${i}/mail/.maildrop.log chown ${i}. /home/${i}/mail/.maildrop.log fi fi done