每隔 3 小时,脚本将运行并备份一个文件夹“SOURCE”,并将其与当天的其他备份一起保存在文件夹“月-日-年”中,例如“03-24-13”。当新的一天到来时,它会创建一个带有新日期的新文件夹,并删除前一天文件夹中除最新备份之外的所有内容。这就是问题所在,不会删除前一天的旧文件夹。有什么想法吗?
#!/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
答案1
if [ ! -d "$DIR/$YESTERDAY" ]; then
这个执行失败。您正在测试是否存在目录。
它应该是
if [ -d "$DIR/$YESTERDAY" ]; then