[英]how to check my file is created within 10 days in shell script
I have a bunch of log files which are named according to their creation dates. 我有一堆根据其创建日期命名的日志文件。 For example; 例如; if my log file is created on 12 March 2018, the name of the logfile is log-2018-03-12.log
如果我的日志文件创建于2018年3月12日,则日志文件的名称为log-2018-03-12.log
Here is what I want to do: From today's date, I want to check the name of my log files and zip the log files which are created in last 10 days. 这是我想做的事情:从今天起,我要检查日志文件的名称并压缩最近10天创建的日志文件。
Here is my code that zip all log files in a specific directory: 这是将所有日志文件压缩到特定目录中的代码:
#!/bin/bash
# current time
now=$(date +"%F")
backupfile="backup-$now"
scripthome=/opt/MyStore/action_scripts/deneme/
tendaysbefore= date -d "$now - 10 days" '+%F'
for file in $scripthome;
do
find "$(basename "$file")" | zip -R $backupfile.zip "log-2*.log"
done
But I want to zip last 10 days log file, not all log files, and also I want to continue doing it for every 10 days after this. 但是我想压缩最近10天的日志文件,而不是所有日志文件,而且我想在此之后每10天继续压缩一次。 Also, after having zip file, I want to delete old log files. 另外,在拥有zip文件之后,我想删除旧的日志文件。 In other words, I am trying to write a log-backup script. 换句话说,我正在尝试编写一个日志备份脚本。 Can you help me please? 你能帮我吗?
Thank you very much! 非常感谢你!
#!/bin/bash
END=10
for ((i=1;i<=END;i++)); do
file=log-`date -d "$i days ago" +"%F"`.log
echo $file
done
With the above script you have file names for last 10 days. 使用上述脚本,您可以获得最近10天的文件名。 Later(inside loop) you can do whatever you want like adding it to existing zip or searching for its existence. 稍后(在内部循环中),您可以执行任何想要的操作,例如将其添加到现有zip或搜索其存在性。
Edit: 编辑:
Following code may be useful according to your requirement 根据您的要求,以下代码可能会有用
#!/bin/bash
# current time
now=$(date +"%F")
backupfile="backup-$now"
scripthome=/home/bhanu/opt/MyStore/action_scripts/deneme/
tendaysbefore=`date -d "$now - 10 days" '+%F'`
for file in $scripthome;
do
zip -r -tt $now -t $tendaysbefore "$backupfile.zip" $scripthome/log-*.log > add.log 2>&1
zip "$backupfile.zip" -d "*" -tt $tendaysbefore > delete.log 2>&1
done
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.