LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Back up script (find, cp, md5sum, rm) (https://www.linuxquestions.org/questions/linux-newbie-8/back-up-script-find-cp-md5sum-rm-839758/)

Kuro 10-22-2010 07:57 AM

Back up script (find, cp, md5sum, rm)
 
Hey all
I'm just trying to make a simple back up script.

I want to move all files and directories that are 1 month old out to back up into a separate folder.

There will be a lot of files and I want to make sure it copies properly.
The problem I'm having is integrating a MD5SUM into it to check integrity. MD5SUM is not recursive, so I figured it would work in a loop when it copies each individual file, I'll do a md5sum on each file and delete that md5 once its verified it copied ok.

I'm pretty bad with the syntax, but I think this is what I want to do.
Code:

#!/bin/sh
for i in find ~/Downloads -mtime +31 | xargs -0 md5sum > md5file.md5
do

cp $i ~/backup
echo "Checking $i"
md5sum -c $i md5file.md5
#on successful hash check, rm $i

done

I think I also need some sort of error handling to output all md5's that didnt pass the hash check.

Inputs?

Thanks.

H_TeXMeX_H 10-22-2010 08:07 AM

I call this script genmd5sum, it makes an md5sum of all files down from the current directory + subdirectories:

Code:

#!/bin/sh
# generates md5sum.md5 for all files in current directory and subdirectories

# clean up current dir
if test -f md5sum.md5
then
  rm -f md5sum.md5
fi

# clean up /tmp
if test -f /tmp/md5sum.md5
then
  rm -f /tmp/md5sum.md5
fi

# find all files, pipe to md5sum, output to /tmp, the -print0 and -0 avoid nonsense caused by whitespace
find -type f -print0 | xargs -0 md5sum > /tmp/md5sum.md5

# move to current dir
mv /tmp/md5sum.md5 .

# add the date this was made, for convenience
date >> md5sum.md5

# verify it now, just to make sure it was made properly
md5sum -c md5sum.md5

# success
exit 0

It can be made to work more efficiently by removing some commands that you don't need.

goldenbarb 10-22-2010 11:09 AM

Why don't you use md5deep -r?

Dark_Helmet 10-22-2010 01:08 PM

Quote:

Originally Posted by Kuro
Code:

#!/bin/sh
for i in find ~/Downloads -mtime +31 | xargs -0 md5sum > md5file.md5
do


I don't think that code snippet will do what you're hoping it will. Granted, you said syntax is not your thing, so...

I don't think your find command will execute, and secondly, even if it did, you redirect all output into either a pipe or a file. In doing so, there will never be anything the for loop will store in $i.

My suggestion:
Write a script that handles one file as an argument from the command line--which you almost have already inside the body of the for loop. Save the script, make it executable, and then issue a command like so:
Code:

$ find ~/Downloads -mtime +31 -exec yourscriptname {} \;


All times are GMT -5. The time now is 12:11 AM.