LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 10-22-2010, 07:57 AM   #1
Kuro
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.
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.
 
Old 10-22-2010, 08:07 AM   #2
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
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.
 
Old 10-22-2010, 11:09 AM   #3
goldenbarb
Member
 
Registered: Aug 2010
Distribution: Fedora, Centos, Debian
Posts: 49

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

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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 {} \;
 
  


Reply



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 01:14 AM
Help Please!! -- need to find command that will switch you back to your previous CWD Tron55555 Linux - General 2 05-15-2008 10: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 02:00 PM
md5sum script Axion Linux - Software 4 02-14-2004 07:56 PM
how to find the pid of a perl script from shell script toovato Linux - General 1 12-19-2003 06:25 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:21 AM.

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