최근, 한 친구가 VPS 제공업체가 사전 통보 없이 갑자기 사업을 접는 문제를 겪었습니다. 불행히도, 저렴한 가격을 위해 틈새 제공업체를 선택했는데, 이 업체는 데이터 다운로드 기능조차 제공하지 않았습니다. 더糟糕한 것은, 그녀는 정기적으로 백업을 다운로드하는 습관이 없었습니다. 하루아침에 그녀의 사이트는 모든 데이터와 함께 사라졌습니다.
이 충격적인 경험으로 인해, 저는 추가적인 객체 저장 서비스나 다른 곳에 돈을 쓰지 않고도 서버에서 백업을 자동으로 로컬 컴퓨터로 정기적으로 다운로드하는 PowerShell 스크립트를 작성했습니다. 필요한 것은 정기적으로 사용하며(하드 드라이브 공간이 충분하고) 인터넷에 연결된 Windows 컴퓨터 하나뿐입니다.
이 스크립트는 5일 이전의 백업을 자동으로 정리합니다. 필요에 따라 자동 백업 간격과 로컬에 저장할 가장 오래된 백업 시간을 설정할 수 있습니다.
$ssh_port = 22
$ssh_address = "username@your.site"
Write-Output "Discourse 백업 다운로드 작업 시작..."
Write-Output '------------------------'
Write-Output "최신 백업 파일 가져오는 중..."
Write-Output ''
while ($true) {
$filename = ''
while ($true) {
try {
Write-Output "> ssh -p $ssh_port $ssh_address 'cd /var/discourse/shared/standalone/backups/default/ && ls -t | head -n 1'"
Write-Output ''
$filename = ssh -p $ssh_port "$ssh_address" 'cd /var/discourse/shared/standalone/backups/default/ && ls -t | head -n 1'
break
}
catch {
$filename = ''
Write-Output "가져오기 실패... 로그는 다음과 같습니다:"
Write-Output '-------------'
Write-Output $_
$answer = Read-Host "다시 가져오시겠습니까? (y/N)"
if ($answer -ne 'y') {
break
}
Write-Output ''
}
}
if ([String]::IsNullOrEmpty($filename)) {
Write-Output "오류: 파일 이름 $filename 가져오기 실패"
Write-Output ''
$answer = Read-Host '재시도?(y/N)'
if ($answer -eq 'y') {
}
else {
exit 1
}
}
else {
Write-Output "최신 백업: $filename"
Write-Output ''
$need_download = $true
if (Test-Path ".\backups\$filename") {
$answer = Read-Host ".\backups\$filename 이(가) 이미 존재합니다. 다시 다운로드하시겠습니까?(y/N)"
Write-Output ''
if ($answer -ne 'y') {
$need_download = $false
}
}
if ($need_download) {
Write-Output "다운로드 시작..."
Write-Output ''
while ($true) {
try {
Write-Output "scp -p $ssh_port ${ssh_address}:/var/discourse/shared/standalone/backups/default/$filename .\backups\"
Write-Output ''
scp -p $ssh_port "${ssh_address}:/var/discourse/shared/standalone/backups/default/$filename" .\backups\
Write-Output "다운로드 완료"
Write-Output ''
break
}
catch {
Write-Output "다운로드 실패 >_<... 로그는 다음과 같습니다:"
Write-Output ''
Write-Output $_
$answer = Read-Host "다시 다운로드하시겠습니까? (y/N)"
Write-Output ''
if ($answer -ne 'y') {
break
}
}
}
}
Write-Output "오래된 백업 파일 정리 시도 시작..."
Write-Output ''
$count = 0
$backupfiles = Get-ChildItem -Path .\backups\
foreach ($file in $backupfiles) {
if ($file.CreationTime -le (Get-Date).AddDays(-5)) {
try {
Write-Output "오래된 백업 파일 $file 삭제 중..."
Write-Output ''
$file.Delete()
$count = $count + 1
} catch {
Write-Output "오래된 백업 파일 $file 삭제 중 오류 발생 >_<"
Write-Output '-------------------'
Write-Output $_
Write-Output '-------------------'
}
}
}
if ($count -ge 0) {
Write-Output "$count 개의 오래된 백업 파일 정리 완료"
Write-Output ''
}
else {
Write-Output '정리할 오래된 백업 파일 없음'
Write-Output ''
}
Pause
exit 0
}
}
위 스크립트를 백업을 다운로드하려는 경로에 scriptname.ps1이라는 이름으로 저장하십시오. "PowerShell에서 실행"을 시도해 보세요. 성공하면 다음 단계로 진행할 수 있습니다.
작업 예약하기
- "예약된 작업"을 검색합니다.
- 예약된 작업 추가를 두 번 클릭합니다. 예약된 작업 마법사가 나타납니다.
- 다음을 클릭한 뒤, 찾아보기를 클릭합니다. 예약할 프로그램 선택 대화 상자가 나타납니다.
- 만든 스크립트로 이동하여 클릭한 후, 열기를 클릭합니다. 예약된 작업 마법사로 돌아옵니다.
- 작업 이름을 지정하거나 기본값인 파일 이름을 유지한 채, 스크립트를 실행할 빈도를 지정하고 다음을 클릭합니다.
- 시작 시간과 날짜(일일, 주간, 월간 등을 지정했다면) 및 반복을 지정하고 다음을 클릭합니다. 이 항목은 discourse의 자동 백업 사이클과 일치해야 합니다.
- 스크립트를 실행할 계정의 사용자 이름과 비밀번호를 입력하고 다음을 클릭합니다.
- 고급 속성을 구성하려면 확인란을 선택한 후, 완료를 클릭합니다.


