0

I am trying to create a script that will find specific log files in different folders and compress them using tar. This is how I am doing:

find /tmp/logs -type f -name '*.log*' -mtime -1 -print0 | tar -czf $target_folder1/$current_date.tar.gz --null -T -

find /home/second_logs -type f -name '*.log*' -mtime -1 -print0 | tar -czf $target_folder2/$current_date.tar.gz --null -T -

This works great, find all .log files under /tmp/logs and /home/second_logs within last day and compress them. Now, when there is a process writing to any log file under /tmp/logs, tar will give me a warning but then the script won't execute the second find/tar command and just exit out:

tar: Removing leading `/' from member names
tar: /tmp/logs/somelog.log: file changed as we read it

So I get the first compressed tar file but the second one won't be executed. How can I make sure both find commands will be executed in my bash script?

Full script:

#!/bin/bash -xe

serverhost=$(hostname -s)
current_date=$(date +"%Y-%m-%d")

target_folder=/tmp/pull-logs
app1_logs=/tmp/logs/app1
app2_logs=/tmp/logs/app2
tomcat_logs=/tmp/logs/tomcat

find $app1_logs -type f -name '*.log*' -mtime -1 -print0 | tar -czf $target_folder/$serverhost-app1-$current_date.tar.gz --null -T -
find $app2_logs -type f -name '*.log*' -mtime -1 -print0 | tar czf $target_folder/$serverhost-app2-$current_date.tar.gz --null -T -
find $tomcat_logs -type f -name '*.log*' -mtime -1 -print0 | tar czf $target_folder/$serverhost-tomcat-$current_date.tar.gz --null -T -

#aws s3 cp $target_folder s3://my/s3 --recursive

Thank you!

CaioT
  • 1,973
  • 1
  • 11
  • 20
  • Add your script to your question including its shebang. – Cyrus Dec 22 '20 at 18:56
  • Normally a bash script keeps running past a failed command (and expect the script to handle the error)... that is unless you've explicitly told it otherwise, e.g. by running `bash` with `-e`, using `set -e` / `set -o errexit`. Which tracks back to the other comment posted here -> context is important. – Ondrej K. Dec 22 '20 at 19:03
  • @Cyrus I've added the full script. Thank you! – CaioT Dec 22 '20 at 19:07
  • Why do you use `bash -e`? – Cyrus Dec 22 '20 at 19:11
  • Disregard the -e, I meant to only run with -x to see if I could catch anything during execution. – CaioT Dec 22 '20 at 19:13
  • `How can I make sure both find commands will be executed in my bash script?` Remove `-e`. Remove `-xe` on the first line. – KamilCuk Dec 22 '20 at 19:17
  • LOL! Thank you KamilCuk and Cyrus. Beginner mistake right here. It worked! – CaioT Dec 22 '20 at 19:20

0 Answers0