我有MySQL数据库的备份,备份文件的名称中包含时间和日期,如下所示:
daily_amavisd_2016-10-01_05h28m_Saturday.sql.gz
daily_amavisd_2016-10-02_05h28m_Sunday.sql.gz
daily_amavisd_2016-10-03_05h28m_Monday.sql.gz
我需要一个脚本来检查文件夹/backup/amavisd/daily
中的最旧文件是否不超过一周。如果它超过一周,它应该给我状态"backup failed"
,如果它不超过一周,它应该给我状态"backup OK"
。
有人可以帮助我使用shell脚本吗?
答案 0 :(得分:0)
如果你想打印文件名和状态,这可以帮到你;
#!/bin/bash
find /backup/amavisd/daily/ -name '*.gz' -exec bash -c '
file_date=$(echo $1|cut -f 3 -d '_');
start_date=$(date +"%Y-%m-%d" -d "-7 days");
if [[ "$file_date" > "$start_date" ]]; then
echo "$1: backup OK"
else
echo "$1: backup failed"
fi' -- {} \;
例如
user@host:/tmp/test$ ./test.sh
backup/amavisd/daily/daily_amavisd_2016-10-01_05h28m_Saturday.sql.gz: backup OK
backup/amavisd/daily/daily_amavisd_2016-10-03_05h28m_Monday.sql.gz: backup OK
backup/amavisd/daily/daily_amavisd_2016-10-02_05h28m_Sunday.sql.gz: backup OK
backup/amavisd/daily/daily_amavisd_2016-10-07_05h28m_Monday.sql.gz: backup OK
backup/amavisd/daily/daily_amavisd_2016-09-07_05h28m_Monday.sql.gz: backup failed