I am currently working on script with the following goals:
- Script will check every 15-minute if there is
AppA.log4in/logsdirectory. There are numerous apps in/logsdirectory, for exampleAppA.log4,AppB.log4,AppC.log4 - If there is
AppA.log4, it will be renamed toAppA.log.<timestamp> zipthe renamed file and move to/backupDIR
So far, I have created working script however it is hardcoded.
Can you help improving this to be more dynamic? For example, AppA.log4 and AppB.log4 will be moved and zipped, leaving AppC.log4 in the /logs directory.
What if there's a separate file that lists all the App (AppA and AppB) that will be read by this script. How can I apply it here. Thank you in advance.
TIMESTAMP=`date "+%Y.%m.%d-%H.%M"`
LOGS="AppA.log.4"
APPLOGS_DIR="/logs/"
BACKUP_DIR="/backupDIR/"
cd $APPLOGS_DIR;
if [ -f "$LOGS" ];
then
mv $LOGS $BACKUP_DIR;
cd $BACKUP_DIR;
mv $LOGS AppA_$TIMESTAMP;
gzip AppA_$TIMESTAMP;
else
echo "No log file to be backed up"
fi
ls | find *.log4 | sed s/log4/logto dynamically use the names (also it will rename log4 to log) – theSwapnilSaste Jul 17 '20 at 07:00What I mean is, my goal now is to move and gzip AppA.log4 and AppB.log4. However there is still AppC.log4 in the /logs/ directory. I just want to specify AppA and AppB. I am thinking of another file where I can input AppA and AppB. The script will read this, but not yet sure on how can I implement it.
– UnixDummy001 Jul 17 '20 at 07:41/logsdirectory before and the desired content after running your script, so that we can understand which files should be treated in what way. – AdminBee Jul 17 '20 at 08:16lsis highly disrecommended as it will stumble on spaces or other special characters in the filenames (even though it would appear that in this case, the names are "well-behaved" in that respect). Also, why do you pipe the output oflsintofind? – AdminBee Jul 17 '20 at 08:18lsintofind, becausefinddoesn't care about its stdin. What you've written is equivalent tofind *.log4 | sed s/log4/log, which will also fail because thesedis missing a trailing/– Chris Davies Jul 17 '20 at 08:50gzip. On UNIX/Linux platformsgzipis definitely the preferred solution, but it doesn't zip as inZIP.EXEon Windows, it compresses. – Chris Davies Jul 17 '20 at 08:54shor forbash? – Chris Davies Jul 17 '20 at 08:55