Shell script Help

They have: 5,633 posts

Joined: Jan 1970

I have tried searching and can't find anything that is on this topic. My provider deletes all logs after one day to conserve space. Here is what I am trying to do:

Make a shell or PHP script that will:

1) Copy a file from one directory to another. It is a log file, so a shell script would work better.

2) Rename the file to include todays date. This is essential because I need to no overwrite it the next day.

3) gzip the file to save space.

I want to save stats for atleast a week so I can go on vacation and still access the raw logs.

Chuck

Any help would be appreciated. If you can just point me to the right reference, that would even help.

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

I just wrote this up. I tested it on my system (paths still in the code) and it works fine as long as you only do one file at a time (you can't use *).

usage:
filename [source file [destination directory]]

#!/bin/sh

if [ $# -gt 0 ]; then
  source_file=$1
else
  source_file=/home/maxalbert.com/logs/access_log
fi

if [ $# -gt 1 ]; then
  dest_dir=$2
else
  dest_dir=/home/maxalbert.com/test
fi

day_stamp=`date +%Y_%m_%d`
filename=`echo $source_file | awk -F/ '{print $NF}'`

dest_dir="$dest_dir/$day_stamp/"

if [ ! -d $dest_dir ]; then
  mkdir -p $dest_dir
fi

cp $source_file "$dest_dir/$filename"

exit 0;
'

Mark Hensler
If there is no answer on Google, then there is no question.

They have: 37 posts

Joined: Nov 2002

Make sure your provide allows cron jobs so that you can schedule the task.

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

BTW, the way the script is configured above, it will copy the file(access_log)
FROM: /home/maxalbert.com/logs/access_log
TO: /home/maxalbert.com/test/YYYY_MM_DD/access_log

You can pass the FROM and TO as arguements to the shell script. And/Or configure the script to use a certain file and path for default values.

CRON is probably the best way to do it (as FunkyJ mentioned). Try to run it just before they prun the file.

Mark Hensler
If there is no answer on Google, then there is no question.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.