これは、最近cronジョブ用に行った半関連のbashスクリプトです。自動生成されたバックアップが保存されている特定のディレクトリを調べ、ディレクトリのサイズがユーザーが指定した制限を超えると、最も古いものを削除します。.log ファイルを維持しながら。
#!/bin/bash
# IFS is needed for bash to recognize spaces
IFS=$'\n'
# Backup directory
BACKUPDIR="your directory here"
# Backup directory size limit (in kB)
DIRLIMIT=20971520
# Start logging
LOGFILE="where you want the log file stored"
echo -e "Cron job started at $(date +"%m/%d/%Y %r")\r" >> $LOGFILE
echo -e "Directory size limit: $DIRLIMIT [kB]\r" >> $LOGFILE
# If $BACKUPDIR exitsts, find its size
if [ -d "$BACKUPDIR" ]
then
DIRSIZE=$(du -sk "$BACKUPDIR" | awk '{print $1}')
echo -e "Current directory size: $DIRSIZE [kB]\r" >> $LOGFILE
else
echo -e "$BACKUPDIR does not exist!\r" >> $LOGFILE
fi
# Check if $BACKUPDIR's size is greater than $DIRLIMIT. If so, delete
# old files. If not, exit.
if [ $DIRSIZE -gt $DIRLIMIT ]
then
echo -e "Directory size over limit! Attempting to delete oldest log backups...\r" >> $LOGFILE
LOOPSIZE=$DIRSIZE
while [ $LOOPSIZE -gt $DIRLIMIT ]
do
# This find command below finds files (-type f) in the $BACKUPDIR directory only
# (-maxdepth 1) and it prints their last modified time and filename followed by a new line (-printf "%T@ %p\n").
# Then it sorts it based on time (sort -n) and selects the file which was modified the furthest in the past. The
# awk command removes the timestamp (which is in field $1). Finally, xargs -I{} -f {} deletes the file even though spaces are
# present in the full file name.
echo -e "Deleting file: $(find "$BACKUPDIR" -type f -maxdepth 1 -printf "%T@ %f\n" | sort -n | head -1 | awk '{ print substr($0, index($0,$2)) }')\r" >> $LOGFILE
find "$BACKUPDIR" -type f -maxdepth 1 -printf "%T@ %p\n" | sort -n | head -1 | awk '{ print substr($0, index($0,$2)) }' | xargs -I{} rm -f {}
# This function calculates the $BACKUPDIR size again and is used in the loop to see when it is permissable to exit.
LOOPSIZE=$(du -sk "$BACKUPDIR" | awk '{print $1}')
echo -e "Directory size is now: $LOOPSIZE [kB]\r" >> $LOGFILE
done
echo -e "Operation completed successfully! Final directory size is: $LOOPSIZE [kB]\r" >> $LOGFILE
else
echo -e "Directory size is less than limit. Exiting.\r" >> $LOGFILE
fi
# Finish logging
echo -e "\r" >> $LOGFILE
echo -e "Cron job exiting at $(date +"%m/%d/%Y %r")\r" >> $LOGFILE
echo -e "----------------------------------------------------------------------\r" >> $LOGFILE
.log ファイルが不快なほど大きくならないようにするために、一番上のエントリを削除する cron スクリプトを作成しました。
#!/bin/bash
# IFS is needed for bash to recognize spaces
IFS=$'\n'
# .log files directory
LOGFILESDIR="your directory here"
# .log file size limits (in KB)
LOGFILELIMIT=35
# Find .log files in $LOGFILESDIR and remove the earliest logs if
# the filesize is greater than $LOGFILELIMIT.
for FILE in "$LOGFILESDIR"/*.log
do
LOGSIZE=$(du -sk "$FILE" | awk '{print $1}')
while [ $LOGSIZE -gt $LOGFILELIMIT ]
do
# The sed command deletes the rows from the top
# until it encounters a bunch of "-"'s in a row
# (which separates the logs in the log files)
sed -i '1,/----------/d' "$FILE"
LOGSIZE=$(du -sk "$FILE" | awk '{print $1}')
done
done
exit