gurbinder
gurbinder

Reputation: 78

shell script not deleting files

I am using some shell script coding , here it is

DBHOSTNAME=********

DBUSERNAME=*****

DBPASSWORD=******

DBNAME=******

BACKUPFOLDER=$HOME/*******

DELETEFILES=Y

DAILYBACKUP=Y

NUMDAILYBACKUPS=2

WEEKLYBACKUP=Y

NUMWEEKLYBACKUPS=2

MONTHLYBACKUP=Y

NUMMONTHLYBACKUPS=2

TODATE=$(date +%d)
TOMORROW=`date +%d -d "1 day"`
TODAY=$(date +%a)
MONTH=$(date +%B)
WEEK=$(date +%U)

if [ $TODATE -gt $TOMORROW ] && [ "$MONTHLYBACKUP" == "Y" ]
then
    /usr/bin/mysqldump -h $DBHOSTNAME -u $DBUSERNAME -p$DBPASSWORD $DBNAME  | gzip > $BACKUPFOLDER/$DBNAME'_'`date '+%m-%d-%Y'`'_'$MONTH.sql.gz
else
    if [ "$TODAY" == "Sat" ] && [ "$WEEKLYBACKUP" == "Y" ]
    then
        /usr/bin/mysqldump -h $DBHOSTNAME -u $DBUSERNAME -p$DBPASSWORD $DBNAME  | gzip > $BACKUPFOLDER/$DBNAME'_'`date '+%m-%d-%Y'`'_'Week$WEEK.sql.gz
    else 
        if [ "$DAILYBACKUP" == "Y" ] 
        then
            /usr/bin/mysqldump -h $DBHOSTNAME -u $DBUSERNAME -p$DBPASSWORD $DBNAME  | gzip > $BACKUPFOLDER/$DBNAME'_'`date '+%m-%d-%Y'`'_'$TODAY.sql.gz
        fi
    fi
fi




if [ $DELETEFILES == Y ]
then
    NUMWEEKLY=$[$NUMWEEKLYBACKUPS*7]
    NUMMONTHLY=$[$NUMMONTHLYBACKUPS*31]
    find $BACKUPFOLDER/*Sun.sql.gz -type f -mtime +$NUMDAILYBACKUPS -delete 2> /dev/null
    find $BACKUPFOLDER/*Mon.sql.gz -type f -mtime +$NUMDAILYBACKUPS -delete 2> /dev/null
    find $BACKUPFOLDER/*Tue.sql.gz -type f -mtime +$NUMDAILYBACKUPS -delete 2> /dev/null
    find $BACKUPFOLDER/*Wed.sql.gz -type f -mtime +$NUMDAILYBACKUPS -delete 2> /dev/null
    find $BACKUPFOLDER/*Thu.sql.gz -type f -mtime +$NUMDAILYBACKUPS -delete 2> /dev/null
    find $BACKUPFOLDER/*Fri.sql.gz -type f -mtime +$NUMDAILYBACKUPS -delete 2> /dev/null
    find $BACKUPFOLDER/*Sat.sql.gz -type f -mtime +$NUMDAILYBACKUPS -delete 2> /dev/null
    find $BACKUPFOLDER/*Week*.sql.gz -type f -mtime +$NUMWEEKLY -delete 2> /dev/null
    find $BACKUPFOLDER/*January.sql.gz -type f -mtime +$NUMMONTHLY -delete 2> /dev/null
    find $BACKUPFOLDER/*February.sql.gz -type f -mtime +$NUMMONTHLY -delete 2> /dev/null
    find $BACKUPFOLDER/*March.sql.gz -type f -mtime +$NUMMONTHLY -delete 2> /dev/null
    find $BACKUPFOLDER/*April.sql.gz -type f -mtime +$NUMMONTHLY -delete 2> /dev/null
    find $BACKUPFOLDER/*May.sql.gz -type f -mtime +$NUMMONTHLY -delete 2> /dev/null
    find $BACKUPFOLDER/*June.sql.gz -type f -mtime +$NUMMONTHLY -delete 2> /dev/null
    find $BACKUPFOLDER/*July.sql.gz -type f -mtime +$NUMMONTHLY -delete 2> /dev/null
    find $BACKUPFOLDER/*August.sql.gz -type f -mtime +$NUMMONTHLY -delete 2> /dev/null
    find $BACKUPFOLDER/*September.sql.gz -type f -mtime +$NUMMONTHLY -delete 2> /dev/null
    find $BACKUPFOLDER/*October.sql.gz -type f -mtime +$NUMMONTHLY -delete 2> /dev/null
    find $BACKUPFOLDER/*November.sql.gz -type f -mtime +$NUMMONTHLY -delete 2> /dev/null
    find $BACKUPFOLDER/*December.sql.gz -type f -mtime +$NUMMONTHLY -delete 2> /dev/null
fi

it is working fine, but doesnt delete old files, i taken this from following address http://www.htpcbeginner.com/automatic-mysql-database-backup-on-godaddy/3/

what i need is backup of 3 days like

Today files in folder 03/01/2017 04/01/2017 05/01/2017

and tommorow it should be 04/01/2017 05/01/2017 06/01/2017

it should delete 3jan file

Upvotes: 0

Views: 134

Answers (1)

cxw
cxw

Reputation: 17041

Besides what the commenters have said, I think a key issue is your find command. You have, for one example:

find $BACKUPFOLDER/*Sun.sql.gz -type f -mtime +$NUMDAILYBACKUPS -delete 2> /dev/null

But that should be

find "$BACKUPFOLDER" -name '*Sun.sql.gz' -type f -mtime "+$NUMDAILYBACKUPS" -delete 

When using find, you list the paths ("$BACKUPFOLDER") and the filenames (-name '*Sun.sql.gz') separately. You also single-quote the filenames to prevent the shell from expanding them.

While debugging, don't use 2> /dev/null. That discards error messages you might otherwise find helpful in solving your problem :) .

Upvotes: 1

Related Questions