通过几个步骤修剪旧备份

发布于 2024-10-20 19:00:19 字数 184 浏览 1 评论 0原文

我正在寻找一种方法来精简旧备份。备份每天运行,我想随着备份变旧而增加间隔。

几天后,我想删除每日备份,只留下“周日”备份。几周后,仅应删除一个月中可用的第一个备份。

由于我正在处理历史备份,因此我不能只更改命名方案。

我尝试使用“查找”来查找,但找不到正确的选项。

有人有什么可以帮忙的吗?

I am looking for a way to thin out old backups. The backups are run on a daily basis, and I want to increase the interval as the backups become older.

After a couple of days I'd like to remove the daily backups, leaving only the "Sunday" backup. After a couple of weeks, only the first backup of a month that is available should be removed.

Since I am dealing with historic backups, I cannot just change the naming scheme.

I tried to use 'find' for it, but couldn't find the right options.

Anyone got anything that might help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

终遇你 2024-10-27 19:00:19

我知道这是历史数据,但您可能更喜欢提出一个命名方案来解决这个问题。通过两遍解决这个问题可能要容易得多:首先,根据日期重命名目录,然后选择将来要保留的目录。

如果 ls -l 输出中的所有目录日期看起来足够好,您可以进行快速近似:

ls -l | awk '{print "mv " $8 " "  $6;}' > /tmp/runme

查看 /tmp/runme,如果它看起来不错,您可以使用 sh /tmp/runme 运行它。您可能希望修剪条目或类似的内容,这取决于您。

如果所有备份都存储在名为例如:的目录中,

2011-01-01/
2011-01-02/
2011-01-03/
...
2011-02-01/
2011-02-02/
...
2011-03-07/

那么您的问题将减少为计算要保留和删除的名称。这个问题比搜索所有文件并尝试根据文件的创建时间选择保留和删除哪些文件要容易得多。 (请参阅date "+%Y-%m-%d" 输出,了解生成此类名称的快速方法。)

方便地命名后,您可以使用以下命令保留每个月的第一个备份:像这样的脚本:

for y in `seq 2008 2010`
    do for m in `seq -w 1 12`
            do for d in `seq -w 2 31`
                    do echo "rm $y-$m-$d"
            done
    done
done

保存其输出,检查它:),然后运行输出,类似于重命名脚本。

一旦您控制了过去的备份,您就可以从 date --date="Last Year" "+%Y" 生成 2010 以及其他改进因此它会处理当月的“每周一次”,并永远保持下去。

I know it is historical data, but you might prefer coming up with a naming scheme to assist this problem. It might be far easier to tackle this problem in two passes: first, renaming the directories based on the date, then selecting the directories to keep in the future.

You could make a quick approximation, if all the directory dates in ls -l output look good enough:

ls -l | awk '{print "mv " $8 " "  $6;}' > /tmp/runme

Look at /tmp/runme, and if it looks good, you can run it with sh /tmp/runme. You might wish to prune the entries or something like that, up to you.

If all the backups are stored in directories named, e.g:

2011-01-01/
2011-01-02/
2011-01-03/
...
2011-02-01/
2011-02-02/
...
2011-03-07/

then your problem would be reduced to computing the names to keep and delete. This problem is much easier to solve than searching through all your files and trying to select which ones to keep and delete based on when they were made. (See date "+%Y-%m-%d" output for a quick way to generate this sort of name.)

Once they are named conveniently, you can keep the first backup of every month with a script like this:

for y in `seq 2008 2010`
    do for m in `seq -w 1 12`
            do for d in `seq -w 2 31`
                    do echo "rm $y-$m-$d"
            done
    done
done

Save its output, inspect it :) and then run the output, similar to the rename script.

Once you've got the past backups under control, then you can generate the 2010 from date --date="Last Year" "+%Y", and other improvements so it handles "one a week" for the current month and maintains itself forever going forward.

你的背包 2024-10-27 19:00:19

我在 @ajreal 的起点上为我的类似需求开发了一个解决方案。我的备份命名为“backup-2015-06-01T01:00:01”(使用日期“+%Y-%m-%dT%H:%M:%S”)。

两个简单的步骤:触摸文件以在每个月的第一天继续使用 shell glob 模式,并使用 findxargs 删除超过 30 天的任何内容。

cd $BACKUPS_DIR

# touch backups from the first of each month
touch *-01T*

# delete backups more than 30 days old
echo "Deleting these backups:"
find -maxdepth 1 -mtime +30
find -maxdepth 1 -mtime +30 -print0 | xargs -0 rm -r

I've developed a solution for my similar needs on top of @ajreal's starting point. My backups are named like "backup-2015-06-01T01:00:01" (using date "+%Y-%m-%dT%H:%M:%S").

Two simple steps: touch the files to keep using a shell glob pattern for first-of-each-month, and use find and xargs to delete anything more than 30 days old.

cd $BACKUPS_DIR

# touch backups from the first of each month
touch *-01T*

# delete backups more than 30 days old
echo "Deleting these backups:"
find -maxdepth 1 -mtime +30
find -maxdepth 1 -mtime +30 -print0 | xargs -0 rm -r
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文