Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 10-22-2010, 08:57 AM   #1
LQ Newbie
Registered: Dec 2009
Posts: 5

Rep: Reputation: 0
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.
for i in find ~/Downloads -mtime +31 | xargs -0 md5sum > md5file.md5

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

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


Old 10-22-2010, 09:07 AM   #2
LQ Guru
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

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

# generates md5sum.md5 for all files in current directory and subdirectories

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

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

# 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.
Old 10-22-2010, 12:09 PM   #3
Registered: Aug 2010
Distribution: Fedora, Centos, Debian
Posts: 49

Rep: Reputation: 7
Why don't you use md5deep -r?
Old 10-22-2010, 02:08 PM   #4
Senior Member
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
Originally Posted by Kuro
for i in find ~/Downloads -mtime +31 | xargs -0 md5sum > md5file.md5
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:
$ find ~/Downloads -mtime +31 -exec yourscriptname {} \;


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't find MD5SUM for Mandriva 2010 KDE4 586 Tony Littlejohn Mandriva 4 11-06-2009 02:14 AM
Help Please!! -- need to find command that will switch you back to your previous CWD Tron55555 Linux - General 2 05-15-2008 11:35 PM
how to pass a variable from a called script back to the calling script steven.c.banks Linux - General 2 05-05-2008 03:00 PM
md5sum script Axion Linux - Software 4 02-14-2004 08:56 PM
how to find the pid of a perl script from shell script toovato Linux - General 1 12-19-2003 07:25 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:20 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration