博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
保留最新N份备份目录脚本
阅读量:6075 次
发布时间:2019-06-20

本文共 2733 字,大约阅读时间需要 9 分钟。

 

如下所示,在/opt/backup下是备份目录,只需要保留最新的三份备份,在此之前的备份目录都要删除。

[root@syslog-ng ~]# cd /opt/backup/[root@syslog-ng backup]# ls -ltotal 68drwxr-xr-x. 2 root root 4096 Jul 11 14:48 20180626drwxr-xr-x. 2 root root 4096 Jul 11 14:48 20180627drwxr-xr-x. 2 root root 4096 Jul 11 14:48 20180628drwxr-xr-x. 2 root root 4096 Jul 11 14:48 20180629drwxr-xr-x. 2 root root 4096 Jul 11 14:48 20180630drwxr-xr-x. 2 root root 4096 Jul 11 14:48 20180631drwxr-xr-x. 2 root root 4096 Jul 11 14:48 20180701drwxr-xr-x. 2 root root 4096 Jul 11 14:48 20180702drwxr-xr-x. 2 root root 4096 Jul 11 14:48 20180703drwxr-xr-x. 2 root root 4096 Jul 11 14:48 20180704drwxr-xr-x. 2 root root 4096 Jul 11 14:48 20180705drwxr-xr-x. 2 root root 4096 Jul 11 14:48 20180706drwxr-xr-x. 2 root root 4096 Jul 11 14:48 20180707drwxr-xr-x. 2 root root 4096 Jul 11 14:48 20180708drwxr-xr-x. 2 root root 4096 Jul 11 14:48 20180709drwxr-xr-x. 2 root root 4096 Jul 11 14:48 20180710drwxr-xr-x. 2 root root 4096 Jul 11 14:48 20180711[root@syslog-ng backup]# /bin/ls -l |grep "^d"drwxr-xr-x. 2 root root 4096 Jul 11 14:48 20180626drwxr-xr-x. 2 root root 4096 Jul 11 14:48 20180627drwxr-xr-x. 2 root root 4096 Jul 11 14:48 20180628drwxr-xr-x. 2 root root 4096 Jul 11 14:48 20180629drwxr-xr-x. 2 root root 4096 Jul 11 14:48 20180630drwxr-xr-x. 2 root root 4096 Jul 11 14:48 20180631drwxr-xr-x. 2 root root 4096 Jul 11 14:48 20180701drwxr-xr-x. 2 root root 4096 Jul 11 14:48 20180702drwxr-xr-x. 2 root root 4096 Jul 11 14:48 20180703drwxr-xr-x. 2 root root 4096 Jul 11 14:48 20180704drwxr-xr-x. 2 root root 4096 Jul 11 14:48 20180705drwxr-xr-x. 2 root root 4096 Jul 11 14:48 20180706drwxr-xr-x. 2 root root 4096 Jul 11 14:48 20180707drwxr-xr-x. 2 root root 4096 Jul 11 14:48 20180708drwxr-xr-x. 2 root root 4096 Jul 11 14:48 20180709drwxr-xr-x. 2 root root 4096 Jul 11 14:48 20180710drwxr-xr-x. 2 root root 4096 Jul 11 14:48 20180711[root@syslog-ng backup]# /bin/ls -l |grep "^d"|awk '{print $9}'2018062620180627201806282018062920180630201806312018070120180702201807032018070420180705201807062018070720180708201807092018071020180711[root@syslog-ng backup]# /bin/ls -l |grep "^d"|awk '{print $9}'|wc -l17[root@syslog-ng backup]# /usr/bin/expr 17 - 3 14

处理脚本如下

[root@syslog-ng backup]# cat /opt/del.sh#!/bin/bashcd /opt/backupNUM1=$(/bin/ls -l |grep "^d"|awk '{print $9}'|wc -l)NUM2=$(/usr/bin/expr $NUM1 - 3)/bin/ls -l |grep "^d"|awk '{print $9}'|/bin/sed -n "1,$NUM2 p"|xargs rm -rf

授予脚本执行权限

[root@syslog-ng backup]# chmod 755 /opt/del.sh

执行脚本

[root@syslog-ng backup]# sh /opt/del.sh

执行脚本后,发现保留下来的备份目录就行最新的三份了

[root@syslog-ng backup]# lltotal 12drwxr-xr-x. 2 root root 4096 Jul 11 14:48 20180709drwxr-xr-x. 2 root root 4096 Jul 11 14:48 20180710drwxr-xr-x. 2 root root 4096 Jul 11 14:48 20180711

转载地址:http://zjxgx.baihongyu.com/

你可能感兴趣的文章
UITableView整理
查看>>
Akka笔记之Actor简介
查看>>
Android 使用SystemBarTint设置状态栏颜色
查看>>
GreenPlum vs MonetDB case
查看>>
JVM源码分析之SystemGC完全解读
查看>>
盘点 PHP 和 ASP.NET 的10大对比!
查看>>
Systemtap examples, Identifying Contended User-Space Locks
查看>>
PostgreSQL Daily Maintenance - cluster table
查看>>
Problem8
查看>>
Git 自救指南
查看>>
async和await详解
查看>>
仿写一个android图片压缩工具
查看>>
Flutter日历,可以自定义风格UI
查看>>
java 零基础快速入门到初中级路线2019快速成长为java初级中级工程师
查看>>
机器学习常用的5种学习框架
查看>>
精读《V8 引擎 Lazy Parsing》
查看>>
从零开发一个健壮的npm包
查看>>
Swift3、4中的@objc、@objcMembers和dynamic
查看>>
前端好文章
查看>>
三方登录
查看>>