简体   繁体   English

递归查找并gzip目录,而无需目录/文件测试

[英]find and gzip a directory recursively without a directory/file test

I'm working on improving our bash backup script, and would like to move away from rsync and towards using gzip and a "find since last run timestamp" system. 我正在改善bash备份脚本,并且希望从rsync转向使用gzip和“自上次运行的时间戳后查找”系统。 I would like to have a mirror of the original tree, except have each destination file gzipped. 除了要压缩每个目标文件外,我还想拥有原始树的镜像。 However, if I pass a destination path to gzip that does not exist, it complains. 但是,如果我将目标路径传递给了不存在的gzip,它会抱怨。 I created the test below, but I can't believe that this is the most efficient solution. 我在下面创建了测试,但是我不敢相信这是最有效的解决方案。 Am I going about this wrong? 我要解决这个错误吗?

Also, I'm not crazy about using while read either, but I can't get the right variable expansion with the alternatives I've tried, such as a for file in 'find' do . 另外,我也不喜欢while read使用它,但是我无法使用尝试过的替代方法(例如, for file in 'find' do使用for file in 'find' do正确的变量扩展。

Centos 6.x. Centos6.x。 Relevant snip below, simplified for focus: 下面的相关片段,为重点进行了简化:

cd /mnt/${sourceboxname}/${drive}/ && eval find . -newer timestamp | while read objresults; 
  do
    if [[ -d "${objresults}" ]]
    then
    mkdir -p /backup/${sourceboxname}/${drive}${objresults}
    else
    cat /mnt/${sourceboxname}/${drive}/"${objresults}" | gzip -fc > /backup/${sourceboxname}/${drive}"${objresults}".gz
    fi
  done
touch timestamp  #if no stderr

With proposed changes from my comments incorporated, I suggest this code: 结合我的评论中提出的建议更改,我建议以下代码:

#!/bin/bash
src="/mnt/$sourceboxname/$drive"
dst="/backup/$sourceboxname/$drive"
timestamp="$src/timestamp"
errors=$({ cd "$src" && find -newer "$timestamp" | while read objresults;
    do
        mkdir -p $(basename "$dst/$objresults")
        [[ -d "$objresults" ]] || gzip -fc < "$objresults" > "$dst/$objresults.gz"
    done; } 2>&1)
if [[ -z "$errors" ]]
then
    touch "$timestamp"
else
    echo "$errors" >&2
    exit 1
fi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM