iamsaicharanburle
New Member
Hi Team,
I prepared one shell script to find a file and move and zip and archive. But while i execute the script it is throwing error for the files dated with 2019 as no such file or directory file name will be something like server.log.2019-07-10* : No Such file or Directory. However it is moving zipping into the archival for the file names server.log.2020*.Below is my Script
#!/bin/bash
############################
# LOGS HOUSEKEEPING SCRIPT
# CREATED BY: Saicharan Burle
# LAST MODIFIED BY: Saicharan Burle : Added Archiving
############################
TIME=`date +%m%d%Y%H%M`
SCRIPTHOME="/home/ec2-user/scripts"
SCRIPT_LOCK=${SCRIPTHOME}/$0.lock
##### Directories to search (also include in the for loop below) ######
LOC1="/home/ec2-user/scripts/log"
##### Verify if script is already running #####
if [ -s ${SCRIPT_LOCK} ]
then
echo -e "Script is already running... Aborting!"
exit 1
fi
##### Create Lock file for the Script #####
echo $$ > ${SCRIPT_LOCK}
##### Archive logs older than 1 days, gzip older than 1 days and delete them if older than 365 days ######
for LOC in $LOC1
do
ls $LOC/archive > /dev/null
if [ "$?" != "0" ]
then
mkdir $LOC/archive
else
echo "archive directory is already created to move logs files older than 1 day"
fi
cd $LOC
if [ "$?" == "0" ]; then
ls -F .|grep -v /| xargs -I {} find ./{} -mtime +1 -exec mv {} archive \;
fi
cd $LOC/archive
if [ "$?" == "0" ]; then
ls -F .|grep -v /| xargs -I {} find ./{} -mtime +1 -exec gzip {} \;
ls -F .|grep -v /| xargs -I {} find ./{} -mtime +365 -exec rm {} \;
fi
done
##################### Realease Lock file for the script ####################################
cd $SCRIPTHOME
rm logrotate.sh.lock
Please correct me if i need to change in the above script
I prepared one shell script to find a file and move and zip and archive. But while i execute the script it is throwing error for the files dated with 2019 as no such file or directory file name will be something like server.log.2019-07-10* : No Such file or Directory. However it is moving zipping into the archival for the file names server.log.2020*.Below is my Script
#!/bin/bash
############################
# LOGS HOUSEKEEPING SCRIPT
# CREATED BY: Saicharan Burle
# LAST MODIFIED BY: Saicharan Burle : Added Archiving
############################
TIME=`date +%m%d%Y%H%M`
SCRIPTHOME="/home/ec2-user/scripts"
SCRIPT_LOCK=${SCRIPTHOME}/$0.lock
##### Directories to search (also include in the for loop below) ######
LOC1="/home/ec2-user/scripts/log"
##### Verify if script is already running #####
if [ -s ${SCRIPT_LOCK} ]
then
echo -e "Script is already running... Aborting!"
exit 1
fi
##### Create Lock file for the Script #####
echo $$ > ${SCRIPT_LOCK}
##### Archive logs older than 1 days, gzip older than 1 days and delete them if older than 365 days ######
for LOC in $LOC1
do
ls $LOC/archive > /dev/null
if [ "$?" != "0" ]
then
mkdir $LOC/archive
else
echo "archive directory is already created to move logs files older than 1 day"
fi
cd $LOC
if [ "$?" == "0" ]; then
ls -F .|grep -v /| xargs -I {} find ./{} -mtime +1 -exec mv {} archive \;
fi
cd $LOC/archive
if [ "$?" == "0" ]; then
ls -F .|grep -v /| xargs -I {} find ./{} -mtime +1 -exec gzip {} \;
ls -F .|grep -v /| xargs -I {} find ./{} -mtime +365 -exec rm {} \;
fi
done
##################### Realease Lock file for the script ####################################
cd $SCRIPTHOME
rm logrotate.sh.lock
Please correct me if i need to change in the above script