Hi,
A bit of a background on the script I am trying to make first of all.
I have a mysql database with some users in, I am taking a dump of the user table into a file, then I am using that file as the input to grep against the ftp log. I need to see the last 5 entries from the ftp log based on the user. For example:
UserA:
x.x.x9.x - usera.x.co.uk [31/May/2009:11:50:00 +0100] "PUT /home/uploads/usera/x.txt" 200 77
etc
UserB:
x.x.x9.x - userb.x.co.uk [31/May/2009:11:50:00 +0100] "PUT /home/uploads/userb/x.txt" 200 77
x.x.x9.x - userb.x.co.uk [31/May/2009:11:50:00 +0100] "PUT /home/uploads/userb/x.txt" 200 77
x.x.x9.x - userb.x.co.uk [31/May/2009:11:50:00 +0100] "PUT /home/uploads/userb/x.txt" 200 77
x.x.x9.x - userb.x.co.uk [31/May/2009:11:50:00 +0100] "PUT /home/uploads/userb/x.txt" 200 77
I also need to (somehow) run a query on the ftp log to find if someone hasnt uploaded data in the past 2 weeks. I also on top of this need to count how many times the username appears in the logs. The username is part of the /home/uploads/ also. The logfile rotation will be changed to rotate every 2 weeks with this script being ran about 12 hours before the log roates, but it will be ran on a weekly basis. I then require all the data to be put into one easy to read log for the managing directors to look at. Needless to say I have been thrown in the deep end and have never really used bash to this extent before.
So far I have this, which is very messy but gets half the job done, I seem to have hit a wall (programmers block?) and cannot seem to find my way around it, any nudges in the correct direction would be very very useful.
Code:
#!/usr/bin/env bash
set -e
FTPL='pureftpd.log'
UFILE='users.txt'
TIMESTAMP=$(date +%Y%m%d)
echo "Checking to see if users file exists..."
# if to see if the users file exists. If it does move to to a backup file and name it after the current timestamp
if [ -f users.txt ]
then
echo "Yes it does, moving to backup file"
mv users.txt users\_$TIMESTAMP.txt
else
echo "no file"
fi
# here we connect to the mySQL database and pull a list of the current users down and put them into a text file.
echo " connecting to mysql to get users"
sleep 5
# I know I shouldnt be using the PW here but I have been told to :/
mysql -h db.xxx.xxx.xx -u xxxxxx --password=xxxxxxx -e "use FTP; SELECT SUBSTRING_INDEX(user , '.', 1) AS username from users;" > users.txt
echo "done!"
echo "now what we need to do is read the user file, then grep the ftp log against that."
sleep 5
while read line;
do
echo "Now trying to read the user list"
echo "${line}";
echo "reading the file.."
echo "grepping for the users"
grep "$line" pureftpd.log | tail -n 5 >> main.log
done < <(cat $UFILE)
sleep 5
echo "now we are going to check for people who have NOT uploaded"
while read line;
if [ -f notuploaded.txt ]
then
echo "Yes it does, moving to backup file"
mv notuploaded.txt noupload\_$TIMESTAMP.txt
else
echo "no sign of it, creating new file"
fi
do
echo "$line";
grep -v "$line" pureftpd.log > notuploaded.log
done < <(cat $UFILE)
sleep 5
Any slight nudges or suggestions on what to do would earn my eternal gratitude!
Thank you!