선택한 파일 및 디렉터리의 자동 tar 백업을 위한 Bash 스크립트

선택한 파일 및 디렉터리의 자동 tar 백업을 위한 Bash 스크립트

Linux 환경의 특정 디렉토리 내에서 모든 항목을 선택해야 합니다.파일특정 날짜(7일이라고 가정) 이후에 수정된 경우와 모든 날짜디렉토리(루트 디렉토리에만 있으므로 재귀적이지 않음) 동일한 날짜 이후에 생성되었습니다.

그 후에는 마지막으로 주어진 규칙을 제외한 3개의 디렉토리를 처리해야 합니다. 이를 위해서는 프로세스가 각 프로세스 내에서 반복되어야 합니다. 이 디렉터리 중 하나에는 무슨 일이 있어도 제외할 파일이 있습니다.

마지막으로 이러한 패턴과 일치하는 모든 개체를 단일 .tar 아카이브에 추가해야 합니다. 물론 모든 파일/디렉토리는 .tar 파일 내에서 전체 상대 경로를 유지해야 합니다(기본 디렉토리에서 시작).

그럼 다음과 같은 결과가 있다고 가정해 보겠습니다.

myHome
|-- normalDir1                  //  older than 7 days
|   |-- blah.txt
|   |-- to_be_excluded_nmw.txt  //  should never be included anyways
|   `-- poems.txt
|-- normalDir2                  //  created yesterday
|   |-- blah2.txt               /*
|   |-- whatever2.txt            *  Since it's a normal directory,
|   |-- whatever3.txt            *  I want to exclude these files from .tar
|   `-- poems2.txt               */  
|-- exceptionDirectory1         //  older than 7 days
|   |-- actions                 //  older than 7 days
|   |   `-- power.sh            //  older than 7 days
|   `-- events                  //  older than 7 days
|       |-- deploy.php          //  older than 7 days
|       `-- set.php             //  older than 7 days
|-- exceptionDirectory2         //  older than 7 days
|   |-- actions2
|   |   `-- power2.sh           //  created yesterday
|   `-- events2                 //  older than 7 days
|       |-- deploy2.php         //  created yesterday
|       `-- set2.php            //  older than 7 days
|-- file_to_be_updated.php      //  created yesterday
`-- file_NOT_to_be_updated.php  //  older than 7 days

결과 .tar에는 다음이 포함되어야 합니다.

./normalDir2/
./exceptionDirectory2/actions2/power2.sh
./exceptionDirectory2/events2/deploy2.php
./file_to_be_updated.php

저는 다음 스크립트를 만들었습니다.

#!/bin/bash
TODAY=`date --rfc-3339=date`
FILENAME=$TODAY-package.tar
find ./require ! -name db_connection.php         ! -path ./require -mtime -7 -print | xargs tar cvf `date --rfc-3339=date`-package.tar
find ./img                                       ! -path ./img     -mtime -7 -print | xargs tar uvf `date --rfc-3339=date`-package.tar
find ./plugin                                    ! -path ./plugin  -mtime -7 -print | xargs tar uvf `date --rfc-3339=date`-package.tar
find . -maxdepth 1 ! -name $TODAY-package.tar.gz ! -path .         -mtime -7 -print | xargs tar uvf `date --rfc-3339=date`-package.tar

하지만 다음 오류와 함께 거의 즉시 종료되므로 제대로 작동하지 않는 것 같습니다.

tar: ./img: Impossibile open: Is a directory

"require", "img" 및 "plugin"은 재귀적으로 처리되는 세 가지 특수 디렉토리입니다. 스크립트에 어떤 문제가 있나요? 도와 주셔서 감사합니다.

답변1

오류의 원인은 아래의 파일 이름에 공백이나 기타 특수 문자가 있기 때문입니다 ./img.

-print에 옵션을 사용하는 대신 `xargs'에 해당 옵션을 find사용하십시오 :-print0-0

 find ./img ! -path ./img -mtime -7 -print0 | xargs -0 tar uvf `date --rfc-3339=date`-package.tar

답변2

  • 대신 find ./foo ! -path ./foofind -mindepth 1 ./foo. 이렇게 하면 파일만내부에지정된 경로가 인쇄됩니다.
  • tarGNU 가 있으면 --exclude=PATTERN. 그렇게 하면 다음과 같이 작성할 수 있습니다.

    today="$(date --rfc-3339="date")"
    last_week="$(date --rfc-3339="date" --date="-7 days")"
    tar --no-recursion --exclude=db_connection.php --after-date="$last_week" cvf "${today}-package.tar" .
    tar --after-date="$last_week" uvf "${today}-package.tar" ./require ./img ./plugin
    

답변3

나는 즉석에서 그것을 작성하고 그것이 주변에서 작동하는지 시도합니다.

tar cvf   --no-recursion --after-date $yourdate $TarFile * */* 
tar uvrf  --after-date $yourdate $TarFile ./require ./plugin ./img

관련 정보