2

Hello I have a quick question. I am trying to implement a differential backup but where I am having trouble is comparing the hash from md5sum.txt to diffmd5.txt

I am getting the following error:

Currently the command runs and gives no errors but files are not being replaced and not backing up any files

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

Please help me out on figuring out where I went wrong. Thank you!

Guess this is my error when running in debug mode

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’
Dmitriy
  • 97
  • 3
  • 15
  • Second call to $sourcegen should be outside main loop? – pasaba por aqui Aug 05 '15 at 07:19
  • md5hashes.txt is not the result of any "find" command. Moreover, verify you are not using diffmd5 instead of md5sum and viceversa. – pasaba por aqui Aug 05 '15 at 07:20
  • @pasaba por aqua I fixed and used while instead as stated in the first comment. The diffmd5.txt is the original database of the original backup to which we're comparing and the md5sum.txt is the database from the files we are going to backup. I just saw my issue that in the grep I have md5hashes.txt. – Dmitriy Aug 05 '15 at 07:29
  • @Dimitry: recall that "f" is from "diffmd5.txt", that is, from directory "bkdest". You can not use it as first argument of "cp", but second. – pasaba por aqui Aug 05 '15 at 07:32
  • md5 files contains the md5 value and the name of the file. For this reason, you have the error "cp: cannot stat ‘9000a3c48f47c26b6c2dcb23d8281cf9’: No such file or directory". In copy statment, use: cp $(cut -d ' ' -f2-- <<< $f) $bkdest – pasaba por aqui Aug 05 '15 at 07:33
  • @pasaba por aqua I edited the code with the update, I'll try running it again to see how it goes and update it here. Thank you! – Dmitriy Aug 05 '15 at 07:36
  • Currently, I am getting this error when I tried the code with the latest edit "./bkdiff: line 11: 1d3935adac5b184a388826a4c718601c: command not found" – Dmitriy Aug 05 '15 at 07:47
  • @Dmitriy: That's because `$(cat diffmd5.txt)` expands to the contents of `diffmd5.txt`. Those contents are not a suitable command. – ruakh Aug 05 '15 at 08:04
  • @ruakh currently I am not getting any errors but my changed files don't seem to be copying over and or overwriting on top of the original. Thanks! – Dmitriy Aug 05 '15 at 08:11
  • Replace: a) "$(cat diffmd5.txt)" with "cat diffmd5.txt" b) "find $bkdest/* ..." includes full path in the file, we do not want that, use "cd $bkdst; find . ..." instead; c) remove second call to "$sourcegen" – pasaba por aqui Aug 05 '15 at 08:13
  • Seems like something is with the cp. Getting the following "cp: missing destination file operand after ‘/home/user/backup/differential/backup_diff’ Try 'cp --help' for more information." weird that my destination is correct. My cp looks like this: "cp $(cut -d ' ' -f2-- <<< $f) $bkdest" – Dmitriy Aug 05 '15 at 08:23

1 Answers1

1

Lets try with:

#!/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
pasaba por aqui
  • 3,446
  • 16
  • 40
  • I'm getting few errors with this script. Not sure where the problem relies on: "cp: cannot stat ‘test2’: No such file or directory cut: d41d8cd98f00b204e9800998ecf8427e: No such file or directory cp: missing destination file operand after ‘/home/dmitriy/backup/differential/backup_diff’" – Dmitriy Aug 05 '15 at 07:53
  • wow your is working perfectly! Is there any way to figure out what is the issue with mine? Don't want to plagiarism for the project. Then I'll revise it and make it better. Trying to figure out where my problem relies. Currently Its running with no errors but nothing is being overwritten. Might be something with the copy function. Thank you for your help! – Dmitriy Aug 05 '15 at 08:08
  • @Dimitry: execute with "bash -x" to debug, and see content of temporary files to verify its is the expected one. – pasaba por aqui Aug 05 '15 at 08:15