我有一个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
有人会帮我创建一个循环遍历目录中所有文件的脚本,然后删除文件,保留以下内容:
我有以下脚本的开头:
#!/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
答案 0 :(得分:1)
我已经看过很多像这样的花哨脚本用于进行预定备份,并想知道为什么人们不会在大多数现有的* nix发行版中使用logroate
实用程序支持您关注的选项:
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.
答案 1 :(得分:0)
您可以通过迭代文件名来解析时间戳,也可以在find命令中使用-cmin标志(有关详细信息,请参阅man 1 find
)。