LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Recursiveli delete in Bash Script (https://www.linuxquestions.org/questions/programming-9/recursiveli-delete-in-bash-script-618958/)

combatdoc 02-05-2008 08:55 PM

Recursiveli delete in Bash Script
 
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


raskin 02-07-2008 02:22 PM

I recommend you reading some manuals and also not letting untested code to remove anything. What you did wrong (look in the 'man find' for details) is that find is recursive. You need to specify -maxdepth 1, or it will can be able to kill one-day-old backup of a year-old directory.

combatdoc 02-08-2008 09:47 AM

Yea, I was reading man pages, etc and added a -maxdepth 0 to the line as such:

Code:

find $BASEDIR/home-backups/ -maxdepth 0 -type d -mtime +7 -exec rm -rf {} \;
that way it would erase the first level under $BASEDIR/home-backups/ which is what I wanted.

I changed the structure a little bit so that it actually does $BASEDIR/home-backups/$DATE/$LOC2 and the find should delete everything at the $DATE level older than 7 days, correct?

A maxdepth of 1 would delete everything under $DATE but leave $DATE directory intact, correct?


As to "not letting untested code to remove anything," I understand. I am running the script in test directories right now till I get everything working and I am not relying on this script for the backups. I have an older, much simpler variant that I am using to do the backup to a different location.


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