1

I am trying to create a script such that it could tar.gz all the previous log of per 7 days log and exclude the tar file in next run.

Is the script ok or should I have to make a change

find /home/bankex/bankex/glassfish/domains/bankex/logs/ -mtime -7 -print0 | xargs tar -czvf /home/bankex/bankex/glassfish/domains/bankex/logs/oldlogs_$(date +"%Y%m%d").tar.gz --exclude=/home/bankex/bankex/glassfish/domains/bankex/logs/*.tar.gz --null
find /home/bankex/bankex/glassfish/domains/bankex/logs/server.log* -mtime -7 -delete
2
  • 1
    Create a playground directory with some files that can be deleted and try your commands. This will probably show some problems. Read the documentation of the find and xargs commands. find ... -print0 | needs xargs -0. This combination might not do what you want if you have many files which will cause xargs to run the command several times.
    – Bodo
    Jan 18, 2019 at 10:19
  • 1
    There is a linux utility called "logrotate" that comes with most GNU/Linux OS and can easily compress and rotate logs. You only need to create a file in /etc/logrotate.d/ with the desired configuration and restart the daemon. You can take a look at the "CONFIGURATION FILE" section in "man logrotate". I know you want to create a script, but I just thought that maybe you didn't know this.
    – rbrtflr
    Jan 18, 2019 at 10:48

1 Answer 1

1

Try this,

 find /home/bankex/bankex/glassfish/domains/bankex/logs/ -maxdepth 1 -mtime -7 -type f ! -name "*.tar.gz" |  xargs tar -czvf /home/bankex/bankex/glassfish/domains/bankex/logs/oldlogs_$(date +"%Y%m%d").tar.gz --remove-files
  • maxdepth levels of directories
  • type f only files
  • ! -name excludes the file with pattern .tar.gz
  • --remove-files will remove files which are tared
3
  • Dear sir, i need to exclude the server.log file too as it has to be static and should remain there to create log Jan 20, 2019 at 14:01
  • @TheStoryTellerNepal you can add another exclude name
    – Siva
    Jan 21, 2019 at 6:46
  • -mtime should it be +7? Feb 17, 2020 at 8:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .