我是脚本新手。目前我有一个脚本,每天将目录备份到文件服务器。它会删除 14 天之外最旧的文件。我的问题是我需要它来计算实际文件并删除第 14 个最旧的文件。几天后,如果文件服务器或主机关闭了几天或更长时间,备份时它将删除几天的备份甚至全部备份。等待停机时间。我希望它总是有 14 天的备份。
我尝试四处搜索,只能找到与按日期删除相关的解决方案。就像我现在拥有的一样。
感谢您的帮助/建议!
我有我的代码,抱歉这是我第一次尝试编写脚本:
#! /bin/sh
#Check for file. If not found, the connection to the file server is down!
if
[ -f /backup/connection ];
then
echo "File Server is connected!"
#Directory to be backed up.
backup_source="/var/www/html/moin-1.9.7"
#Backup directory.
backup_destination="/backup"
#Current date to name files.
date=`date '+%m%d%y'`
#naming the file.
filename="$date.tgz"
echo "Backing up directory"
#Creating the back up of the backup_source directory and placing it into the backup_destination directory.
tar -cvpzf $backup_destination/$filename $backup_source
echo "Backup Finished!"
#Search for folders older than '+X' days and delete them.
find /backup -type f -ctime +13 -exec rm -rf {} \;
else
echo "File Server is NOT connected! Date:`date '+%m-%d-%y'` Time:`date '+%H:%M:%S'`" > /user/Desktop/error/`date '+%m-%d-%y'`
fi