백업 스크립트에서 오류가 발생함

백업 스크립트에서 오류가 발생함

다음 스크립트가 있습니다.

#!/bin/sh

#Finds all folders and subsequent files that were modified yesterday and dumps them out to a text

updatedb && rm /tmp/*

echo $(locate -b `date --date='yesterday' '+%Y.%m.%d'`) > /tmp/files.txt

#creates variable out of the file. 

input="/tmp/files.txt"

yest=$(date --date='yesterday' '+%Y.%m.%d')

#loops through each entry of folders

while IFS= read -r folders

do

echo $folders

tar -cvf $folders\.tar $folders --remove-files

done < "$input"

오류가 발생합니다.

tar: /backup/DNS/intns1/2016.07.19: Cannot open: Is a directory

tar: Error is not recoverable: exiting now

내가 뭘 잘못하고 있는지 알아내려고 노력 중입니다...

답변1

datefind, tar및 다음 을 사용하는 최신 GNU 버전 xargs:

이 스크립트에는 bash, ksh, zsh 또는 다음을 이해하는 기타 합리적으로 현대적인 쉘이 필요합니다.프로세스 교체( <(...))

#!/bin/bash

    today='12am today'
yesterday='12am yesterday'

# function to create a tar file for a directory but only include
# files older than "$2" but newer than "$3".  Delete any files
# added to the tar archive.
#
# the tar file will be created uncompressed in the same directory
# as the directory itself.  e.g. ./dir --> ./dir.tar
function tarit() {
  dir="$1"
  older="$2"
  newer="$3"

  tar cvf "$dir.tar" -C "$dir/.." --null --remove-files \
    -T <(find "$dir" ! -newermt "$older" -newermt "$newer" -type f -print0)
}

# we need to export the function so we can
# run it in a bash subshell from xargs
export -f tarit

# we want to find newer files but output only the path(s)
# containing them with NULs as path separator rather than
# newlines, so use `-printf '%h\0'`.
find . ! -newermt "$today" -newermt "$yesterday" -type f -printf '%h\0' |
  sort -u -z |    # unique sort the directory list
  xargs -0r -I {} bash -c "tarit \"{}\" \"$today\" \"$yesterday\""

안전을 위해 모든 변수가 어떻게 인용되는지(bash 하위 쉘 내에서도 이스케이프 따옴표 내에서도) NUL이 구분 기호로 사용되는 방식에 유의하세요(경로 이름 및/또는 파일 이름에 짜증나지만 완벽하게 유효한 파일 이름이 포함되어 있는 경우 스크립트가 중단되지 않음). 공백이나 줄 바꿈과 같은 문자). 또한 가독성을 높이기 위해 들여쓰기와 추가 공백을 사용하는 것에 유의하세요. 그리고 주석을 사용하여 현재 진행 중인 작업과 이유를 설명하세요.

전체 디렉터리를 압축하고 디렉터리 자체(및 그 안의 모든 파일)를 삭제하려는 경우 더 쉽습니다.

#!/bin/bash

    today='12am today'
yesterday='12am yesterday'    

find . ! -newermt "$today" -newermt "$yesterday" -type f -printf '%h\0' |
  sort -u -z |
  xargs -0r -I {} tar cfv "{}.tar" "{}" -C "{}/.." --remove-files

개인적으로 이러한 스크립트를 사용하는 것은 매우 위험하다고 생각합니다. 주의하지 않으면(예를 들어 검색 경로 /대신 루트로 실행하거나 디렉터리에서 실행하는 경우) 운영 체제에 필요한 파일이나 디렉터리 전체를 삭제할 수도 있습니다. 홈 디렉터리(또는 쓰기 권한이 있는 모든 곳 - 어쨌든 tar 파일을 만들어야 함)의 uid에서 실행하더라도 보관하려는 파일이 삭제될 수 있습니다../

난 당신을 생각진짜--remove-files꼭 옵션을 사용해야 하는지 다시 생각해 볼 필요가 있습니다 tar. 무엇을 달성하고 싶나요? 이것은 일종의 tmpreaper입니까? 그렇다면 그 안의 파일을 맹목적으로 삭제하면 /tmp어제 /tmp에 생성되어 오늘이나 내일 재사용해야 하는 파일과 같은 장기 실행 프로세스가 중단될 수 있습니다.

간단히 말해서:이것은 장전된 산탄총 한 쌍입니다. 그들과 함께 발에 총을 쏘지 마십시오.

관련 정보