openSUSE Forums > Archives > SF Archives > ARCHIVES - Programming & Scripting » Need Help Recursively Delete In Bash

Go Back   openSUSE Forums > Archives > SF Archives > ARCHIVES - Programming & Scripting
Forums FAQ Members List Search Today's Posts Mark Forums Read


ARCHIVES - Programming & Scripting A place to discuss website design, programming, shell scripts, etc

 
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-Feb-2008, 20:29
Combat Doc
Guest
 
Posts: n/a
Default

I have created a little BASH script to do some auto backing up (well really two scripts - one interactive and one automatic for crontabs) my $HOME and /etc directories to an external drive.

The script creates a new folder everday for each backup naming it homedir-date or etc-date respectively. That way I have multiple backups to refer to in case of an accident.

What I am looking to do is add some code that will check for directories TODAY - xdays and rm -rf any directories older than that output. I am not really familiar with BASH scripting and I am pretty sure this qualifies as a noob question, but can anyone help me out with code for that?

Would adding
Code:
find $BASEDIR/home-backups/ -type d -mtime +7 -exec rm -rf {} \;
at the appropriate point work?

Just so you can see, I am providing the interactive version of the script below. The only real difference in the two scripts is removal of the read -p's and replacing them with echos that are logged instead.

Thanks for any help.

Code:
BASEDIR=/media/brain
LOC2=homedir-
DATE=$(date +%G-%m-%d)

clear
#TEST for presence of drive/dir
if test -d "$BASEDIR"; then
****#1 If $LOC2$DATE exists delete it
******if test -d "$BASEDIR/home-backups/$LOC2$DATE"; then
******** read -p "Delete Old Home Backup for $DATE - $LOC2$DATE? <y/n> "
******** 
******** if [ "$REPLY" == "y" ]; then
************rm -rf $BASEDIR/home-backups/$LOC2$DATE
************read -p "Old Home Backup for $DATE Deleted! Continue Backup? <y/n> "** 
**************** if [ "$REPLY" == "n" ]; then
******************** echo "Backup Aborted.**Exiting..."
******************** exit 1
**************** fi

******** else
************ echo "Directory Kept Intact. Exiting..."
************ exit
**********fi**
****** fi** 

************************echo "Creating Backup Home Directory for $DATE"
**************** # VERBOSE MODE cp -PRuv $HOME /media/brain
**************** #cp -a same as cp -dpPR (preserve attrib, no follow symlinks, recursive)
**************** cp -a $HOME $BASEDIR/home-backups/$LOC2$DATE
**************** echo "Done Backing Up Home Directory to $BASEDIR/$LOC2$DATE!!"
******************exit
else
******echo "Directory $BASEDIR not present.**Is the Drive Plugged in and mounted?" 
******exit
fi
  #2 (permalink)  
Old 08-Feb-2008, 09:58
finer recliner
Guest
 
Posts: n/a
Default

bash supports many file comparisons including when a file was last modified and if one file is newer than another file. below is a list of many (if not all) of the conditional statements you can use with files. you should find what you want in there

http://www.faqs.org/docs/bashman/bashref_68.html#SEC75
 

Bookmarks

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




 

Search Engine Friendly URLs by vBSEO 3.3.0 RC2