通过删除旧文件来限制目录的大小

Sul*_*ane 4 bash scripts directory resource-limiting

我有一个 IP 摄像头,可将其录音保存在我的Ubuntu Server 12.04 中名为Camera1的特定目录中。

我想将此文件夹的大小限制为5 gigs ,方法是删除 - 比如每天一次 - 最旧的文件。

我首先检查了配额程序,但它似乎不允许创建新文件和删除旧文件。

所以我认为最好的方法是运行一个 bash 脚本......

max*_*max 7

我处于非常相似的情况。我有一个包含 2 个目录的大 LVM 分区:

  • 视频(录制的直播流)可以使用 88% 的分区

  • 图片(摄像头检测到运动时发送的图片)可以使用7%的分区(因为图片要轻得多,我仍然可以保留比视频更旧的图片)

剩下的 5% 是安全余量,以便分区永远不会变满。注意我说的是百分比而不是固定的千兆字节,因为如果我更换或添加硬盘驱动器,我的 LVM 分区的大小会发生变化。

所以这是脚本(我添加了很多注释,所以很容易理解):

#!/bin/bash
#Usage = sh limit-directory-size.sh /media/computer/mypartition 88 120 ("/media/computer/mypartition" = the directory to be limited / "88"=the percentage of the total partition this directory is allowed to use / "120"=the number of files to be deleted every time the script loops (while $Directory_Percentage > $Max_Directory_Percentage)

#Directory to limit
Watched_Directory=$1
echo "Directory to limit="$Watched_Directory

#Percentage of partition this directory is allowed to use
Max_Directory_Percentage=$2
echo "Percentage of partition this directory is allowed to use="$Max_Directory_Percentage

#Current size of this directory
Directory_Size=$( du -sk "$Watched_Directory" | cut -f1 )
echo "Current size of this directory="$Directory_Size

#Total space of the partition = Used+Available
Disk_Size=$(( $(df $Watched_Directory | tail -n 1 | awk '{print $3}')+$(df $Watched_Directory | tail -n 1 | awk '{print $4}') ))       
echo "Total space of the partition="$Disk_Size

#Curent percentage used by the directory
Directory_Percentage=$(echo "scale=2;100*$Directory_Size/$Disk_Size+0.5" | bc | awk '{printf("%d\n",$1 + 0.5)}')
echo "Curent percentage used by the directory="$Directory_Percentage

#number of files to be deleted every time the script loops (can be set to "1" if you want to be very accurate but the script is slower)
Number_Files_Deleted_Each_Loop=$3
echo "number of files to be deleted every time the script loops="$Number_Files_Deleted_Each_Loop

#While the current percentage is higher than allowed percentage, we delete the oldest files
while [ $Directory_Percentage -gt $Max_Directory_Percentage ] ; do
    #we delete the files
    find $Watched_Directory -type f -printf "%T@ %p\n" | sort -nr | tail -$Number_Files_Deleted_Each_Loop | cut -d' ' -f 2- | xargs rm
    #we delete the empty directories
    find $Watched_Directory -type d -empty -delete
    #we re-calculate $Directory_Percentage
    Directory_Size=$( du -sk "$Watched_Directory" | cut -f1 )
    Directory_Percentage=$(echo "scale=2;100*$Directory_Size/$Disk_Size+0.5" | bc | awk '{printf("%d\n",$1 + 0.5)}')
done
Run Code Online (Sandbox Code Playgroud)

然后你每 15 分钟调用一次 crontab 条目

*/15 * * * * sh /home/computer/limit-directory-size.sh /media/computer/mypartition/videos 88 120
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你!


小智 5

我开始思考只保留一定数量的文件会有多困难。我转向了一段时间没有使用的awk,并想出了以下一个班轮。

cd /path/to/Camera1 && ls -ltc | awk '{ if (!system("test -f " $9)) { size += $5; if (size > 5*2^30 ) system("rm " $9) } }'
Run Code Online (Sandbox Code Playgroud)
  1. 更改到有问题的目录
  2. 列出文件,最新的在前
  3. 在输出上运行 awk,检查它是否是常规文件,将文件大小添加到计数器,如果累积大小超过 5 个演出,则删除文件

您可以将“rm”更改为“ls”以使其列出要删除的文件。如果不仔细测试由网络上的未知人员建议的删除文件的脚本,那将是疯狂的!

如果文件名中包含有趣的字符(例如空格),脚本可能会中断和/或无法执行您期望的操作。


小智 3

find命令可用于查找文件并删除它们。例如,以下命令将删除 7 天前创建的文件:

find /path/Camera1 -ctime +7 -delete 
Run Code Online (Sandbox Code Playgroud)

crontab如果您想安排时间,请使用;这里有更多信息