2012-09-17 31 views
-1

我需要自動清理僅保存備份文件的基於Linux的FTP服務器。刪除目錄中的所有文件,但列出的匹配特定條件除外

在我們的「\ var \ DATA」目錄中是一個目錄集合。這裏用於備份的任何目錄都以「DEV」開頭。在每個「DEVxxx *」目錄中都有實際的備份文件,以及在這些設備維護過程中可能需要的所有用戶文件。

我們只想保留下列文件 - 什麼都在這些「DEVxxx *」目錄中找到要刪除的:

不符合上述
The newest two backups: ls -t1 | grep -m2 ^[[:digit:]{6}_Config] 
The newest backup done on the first of the month: ls -t1 | grep -m1 ^[[:digit:]{4}01_Config] 
Any file that was modified less than 30 days ago: find -mtime -30 
Our good configuration file: ls verification_cfg 

任何應予以刪除。

我們如何編寫腳本?

我猜BASH腳本可以做到這一點,我們可以創建一個cron作業每天運行來執行任務。

回答

0

這是值得的,這裏是我創建的bash腳本來完成我的任務。歡迎評論。

#!/bin/bash 

# This script follows these rules: 
# 
# - Only process directories beginning with "DEV" 
# - Do not process directories within the device directory 
# - Keep files that match the following criteria: 
#  - Keep the two newest automated backups 
#  - Keep the six newest automated backups generated on the first of the month 
#  - Keep any file that is less than 30 days old 
#  - Keep the file "verification_cfg" 
# 
# - An automated backup file is identified as six digits, followed by "_Config" 
# e.g. 20120329_Config 


# Remember the current directory 
CurDir=`pwd` 

# FTP home directory 
DatDir='/var/DATA/' 
cd $DatDir 

# Only process directories beginning with "DEV" 
for i in `find . -type d -maxdepth 1 | egrep '\.\/DEV' | sort` ; do 
cd $DatDir 

echo Doing "$i" 
cd $i 

# Set the GROUP EXECUTE bit on all files 
find . -type f -exec chmod g+x {} \; 

# Find the two newest automated config backups 
for j in `ls -t1 | egrep -m2 ^[0-9]{8}_Config$` ; do 
    chmod g-x $j 
done 

# Find the six newest automated config backups generated on the first of the month 
for j in `ls -t1 | egrep -m6 ^[0-9]{6}01_Config$` ; do 
    chmod g-x $j 
done 

# Find all files that are less than 30 days old 
for j in `find -mtime -30 -type f` ; do 
    chmod g-x $j 
done 

# Find the "verification_cfg" file 
for j in `find -name verification_cfg` ; do 
    chmod g-x $j 
done 

# Remove any files that still have the GROUP EXECUTE bit set 
find . -type f -perm -g=x -exec rm -f {} \; 

done 

# Back to the users current directory 
cd $CurDir 
1

也許這樣的事情?

{ ls -t1 | grep -m2 ^[[:digit:]{6}_Config] ; 
    ls -t1 | grep -m1 ^[[:digit:]{4}01_Config] ; 
    find -mtime -30 ; 
    ls -1 verification_cfg ; 
} | rsync -a --exclude=* --include-from=- /var/DATA/ /var/DATA.bak/ 
rm -rf /var/DATA 
mv /var/DATA.bak /var/DATA 
+0

過程中看起來不錯,但,有我們試圖訪問我們想要的文件中的一個微小的機會,它只會在/var/DATA.bak存在。 沒有辦法去除所有列出的文件嗎? – Calab

+0

嘗試'rsync -a --delete /var/DATA.bak//var/DATA /'而不是'rm -rf/var/DATA'。 –

相關問題