0

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

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.

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. Also, after having zip file, I want to delete old log files. In other words, I am trying to write a log-backup script. Can you help me please?

Thank you very much!

smsms
  • 27
  • 6
  • Does this help you? https://stackoverflow.com/questions/158044/how-to-use-find-to-search-for-files-created-on-a-specific-date – Dominique Nov 19 '18 at 13:21
  • @Dominique I tried adding find . -type f -newerct $tendaysbefore ! -newerct $now But it gives me this error: find: I cannot figure out how to interpret ‘!’ as a date or time – smsms Nov 19 '18 at 13:53
  • That is because your `$tendaysbefore` is empty thus executing the actual command `find . -type f -newerct ! …`. Probably because your script above is incorrect. At the very least you want something like: `tendaysbefore=$(date -d "$now - 10 days" '+%F')` – grifferz Nov 19 '18 at 21:22
  • Actually it is not empty. I can print the value in it and it is correct. The error is most probably caused by syntax that '!' is not understood by the compiler. Any suggestion about this? @grifferz – smsms Nov 20 '18 at 06:14
  • I'm sorry, but you are mistaken. Running your code above results in `tendaysbefore` being left empty, that is why find gives you that error. Your assignment to `tendaysbefore` is missing `$()` around the date command as I said. – grifferz Nov 20 '18 at 15:00

1 Answers1

0
#!/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. Later(inside loop) you can do whatever you want like adding it to existing zip or searching for its existence.

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
bhanu7k
  • 65
  • 9
  • Thx for your answer. But I already have many log files and do not need to create any more. What I am asking for is how to find the ones that are created in last 10 days and I also want to zip these log files (which are created in last 10 days) in every 10 days. So this is like making a back-up script for my log files(which are created every day) in every 10 days. So your answer is not what I am looking for. Now, do you have any other suggestion? – smsms Nov 19 '18 at 14:51
  • Updated my answer – bhanu7k Nov 20 '18 at 07:38
  • Thanks! I guess it is working.. but I am a lil bit confused..what does 2>&1 mean? also -tt? – smsms Nov 20 '18 at 12:29
  • Oh I got 2>&1 but -tt and -t still are still confusing. Can you please briefly explain them? Thanks! – smsms Nov 20 '18 at 14:35
  • try to read the output of zip -h2. It will give description of lot of options using zip. -t and -tt are the options to set the date range. – bhanu7k Nov 21 '18 at 06:01
  • hey -h2 gives more explanation than I expected. Thanks, I will check it immediately.. I was just wondering how checking between two dates (now and tendaysbefore) happened here..is it options? – smsms Nov 21 '18 at 06:29
  • Yes those are options provided by zip utility. the description from -h2 gives you the clarity. You can try different examples to understand how it works. – bhanu7k Nov 22 '18 at 07:37