我是新手脚本编写者。目前我有一个脚本,每天备份一个目录到一个文件服务器。它会在14天之外删除最旧的文件。我的问题是我需要它来计算实际的文件并删除第14个最旧的文件。如果文件服务器或主机关闭几天或更长时间,备份时会删除几天的备份甚至全部备份。等待停机时间。我希望它总是有14天的备份价值。Shell脚本:计数文件,删除'X'最旧的文件
我试着搜索周围,只能找到与按日期删除相关的解决方案。就像我现在所拥有的一样。
谢谢你的帮助/建议!
我的代码我有,对不起,在脚本的我第一次尝试:
#! /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
你可能会对[rsnapshot](http://www.rsnapshot.org/)感兴趣。它使用rsync,比你的解决方案更高效。配置文件有一个参数'retain',如果你设置了'retain 14',它将始终保留最后14个备份。 – Marco