Bash – Back up logs to a new directory

bashcplogsshell-script

I need 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

Best Answer

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
Related Question