Every 3 hours the script will run and backup a folder "SOURCE" and it will save it along with the other backups of the current day in the folder "month-day-year" e.g. "03-24-13". When a new day comes, it creates a new folder with the new date and deletes all but the latest backup in the previous day's folder. Here is where the problem is, will not delete the old folders from the previous day. Any ideas why?
#!/bin/sh
DIR=/media/HDD
SOURCE=/home/eric/Creative/
DATE=$(date +"%m-%d-%Y")
YESTERDAY=$(date -d '1 day ago' +"%m-%d-%Y")
TIME=$(date +"%T")
DESTINATION=$DIR/$DATE/$TIME
SPACE=$(df -P $DIR | tail -1 | awk '{print $4}')
NEEDED=$(du -s $SOURCE | awk '{print $1}')
FOLDERS=$(find $DIR/* -maxdepth 0 -type d | wc -l)
# If there is not enough space, delete the oldest folder
if [ $NEEDED -ge $SPACE ]; then
ls -dt $DIR/* | tail -n +$FOLDERS | xargs rm -rf
fi
# If there is not a folder for today, create one
if [ ! -d "$DIR/$DATE" ]; then
mkdir $DIR/$DATE
# If there is a folder from yesterday, keep only one of its backups
if [ ! -d "$DIR/$YESTERDAY" ]; then
ls -dt $DIR/$YESTERDAY/* | tail -n +2 | xargs rm -rf
fi
fi
# Create the new backup directory
if [ ! -d "$DESTINATION" ]; then
mkdir $DESTINATION
fi
# Backup source to destination
rsync -a $SOURCE $DESTINATION
This one fails to execute. You are testing if there is NOT a directory.
It should be