0

我有一个 bash 脚本,它每小时在某个目录中创建一个 mysqldump 备份。

根据以下架构,备份文件的文件名包括日期和小时:

backupfile_<day>-<month>-<year>_<hour>.sql.gz

并在这里澄清一些示例文件名:

backupfile_30-05-2012_0800.sql.gz
backupfile_01-06-2012_0100.sql.gz
backupfile_05-06-2012_1500.sql.gz

有人可以帮我创建一个脚本,该脚本将遍历目录中的所有文件,然后删除文件,留下以下内容:

  1. 保留超过一天的备用小时备份
  2. 保留一周以上的每日两次备份
  3. 保留超过一个月的每日备份一次。

我有以下脚本的开头:

#!/bin/bash
cd /backup_dir

for file in *
do
    # do the magic to find out if this files time is up (i.e. needs to be deleted)
    # delete the file
done
4

2 回答 2

1

我见过很多像这样的花哨的脚本来进行预定的备份,并且想知道为什么人们不使用logroate大多数 *nix 发行版上可用的实用程序,现在支持以下您感兴趣的选项:

compress
     Old versions of log files are compressed with gzip by default.

dateext
     Archive old versions of log files adding a daily extension like YYYYMMDD instead
     of simply adding a number.

olddir directory
     Logs are moved into directory for rotation. The directory must be on the same
     physical device as the log file being rotated, and is assumed to be relative to
     the directory holding the log file unless an absolute path name is specified.
     When  this  option is used all old versions of the log end up in directory. This
     option may be overriden by the noolddir option.

notifempty
      Do not rotate the log if it is empty (this overrides the ifempty option).

postrotate/endscript
      The lines between postrotate and endscript (both of which must appear on lines by
      themselves)  are  executed  after the  log file is rotated. These directives may
      only appear inside of a log file definition.  See prerotate as well.
于 2012-06-06T13:10:53.680 回答
0

您可以通过遍历文件名来解析时间戳,也可以在 find 命令中使用 -cmin 标志(详情请参阅man 1 find)。

于 2012-06-06T13:09:35.090 回答