简体   繁体   English

限制 Unix 中的目录大小

[英]Limit size of directory in Unix

I am using my raspberry pi 4 with raspbian to host server.我正在使用带有 raspbian 的 raspberry pi 4 来托管服务器。 I have written some code that makes backups of the server data and I upload these to google drive using rclone.我编写了一些代码来备份服务器数据,然后使用 rclone 将这些代码上传到谷歌驱动器。 The problem is that I only have the 15 gb free plan on Google drive.问题是我在 Google Drive 上只有 15 GB 的免费计划。 I would want to limit the size of the local directory which I sync to Google drive to 15 gb.我想将同步到 Google 驱动器的本地目录的大小限制为 15 GB。 Is there a way to do this in Linux?有没有办法在 Linux 中做到这一点?

EDIT: I have understood that I am supposed to use Quota to do this.编辑:我明白我应该使用配额来做到这一点。 However Quota seems to be based on different users.但是配额似乎基于不同的用户。 At the moment I only have the standard user pi, is there a way to just limit the size of a certain backup directory with just the pi user.目前我只有标准用户 pi,有没有办法只使用 pi 用户限制某个备份目录的大小。

Quotas in Linux work on a partition level: you can effectively limit a user to 15 GB worth of files on the root partition. Linux 中的配额在分区级别起作用:您可以有效地将用户限制在根分区上的 15 GB 文件。 That will also mean that the user's files will not exceed 15 GB in any given directory.这也意味着用户的文件在任何给定目录中都不会超过 15 GB。

If the user owns files of significant size elsewhere, the target value of 15 GB will not be reachable.如果用户在其他地方拥有相当大的文件,则无法达到 15 GB 的目标值。 On the other hand, if the size of files owned by the user elsewhere is predictable, you could just add it to their quota: eg if you know the user owns a ~5 GB /home folder, you could set the quota to 20 GB, which will enable them to create 15 GB of backups.另一方面,如果用户在其他地方拥有的文件大小是可预测的,您可以将其添加到他们的配额中:例如,如果您知道用户拥有 ~5 GB /home文件夹,则可以将配额设置为 20 GB ,这将使他们能够创建 15 GB 的备份。 Obviously, if some of those 5 GB are subsequently removed, this will allow the user to exceed their 15 GB allowance for backups.显然,如果这 5 GB 中的一些随后被删除,这将允许用户超过 15 GB 的备份限额。

Quotas are really useful if you have lots of users to manage.如果您要管理大量用户,配额非常有用。 If you want to limit the size of a single folder, better solutions exist.如果您想限制单个文件夹的大小,有更好的解决方案。 The best solution would be to add a check to your backup script which removes old backups until the backup folder is under 15 GB .最好的解决方案是在备份脚本中添加一个检查,它会删除旧备份,直到备份文件夹低于 15 GB Using quotas, or making a dedicated 15 GB backup partition, or a 15 GB file mounted to your backup folder, will all have the same drawback: your backup script will fail because of insufficient disk space, and you might end up with an incomplete backup.使用配额,或制作专用的 15 GB 备份分区,或将 15 GB 文件挂载到您的备份文件夹,都会有相同的缺点:您的备份脚本将因磁盘空间不足而失败,并且您最终可能会得到不完整的备份.

Not sure how fast it would be but creating an .img file of 15GB and using it as mounted as storage will do exactly what you want.You can do all the normal things with it like create partitions format them if wanted.不确定它的速度有多快,但创建一个 15GB 的 .img 文件并将其用作存储设备将完全满足您的需求。您可以使用它执行所有正常操作,例如创建分区,如果需要,可以对其进行格式化。

root@zeus-H370M-DS3H:/home/zeus/qemu_vms# fdisk -l ubuntu-20.10-preinstalled-desktop-arm64+raspi.img 
Disk ubuntu-20.10-preinstalled-desktop-arm64+raspi.img: 8.15 GiB, 8750736384 bytes, 17091282 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xb6b5c3c7

Device                                             Boot  Start      End  Sectors  Size Id Type
ubuntu-20.10-preinstalled-desktop-arm64+raspi.img1 *      2048   526335   524288  256M  c W95 FAT32 
ubuntu-20.10-preinstalled-desktop-arm64+raspi.img2      526336 17091247 16564912  7.9G 83 Linux
root@zeus-H370M-DS3H:/home/zeus/qemu_vms# losetup -P /dev/loop0 ubuntu-20.10-preinstalled-desktop-arm64+raspi.img 
root@zeus-H370M-DS3H:/home/zeus/qemu_vms# mkdir /tmp/imgboot
root@zeus-H370M-DS3H:/home/zeus/qemu_vms# mkdir /tmp/imgroot
root@zeus-H370M-DS3H:/home/zeus/qemu_vms# mount /dev/loop0p2 /tmp/imgroot
root@zeus-H370M-DS3H:/home/zeus/qemu_vms# mount /dev/loop0p1 /tmp/imgboot
root@zeus-H370M-DS3H:/home/zeus/qemu_vms# ll /tmp/imgboot/
total 68870
-rwxr-xr-x 1 root root    26530 Oct 22 11:21 bcm2710-rpi-2-b.dtb
-rwxr-xr-x 1 root root    28028 Oct 22 11:21 bcm2710-rpi-3-b.dtb
snip.....
-rwxr-xr-x 1 root root 24222208 Oct 22 11:21 vmlinuz

root@zeus-H370M-DS3H:/home/zeus/qemu_vms# ll /tmp/imgroot/
total 1048660
lrwxrwxrwx   1 root root          7 Oct 22 11:00 bin -> usr/bin
drwxr-xr-x   4 root root       4096 Oct 22 11:12 boot
root@zeus-H370M-DS3H:/home/zeus/qemu_vms# rm -r /tmp/imgroot/*
root@zeus-H370M-DS3H:/home/zeus/qemu_vms# ll /tmp/imgroot/
total 0

You can do pretty much anything you want with them, how fast it would be as I say no clue.你几乎可以用它们做任何你想做的事情,我说不知道有多快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM