【问题标题】:Different timestamps when backup files using cron使用 cron 备份文件时的不同时间戳
【发布时间】:2016-11-21 03:38:27
【问题描述】:

我需要一些帮助来解释备份文件的 cron。

我有一个 shell 脚本来备份在 RHEL 6.7 和 Solaris 10 中运行的日志文件。它会将日志文件移动到备份目录并压缩每个日志文件。

这里是脚本。

#!/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

脚本在 crontab 中注册,每天在 1:20 AM 运行。

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

这里是 cron 创建的备份文件。

-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

据我所知,当我们在 cron 中注册脚本特定时间时。它将完全按照 cron 所说的那样运行并创建文件(如果我错了,请纠正我)。但就我而言,创建备份文件的时间与 cron 不同。我错过了什么吗?

谢谢。

【问题讨论】:

  • 您是否有不能使用 logrotate 或类似方法来轮换日志的原因?
  • @BenWhaley 我不介意使用其他命令或机制。你能给我简单解释一下使用 logrotate 吗?
  • 您使用的是 Linux 还是 Solaris?您的问题被标记为两者。
  • 但是如果你不介意的话,我还是想知道我的问题的解决方案。
  • @BenWhaley 两者。有几个使用 Linux 和 Solaris 的服务器。

标签: linux shell cron solaris


【解决方案1】:

当您移动文件时,时间戳不会改变。并且您使用 gzip 来压缩使用文件时间戳的文件。这就是为什么您会看到不同的时间戳

$ 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 显示你的日志文件的最后更新时间

【讨论】:

  • 啊,我想这就是我的问题的答案。谢谢你给我解释。
猜你喜欢
  • 2013-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-10
相关资源
最近更新 更多