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