【问题标题】:Backup Yesterday Files in Folder备份文件夹中的昨天文件
【发布时间】:2014-07-24 08:11:08
【问题描述】:

我开发了以下脚本来 gzip 目录中的昨天文件,任何改进建议

Yesterday=`TZ=GMT+24 date +%d-%b-%y`;
mkdir $Yesterday
mv /tmp/logs/servicemix.log.* /tmp/logs/$Yesterday
for File in /tmp/logs/$Yesterday/app.log.*;
do gzip $File;
done

问候

【问题讨论】:

  • 如何确保 /tmp/logs/servicemix.log.* 不包含今天的文件?
  • 我会在 00:05 完成 cron 任务

标签: bash shell solaris


【解决方案1】:

1.替换

mkdir $Yesterday

通过

mkdir -p /tmp/logs/${Yesterday}
  1. gzip 你移动的文件 移动 servicemix* 文件时,不要 gzip app* 文件

【讨论】:

    【解决方案2】:

    使用以下代码行

    TIME=`date +"%b-%d-%y"` # This Command will add date in Backup File Name.
    now=$(date +"%T")
    FILENAME="filename-$TIME:$now.tar.gz"      # Here i define Backup file name format.
    SRCDIR="/home"              # Location of Important Data Directory (Source of backup).
    DESDIR="/home/user/backup"     # Destination of backup file. 
    tar -cpzf $DESDIR/$FILENAME $SRCDIR #creating backup as tar file
    echo
    echo "Backup finished"
    

    【讨论】:

      【解决方案3】:

      它们是昨天更改还是创建的?使用带有正确修饰符的find

      Yesterday=`TZ=GMT+24 date +%d-%b-%y`;
      mkdir $Yesterday
      
      # -1 is equal to last 24 hours
      # ctime is for last inode modified time. If you need creation date you need a different modifier. See attached link.
      find -ctime -1 -exec mv '{}' "$yesterday/" \;
      

      但是this is i think pretty much the best option: Use 'find' to determine files modified yesterday.

      【讨论】:

      • -ctime 是更改时间。我认为这是最好的选择。
      • @jm666 这就是我写的。更改时间...我不明白您的评论。
      • @jm666 这就是为什么我的答案中的第一行被更改或创建的原因?如果创建时间比ctime 重要,那就错了。你是对的。
      猜你喜欢
      • 2013-08-08
      • 2019-11-16
      • 2012-05-30
      • 2021-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-24
      • 1970-01-01
      相关资源
      最近更新 更多