【问题标题】:Shell script to remove backups that are older than 2 Weeks用于删除超过 2 周的备份的 Shell 脚本
【发布时间】:2017-11-06 23:04:57
【问题描述】:

我有一个每天备份 Mongo DB 的 shell 脚本。它按预期工作。现在我需要删除超过 2 周的备份。这可以通过当前的命名约定来实现吗?任何人都可以解释一下吗?我对 shell 脚本还很陌生

#!/bin/sh
DIR=`date +%m%d%y`
DEST=/dbBackups/$DIR
mkdir $DEST
mongodump --authenticationDatabase admin -h 127.0.0.1 -d pipe -u <username> -p <password>

【问题讨论】:

标签: shell ubuntu scripting cron


【解决方案1】:

终于用下面的脚本搞定了

#!/bin/sh
DIR=`date +%m%d%y`
DEST=/dbBackups/$DIR
mkdir $DEST
mongodump --authenticationDatabase admin -h 127.0.0.1 -d pipe -u <username> -p <password>
find /dbBackups/* -type d -ctime +14 -exec rm -rf {} +

感谢Shell script to delete directories older than n days

【讨论】:

  • 使用 GNU find,运行 -exec rm -rf {} + 会更快,因为它不会为每个文件生成单独的进程。
  • 只是在这个答案中使用代码的人的注释,我认为您可能希望在 mongodump 行的末尾添加“-o $DEST”,例如 mongodump --authenticationDatabase admin -h 127.0.0.1 -d 管道 -u -p -o $DEST
猜你喜欢
  • 2012-12-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-03
  • 1970-01-01
  • 1970-01-01
  • 2013-11-09
  • 1970-01-01
相关资源
最近更新 更多