我們經常會通過find命令進行批量操作,如:批量刪除舊文件、批量修改、基于時間的文件統計、基于文件大小的文件統計等,在這些操作當中,由于rm刪除操作會導致目錄結構變化,如果要通過find結合rm的操作寫成腳本,就會遇到一些麻煩,本文通過一個例子為大家進行介紹。
系統環境:
SUSE Linux Enterprise Server 11 或
Red Hat Enterprise Linux
問題癥狀:
客戶現場有一個自動化的腳本,有以下的find語句,每天運行以刪除某個目錄下7天以前的文件或目錄,這些目錄都是按時間順序生成PostgreSQL數據庫的WAL日志及其錯誤日志pg_log:
/bin/find /enterprisedb_backup/postgresql/ -mtime +7 -exec /bin/rm -rf '{}' /;
運行過程中,間歇性地出現以下錯誤:
[root@edb ~]# /bin/find /enterprisedb_backup/postgresql/ -mtime +7 -exec /bin/rm -rf {} /;/bin/find: `/enterprisedb_backup/postgresql/network-scripts': No such file or directory[root@edb ~]# echo $?1顯然,以上命令返回了錯誤的結果,但客戶反映說,以上腳本運行后目錄下7天前的數據的確備刪除了。
問題分析:
進行故障重現,在另一臺服務器中通過模擬數據單獨運行find命令分析此問題,測試過程如下:
1.模擬數據
[root@edbnode1 ~]# dateWed Jun 18 23:08:18 CST 2014[root@edbnode1 ~]# cp -rcp /etc/sysconfig/network-scripts/ /enterprisedb_backup/postgresql/[root@edbnode1 ~]# cp -rcp /etc/init.d/iptables /enterprisedb_backup/postgresql/## 以上通過 cp -rcp 命令使得拷貝到目標目錄的數據保持包括:建立時間、用戶權根等信息,以模擬出一個舊文件及一個舊目錄[root@edbnode1 ~]# ll /enterprisedb_backup/postgresql/total 16-rwxr-xr-x. 1 root root 9409 Oct 31 2012 iptablesdrwxr-xr-x. 2 root root 4096 Jun 18 2013 network-scripts## 以上可以看到iptables文件是2012年建立的,network-scripts是2013年建立的,都遠遠超過了7天[root@edbnode1 ~]# ll /enterprisedb_backup/postgresql/*-rwxr-xr-x. 1 root root 9409 Oct 31 2012 /enterprisedb_backup/postgresql/iptables/enterprisedb_backup/postgresql/network-scripts:total 212-rw-r--r--. 1 root root 159 Jun 18 2013 ifcfg-eth0-rw-r--r--. 1 root root 203 Jun 18 2013 ifcfg-eth1-rw-r--r--. 1 root root 203 Jun 18 2013 ifcfg-eth2-rw-r--r--. 1 root root 254 Jan 9 2013 ifcfg-lolrwxrwxrwx. 1 root root 20 Jun 18 2013 ifdown -> ../../../sbin/ifdown-rwxr-xr-x. 1 root root 627 Jan 9 2013 ifdown-bnep-rwxr-xr-x. 1 root root 5397 Jan 9 2013 ifdown-eth-rwxr-xr-x. 1 root root 781 Jan 9 2013 ifdown-ippp-rwxr-xr-x. 1 root root 4168 Jan 9 2013 ifdown-ipv6lrwxrwxrwx. 1 root root 11 Jun 18 2013 ifdown-isdn -> ifdown-ippp-rwxr-xr-x. 1 root root 1481 Jan 9 2013 ifdown-post-rwxr-xr-x. 1 root root 1064 Jan 9 2013 ifdown-ppp-rwxr-xr-x. 1 root root 835 Jan 9 2013 ifdown-routes-rwxr-xr-x. 1 root root 1370 Jan 9 2013 ifdown-sit-rwxr-xr-x. 1 root root 1434 Jan 9 2013 ifdown-tunnellrwxrwxrwx. 1 root root 18 Jun 18 2013 ifup -> ../../../sbin/ifup-rwxr-xr-x. 1 root root 12365 Jan 9 2013 ifup-aliases-rwxr-xr-x. 1 root root 859 Jan 9 2013 ifup-bnep-rwxr-xr-x. 1 root root 10157 Jan 9 2013 ifup-eth-rwxr-xr-x. 1 root root 11971 Jan 9 2013 ifup-ippp-rwxr-xr-x. 1 root root 10401 Jan 9 2013 ifup-ipv6lrwxrwxrwx. 1 root root 9 Jun 18 2013 ifup-isdn -> ifup-ippp-rwxr-xr-x. 1 root root 727 Jan 9 2013 ifup-plip-rwxr-xr-x. 1 root root 954 Jan 9 2013 ifup-plusb-rwxr-xr-x. 1 root root 2364 Jan 9 2013 ifup-post-rwxr-xr-x. 1 root root 4154 Jan 9 2013 ifup-ppp-rwxr-xr-x. 1 root root 1925 Jan 9 2013 ifup-routes-rwxr-xr-x. 1 root root 3499 Jan 9 2013 ifup-sit-rwxr-xr-x. 1 root root 2488 Jan 9 2013 ifup-tunnel-rwxr-xr-x. 1 root root 3770 Jan 9 2013 ifup-wireless-rwxr-xr-x. 1 root root 4623 Jan 9 2013 init.ipv6-global-rwxr-xr-x. 1 root root 1125 Jan 9 2013 net.hotplug-rw-r--r--. 1 root root 13079 Jan 9 2013 network-functions-rw-r--r--. 1 root root 29853 Jan 9 2013 network-functions-ipv6## 以上可以看到network-script不是一個空的目錄,當中還有文件,而且文件也都已經是7天前建立的了
新聞熱點
疑難解答