폴더 백업 예약

폴더 백업 예약

CentOs 7의 사용자 홈 디렉터리를 원격 호스트나 NAS 또는 ~/.snapshot에 자동으로 백업하는 방법을 찾고 있습니다. 일부 Linux 설정에서는 사용자의 홈 디렉터리(~/.snapshot/)에서 홈 디렉터리(예: ~/.snapshot/weekly1)의 매시간, 밤, 매주 백업을 보관하는 .snapshot 폴더를 보았습니다. 1주일 전에 사용자의 홈 디렉토리에 있었던 사용자 홈 디렉토리의 복사본).

/home/username/.snapshot/ 디렉터리는 사용자에게 읽기 전용입니다. 하드웨어 오류로부터 보호하기 위한 백업이 아닙니다. 변경한 내용이 마음에 들지 않으면 어제 또는 오늘 아침의 파일을 복원할 수 있는 것이 좋습니다.

Stack Overflow에서 관련 게시물을 몇 개 본 적이 있지만, 아직까지 전체 작업 흐름을 설명하는 가이드는 본 적이 없습니다.

내가 지금까지 알고 있는 것은 다음과 같습니다.

  1. rsync특정 폴더의 내용을 원격 호스트, NAS 또는 (~/.snapshot/hourly0)에 복사하는 데 사용됩니다.
  2. rsync명령을 실행하는 쉘 스크립트 만들기

#!/bin/bash sudo rsync -av --progress --delete --log-file=/home/username/$(date +%Y%m%d)_rsync.log --exclude "/home/username/.snapshot" /home/username/ /home/username/.snapshot/hourly1

  1. 스크립트의 권한을 변경하여 실행 가능하게 만듭니다.

sudo chmod +x /home/username/myscript.sh

  1. crontab원하는 백업 간격으로 rsync 명령을 예약하는 데 사용됩니다.

  2. 예약된 시간별 rsync를 실행하기 전에 어떻게든 hourly0을 hourly1로 이동하세요.

  3. rsync가 성공적으로 완료된 후 가장 오래된 백업 삭제

이를 수행하는 방법에 대한 가이드가 있습니까? 시간이 지남에 따라(예: 1주차에서 2주차까지) 폴더 이름을 자동으로 바꾸는 방법이나 최대 9주만 유지하기로 결정한 경우 "10주차"를 제거하는 방법을 이해할 수 없습니다. 이건 또 다른 cron직업인가요?

업데이트: 좀 더 인터넷 검색을 한 후에 NetApp이 스냅샷 폴더를 생성한다는 사실을 발견했습니다. 지금은 NetApp NAS가 없습니다.https://library.netapp.com/ecmdocs/ECMP1635994/html/GUID-FB79BB68-B88D-4212-A401-9694296BECCA.html

답변1

이 가이드는 어떻습니까?

  1. 스크립트 만들기: 새 파일을 만들고 이름을 지정하고 myrsync.sh다음 줄을 복사하여 붙여넣습니다.

    #!/bin/bash sudo rsync -av --progress --delete --log-file=/home/yourusername/desktop/$(date+%Y%m%d)rsync.log --exclude "/home/yourusername/.folder"/home/data/media/dataBackup$(날짜+%Y%m%d_%T)

로고의 의미:

 -av bit: 'a' means archive, or copy everything recursively, preserving things like permissions, ownership and time stamps. 
  -'v' is verbose, so it tells you what its doing, either in the terminal, in this case, in the log file.
  --progress gives you more specific info about progress.
  --delete checks for changes between source and destination, and deletes any files at the destination that you've deleted at the source.
  --log-file saves a copy of the rsync result to a date-stamped file on my desktop.
  --exclude leaves out any files or directories you don't want copied. In the command above, the .folder directory

  /home/data is the directory I want copied. /home/data copies the directory and its contents, /home/data would just copy the contents. 

  /media/dataBackup_$(date +%Y%m%d_%T) is the separate drive. Change this to whatever your backup location is. Note that `rsync` will name every sync differently based on day/time of sync
  1. ~$HOME에 저장 myrsync.sh하고 다음을 입력하여 실행 가능하게 만듭니다.

    sudo chmod +x /home/사용자 이름/Desktop/rsync-shell.sh

이제 .sh 파일을 두 번 클릭하고 "터미널에서 실행"을 선택하면 암호를 묻고 실행되며 바탕 화면에 로그 파일이 남습니다. 또는 크론 작업을 생성하여 이 작업을 수행할 수 있습니다!

  1. 예약 된 일들

다음을 입력하여 myrsync.sh파일을 /root에 복사합니다 .

sudo cp /home/your-username/Desktop/myrsync.sh /root

그런 다음 다음을 입력하십시오.

sudo crontab -e

다음 줄이 표시됩니다. 분 시 일 월 연도 명령

그 아래에 다음을 입력하십시오: 0 22 * ​​​​* * /root/myrsync.sh > $HOME/readme.log 2>&1

이는 다음을 의미합니다.

The minute
The hour in military time (24 hour) format (0 to 23)
The day of the month (1 to 31)
The month (1 to 12)
The day of the week(0 or 7 is Sun, or use name)
The command to run
So at 22:00 (10pm) every day root will run the shell script, without prompting you for sudo password (because its running as root already).

이제 Ctrl-X를 누르고 "Y"를 입력한 다음 Enter를 누르세요.

오래된 백업을 삭제하는 한 가지 방법은 각 동기화의 타임스탬프가 포함된 파일을 만드는 것입니다. 예를 들어 rsync명령 뒤에 다음 명령을 추가합니다.myrsync.sh

date +%Y%m%d_%T >> time.txt

타임스탬프와 일치하는 백업을 삭제하려면 이 명령을 사용합니다 find. 예: date +%Y%m%d_%T >> time.txt다음 명령을 in 뒤에 추가합니다.myrsync.sh

find . -type f ! -newer /tmp/timestamp -delete

또는

find . ! -newermt $date ! -type d -delete

특정 날짜/시간 이전의 백업이 삭제됩니다.

시간별/일별/월별 백업에 대한 자세한 내용과 샘플 코드를 확인할 수 있습니다.여기

관련 정보