-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 作业来执行任务。

4

2 回答 2

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
于 2012-09-17T12:57:47.857 回答
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
于 2012-09-19T10:25:45.313 回答