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

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

我有一个 IP 摄像头,它将其记录保存在名为相机1在我的Ubuntu 服务器 12.04

我想限制此文件夹的大小为5 场演出,通过删除(比如说每天一次)最旧的文件。

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

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

答案1

我的情况非常相似。我有一个很大的 LVM 分区,其中包含 2 个目录:

  • 视频(录制的直播)可以使用 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

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

*/15 * * * * sh /home/computer/limit-directory-size.sh /media/computer/mypartition/videos 88 120

希望能帮助到你!

答案2

我开始思考只保留一定数量的文件会有多难。我转向 awk(我已经有一段时间没用它了),并想出了下面的一行代码。

cd /path/to/Camera1 && ls -ltc | awk '{ if (!system("test -f " $9)) { size += $5; if (size > 5*2^30 ) system("rm " $9) } }'
  1. 更改至相关目录
  2. 列出文件,最新文件优先
  3. 在输出上运行 awk,检查它是否为常规文件,将文件大小添加到计数器,如果累计大小超过 5 GB,则删除文件

您可以将“rm”改为“ls”,让其列出要删除的文件。如果不仔细测试网上某个不知名人士推荐的删除文件的脚本,那简直是疯了!

如果文件名中包含奇怪的字符(例如空格),脚本可能会中断和/或无法按照您的预期执行。

答案3

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

find /path/Camera1 -ctime +7 -delete 

crontab如果您想安排它,请使用;更多信息请点击此处

答案4

使用 awk 的绝佳解决方案!

不过,我会采取额外措施来防止您的垃圾桶溢出。

在我的 cron 中......

如果超过 500mb,则擦除旧的摄像机镜头(请注意大小修复 += $5)

*/2 * * * * cd /home/me/Desktop/Dropbox/a_security_cam && ls -ltc | awk '{ if (!system("test -f " $9)) { size += $5; if (size > 0.5*2^30 ) system("rm " $9) } }'

清空垃圾文件,最旧的优先,超过 2GB

*/10 * * * * autotrash –min-free 2048

http://www.logfish.net/pr/autotrash/

相关内容