清理备份,保留最近5个备份文件

1、用法:

backup_path=${backup_path}      #设置备份文件存放的目录。
 
cd ${backup_path}
 
count=$(ls -l *.*ar|wc -l)      #统计备份目录文件总数
 
if [ $count -gt 5 ]             #判断文件总数是否超过5
then
    n=$(expr $count - 5)        #取需要删除的文件数量+1
    echo "There have $(expr $n - 1 ) files need to be deleted!"
    for file in $(ls -lrt *.*ar|head -$n|awk '{print $9}')
        do
            cd $backup_path
            rm -f $file
            echo "Delete $file successfully!"
        done
else
    echo "No backup files need to be deleted."
fi

2、示例:

backup_path=/home/itadmin/deployment/aeonlife-${service_name}/backup
 
 
count=$(ssh itadmin@${service_ip} "ls -l ${backup_path}/*.*ar|wc -l")
 
if [ $count -gt 5 ];then
    n=$(expr $count - 5)
    echo "There have $(expr $n - 1 ) files need to be deleted!"
    for file in $(ssh itadmin@${service_ip} "ls -rt ${backup_path}/*.*ar|head -$n|awk '{print $9}'")
        do
            ssh itadmin@${service_ip} "rm -f $file"
            echo "Delete $file successfully!"
        done
else
    echo "No backup files need to be deleted."
fi

你可能感兴趣的:(linux)