shell脚本不删除文件

时间:2017-01-05 13:08:32

标签: bash shell

我正在使用一些shell脚本编码,这里是

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

它工作正常,但没有删除旧文件,我从以下地址http://www.htpcbeginner.com/automatic-mysql-database-backup-on-godaddy/3/

获取此信息

我需要的是3天的备份 喜欢

今天文件夹中的文件 2017年3月1日 2017年4月1日 2017年5月1日

和tommorow它应该是 2017年4月1日 2017年5月1日 2017年6月1日

它应该删除3jan文件

1 个答案:

答案 0 :(得分:1)

除了评论者所说的内容之外,我认为一个关键问题是你的find命令。举个例子,你有:

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

但那应该是

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

使用find时,会分别列出路径("$BACKUPFOLDER")和文件名(-name '*Sun.sql.gz')。您还单引号文件名以防止shell扩展它们。

调试时,请勿使用2> /dev/null。这会丢弃您在解决问题时可能会有帮助的错误消息:)。