保留最新N份備份目錄指令碼

2021-09-08 11:54:44 字數 3354 閱讀 6158

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

[root@syslog-ng ~]# cd /opt/backup/

[root@syslog-ng backup]# ls -l

total 68

drwxr-xr-x. 2 root root 4096 jul 11 14:48 20180626

drwxr-xr-x. 2 root root 4096 jul 11 14:48 20180627

drwxr-xr-x. 2 root root 4096 jul 11 14:48 20180628

drwxr-xr-x. 2 root root 4096 jul 11 14:48 20180629

drwxr-xr-x. 2 root root 4096 jul 11 14:48 20180630

drwxr-xr-x. 2 root root 4096 jul 11 14:48 20180631

drwxr-xr-x. 2 root root 4096 jul 11 14:48 20180701

drwxr-xr-x. 2 root root 4096 jul 11 14:48 20180702

drwxr-xr-x. 2 root root 4096 jul 11 14:48 20180703

drwxr-xr-x. 2 root root 4096 jul 11 14:48 20180704

drwxr-xr-x. 2 root root 4096 jul 11 14:48 20180705

drwxr-xr-x. 2 root root 4096 jul 11 14:48 20180706

drwxr-xr-x. 2 root root 4096 jul 11 14:48 20180707

drwxr-xr-x. 2 root root 4096 jul 11 14:48 20180708

drwxr-xr-x. 2 root root 4096 jul 11 14:48 20180709

drwxr-xr-x. 2 root root 4096 jul 11 14:48 20180710

drwxr-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 20180626

drwxr-xr-x. 2 root root 4096 jul 11 14:48 20180627

drwxr-xr-x. 2 root root 4096 jul 11 14:48 20180628

drwxr-xr-x. 2 root root 4096 jul 11 14:48 20180629

drwxr-xr-x. 2 root root 4096 jul 11 14:48 20180630

drwxr-xr-x. 2 root root 4096 jul 11 14:48 20180631

drwxr-xr-x. 2 root root 4096 jul 11 14:48 20180701

drwxr-xr-x. 2 root root 4096 jul 11 14:48 20180702

drwxr-xr-x. 2 root root 4096 jul 11 14:48 20180703

drwxr-xr-x. 2 root root 4096 jul 11 14:48 20180704

drwxr-xr-x. 2 root root 4096 jul 11 14:48 20180705

drwxr-xr-x. 2 root root 4096 jul 11 14:48 20180706

drwxr-xr-x. 2 root root 4096 jul 11 14:48 20180707

drwxr-xr-x. 2 root root 4096 jul 11 14:48 20180708

drwxr-xr-x. 2 root root 4096 jul 11 14:48 20180709

drwxr-xr-x. 2 root root 4096 jul 11 14:48 20180710

drwxr-xr-x. 2 root root 4096 jul 11 14:48 20180711

[root@syslog-ng backup]# /bin/ls -l |grep "^d"|awk ''

20180626

20180627

20180628

20180629

20180630

20180631

20180701

20180702

20180703

20180704

20180705

20180706

20180707

20180708

20180709

20180710

20180711

[root@syslog-ng backup]# /bin/ls -l |grep "^d"|awk ''|wc -l

17[root@syslog-ng backup]# /usr/bin/expr 17 - 3

14

處理指令碼如下

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

#!/bin/bash

cd /opt/backup

num1=$(/bin/ls -l |grep "^d"|awk ''|wc -l)

num2=$(/usr/bin/expr $num1 - 3)

/bin/ls -l |grep "^d"|awk ''|/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]# ll

total 12

drwxr-xr-x. 2 root root 4096 jul 11 14:48 20180709

drwxr-xr-x. 2 root root 4096 jul 11 14:48 20180710

drwxr-xr-x. 2 root root 4096 jul 11 14:48 20180711

shell指令碼(保留最近N天的備份)

bin bash 保留最近n天的備份 2019年6月16日11 32 59 需求 檔案備份格式為date f h m 節假日不備份 無論過幾個節假日,始終保留最新n天的備份 模擬備份 mkdir pwd test touch pwd test date d 1 day ago f h m tar.g...

mysql備份指令碼並保留7天

指令碼需求 每天備份mysql資料庫,保留7天的指令碼。存放在 opt dbbak目錄中。指令碼名稱為database x xx xx.sql 指令碼內容 bin bash export now date y m d export data dir opt dbbak usr local ywgh ...

資料庫 目錄 自動備份指令碼

windows通過任務計畫可以呼叫bat批處理檔案。可以實現簡單的備份功能 與同步功能。1.通過exp匯出oracle資料庫,自動命名為 name1 當日日期 輸出log。匯出成功後,將dmp檔案壓縮打包,刪除dmp檔案。exp system oracle servicename owner dat...