使用与shell脚本中的md5sum进行比较的差异备份问题

时间:2015-08-05 06:50:54

标签: bash shell md5sum

你好,我有一个简单的问题。我正在尝试实现差异备份但我遇到问题的方法是将md5sum.txt中的哈希值与diffmd5.txt进行比较

我收到以下错误:

目前,该命令运行并且没有错误,但文件没有被替换,也没有备份任何文件

#!/bin/bash
bkdest="/home/user/backup/differential/backup_diff"
bksource="/home/user/Documents"

destgen=`find $bkdest/* -exec md5sum {} + > diffmd5.txt`
sourcegen=`find $bksource/* -exec md5sum {} + > md5sum.txt`

    $sourcegen
    $destgen

$(cat diffmd5.txt) | while read f;
do
        if [ $(grep f md5sum.txt | wc -l) -lt 1 ]
            then
                # Code to backup the file that has changed or is not on record
                cp $(cut -d ' ' -f2-- <<< $f) $bkdest   
        fi
done
                # Afterwards, update md5hashes to list newly backed up files
                $sourcegen

请帮我弄清楚我哪里出错了。谢谢!

猜测在调试模式下运行时这是我的错误

Try 'cp --help' for more information.
grep: md5sum.txt: No such file or directory
cut: invalid byte, character or field list
Try 'cut --help' for more information.
cp: missing destination file operand after ‘/home/dmitriy/backup/differential/backup_diff’

1 个答案:

答案 0 :(得分:1)

让我们尝试:

#!/bin/bash

bkdest="/home/user/backup/differential/backup_diff"
bksource="/home/user/Documents"

cd $bkdest
find . -type f -exec md5sum {} \; > /tmp/md5dest.txt

cd $bksource
find . -type f -exec md5sum {} \; | while read c f; do
    if fgrep "$c" /tmp/md5dest.txt | fgrep -q "$f"; then
       echo "$f" ignored
    else
       cp $f $bkdest
    fi
done