假设每天都运行备份,如何仅使用 100 个备份副本来保留过去 1 年的备份?
假设我每天创建一个备份副本,并假设每个备份都是一个压缩文件,并且文件名中包含时间戳信息。
现在,我想编写一个每日运行的脚本,删除较旧的备份并仅保留 100 个备份。然而,这 100 个备份分布在 1 年的时间里,这样我仍然有 1 年前的副本,但是我应该有更多的最近备份副本和更少的旧备份副本,即在任何给定时刻,两个幸存的连续备份之间的距离随着时间的推移,副本不断增加。
算法还应该考虑到它将每天运行一次这一事实。
另外,这种算法有名称吗 - 指数范围?
、指数距离?
、< code>非线性备份到期?,对数什么?
什么?
如果您在答案中包含代码,我更喜欢 Ruby(但不是必需的,因为如果它是其他语言,我可以手动将其转编译为 Ruby)。
Assuming that I create a backup copy everyday and assuming that each backup is a single compressed file and contains the timestamp information in its filename.
Now, I want to write a daily running script that deletes older backups and keeps only 100 backups. However these 100 backups are spread over 1 year such that I still have a 1 year old copy, however I should have more copies of recent backups and lesser copies of older backups, i.e., at any given moment, the distance between two surviving consecutive backup copies keeps increasing as we go back in time.
Algorithm should also take the fact into account that it will be running once daily.
Also, is there a name for such algorithm - exponential range?
, exponential distance?
, non-linear backup expiry?
, logarithmic something?
anything?
If you're including code in your answer, I'd prefer Ruby (but not necessary, as I can manually transcompile it to Ruby if it is in another language).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
听起来您正在考虑类似河内塔之类的东西。磁带备份系统有时使用它作为管理备份磁带的方法。维基百科文章有多种算法。当您学习递归时,它通常用作编程练习。
该游戏的基础是将一堆不同大小的圆盘从一个钉子移动到另一个钉子,而无需将较大的圆盘放在较小的圆盘上。如果您有五套磁带,分别标记为 A 到 E,您最终会像这样移动它们。
最不常用的集合是 E,因此如果您在 1 月 1 日从 E 开始,您最终会像这样。
E 集将于 1 月 17 日被覆盖。1 月 16 日的覆盖范围将如下所示。
一个 ruby 程序,解决河内塔谜题
It sounds like you're thinking about something like the Tower of Hanoi. Tape backup systems sometimes use this as a way to manage backup tapes. The Wikipedia article has several algorithms. It's often used as a programming exercise when you're learning recursion.
The game is based on moving a stack of different sized disks from one peg to another without ever putting a bigger disk on top of a smaller one. If you had five sets of tapes, labelled A through E, you'd end up moving them like this.
The set used least often is E, so if you start with E on January 1, you'll end up like this.
The E set will be overwritten on Jan 17. Coverage will look like this on Jan 16.
A ruby program that solves the Tower of Hanoi puzzle
让我描述一个可能适合您的非常简单的算法。
首先,每天分配一个全局日期计数器,比如基督之后的天数。您想要 ruby,所以我向您展示如何在 ruby 中执行此操作(这表明今天是第 734918 天)
现在让我告诉您保留哪些文件:您保留过去 30 天的所有文件,在接下来的 40 天内保留每个文件索引可被 2 整除的文件,在接下来的 80 个文件中,您每天保留索引可被 4 整除的文件,在接下来的 120 个文件中,您保留索引可被 8 整除的每个文件,其余的文件85/86 您保留每个文件的索引可被 16 整除。因此,随着时间的推移,您将需要检查是否需要删除某些存储的文件,并且永远不需要显示您已删除的文件。告诉我您是否也需要此逻辑的代码。
Let me describe a very simple algorithm that might work for you.
First of all assign everyday a global date counter, say number of days after Christ. You wanted ruby, so I show you how to do that in ruby (this shows that today is 734918th day)
Now let me tell you which files you keep: You keep all files from the last 30 days, for the next 40 you keep every file with index divisible by 2, for the next 80 you keep every day with index divisible by 4, for the next 120 you keep every file with index divisible by 8 and for the rest 85/86 you keep every file with index divisible by 16. Thus as time moves on all you will need will be to check if you need to remove some of the stored files and will never need to show a file you already erased. Tell me if you need code for this logic, too.
保留过去两个月的所有内容以及去年每个周日的备份将导致 100 多个文件。易于解释,易于执行,并且您可以事先知道备份是否存在。
未经测试:
Keeping everything from the last two months and every sunday backup from the last year would result in a little over 100 files. Easy to explain, easy to execute and you know beforehand if a backup is there or not.
Untested: