Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I need help to write a script to copy log files.

There are two format of logs:

  • System_<date_timestamp>.log
  • trace_<date_timestamp>.log

New files are generated when trace log reaches 20 MB and sytem log reaches 10MB.

Only 5 of these logs (5 for each kind) are allowed to be saved, once there is more than 5 of them, the old files get deleted and replaced by new ones.

Therefore, before they get deleted I need to copy them to a different location so that I can view them later when needed to debug.

Basically, it will look like the following (showing just trace log format, similar with System log):

trace_12.03.05_17.11.20.log
trace_12.03.05_17.12.30.log
trace_12.03.05_17.13.45.log
trace_12.03.05_17.13.23.log
trace_12.03.05_17.14.40.log
share|improve this question
add comment

3 Answers

Also you can use log-rotate for the same, see following example

# Logrotate file for trace

/source/path/trace_*.log {
    missingok
    create
    compress
    rotate 1
    lastaction
        # After compressing logs, move to other location 
        Log_dir="/target/dir/old_log_$(date +%F)/$(date +%H_%S)/"
        [[ ! -d "${Log_dir}" ]] && /bin/mkdir -p "${Log_dir}"
        /bin/mv /source/path/*.gz "${Log_dir}"
    endscript
}

save above file, let say /etc/logrotate_trace.conf then simply set cron job for every hour

00 * * * * /usr/sbin/logrotate  -f /etc/logrotate_trace.conf

for testing you can run it from command line as

/usr/sbin/logrotate  -f /etc/logrotate_trace.conf
share|improve this answer
add comment

With GNU cp (as found on most non-embedded Linux distributions and a few other Unices):

cp -p --update /source/path/trace_*.log /target/dir
share|improve this answer
add comment

The most straightforward tool is rsync, which copies new files, updates changed files, and leaves existing unchanged files alone.

rsync -a /path/to/source/*.log /path/to/backup/directory/
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.