mr.bear mr.bear - 12 days ago 5
Linux Question

Different timestamps when backup files using cron

I need some help to explain cron for backup files.

I have a shell script to backup logfile running in RHEL 6.7 and Solaris 10. It'll move the logfile to backup directory and gunzip each logfile.

Here the script.

#!/bin/bash

# Defined variable
dirLog=/app/rbt3/prod/cda/logs
dirBackup=/app/rbt3/prod/cda/logs/backup

# Change directory to CDA logfile
cd $dirLog

# Backup mechanism
for file in `ls *.log.*` ; do
#echo "FileSemua -> $file"
echo " Pindahkan file $file ke directory $dirBackup "
/bin/mv $dirLog/$file $dirBackup
echo " start Gzip file [$file]..... "
/bin/gzip $dirBackup/$file
echo " done Gzip file [$file]..... "
done


The script is registered in crontab to run every day at 1:20 AM.

20 1 * * * /app/prod/logs/backupLog.sh


Here the backup files that cron created.

-rw-r--r-- 1 user3 user 36344 Nov 18 11:59 alarm.log.20161117.gz
-rw-r--r-- 1 user3 user 35085 Nov 19 11:59 rsync.log.20161117.gz
-rw-r--r-- 1 user3 user 35018 Nov 20 11:59 trace.log.20161117.gz


As far as I know, when we register the script in cron for a specific time. It'll be running and create the files exactly as the cron said (Please correct me if I'm wrong). But in my case, the time for backup files created is different from the cron. Did I miss something?

Thanks.

Answer

When you move file time stamps not change. and you use gzip for compressing which use file time stamps.thats why you see different time stamps

$ ls -l
-rw-r--r-- 1 user3 user   36344 Nov 18 11:59 alarm.log.20161117.gz
-rw-r--r-- 1 user3 user   35085 Nov 19 11:59 rsync.log.20161117.gz
-rw-r--r-- 1 user3 user   35018 Nov 20 11:59 trace.log.20161117.gz

ls -l shows the last update time of your log file

Comments