윈도우에서 매일 자동 덤프 스케줄링 만들기
윈도우에서 매일 자동 덤프 스케줄링 만들기
원문: https://www.notion.so/25ebf506e99480e9a182d9d5cd389e2b?pvs=1
0) 준비물
-
pg_dump.exe 경로: D:\Downloads\postgresql-17.6-1-windows-x64-binaries\pgsql\bin
-
백업 폴더: D:\test_pg_dump
-
접속: 10.100.00.000:5432 / DB=test / USER=tester / PWD=testpwd
1) 백업 스크립트 파일 만들기
파일: D:\sem_pg_dump\sem_backup.ps1
(특정 테이블만 골라서 백업)
# ================== 설정 ==================
$env:PGPASSWORD = 'testpwd' # DB 비밀번호
$PG_BIN = 'D:\Downloads\postgresql-17.6-1-windows-x64-binaries\pgsql\bin'
$OUT_DIR = 'D:\test_pg_dump'
$RETENTION_DAYS = 30 # 보관 기간(일)
# 파일명/로그
$TS = Get-Date -Format 'yyyyMMdd_HHmmss'
$OUT = Join-Path $OUT_DIR "test_backup_$TS.dump"
$LOG = Join-Path $OUT_DIR "test_backup.log"
# 폴더 보장
New-Item -ItemType Directory -Force -Path $OUT_DIR | Out-Null
# ================== 백업 실행 ==================
Add-Content $LOG "`n[$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')] START backup -> $OUT"
Set-Location $PG_BIN
.\pg_dump.exe -h 10.100.00.000 -p 5432 -U tester -d test -Fc -Z 9 --no-owner --no-privileges -v --strict-names `
-t public.testtable1 `
-t public.testtable2 `
-t public.testtable3 `
-t public.testtable4 `
-f $OUT
# 성공/용량 기록
if (Test-Path $OUT) {
$mb = "{0:N2}" -f ((Get-Item $OUT).Length / 1MB)
Add-Content $LOG "[$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')] DONE size=${mb}MB"
} else {
Add-Content $LOG "[$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')] FAILED (no output file)"
}
# ================== 보관 정책(30일 초과 삭제) ==================
Get-ChildItem $OUT_DIR -Filter 'test_backup_*.dump' |
Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-$RETENTION_DAYS) } |
Remove-Item -Force
Add-Content $LOG "[$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')] RETENTION: kept last $RETENTION_DAYS days"
2) 작업 스케줄러 등록 (매일 00:00)
-
작업 스케줄러 실행 → “작업 만들기”
-
트리거: “매일” / 시간 “00:00”
-
동작:
3) 테스트 & 검증
-
작업 스케줄러에서 우클릭 → 실행 → D:\test_pg_dump에 test_backup_YYYYMMDD_HHmmss.dump 생성 확인
-
오류/용량은 D:\test_pg_dump\test_backup.log에서 확인
-
덤프가 복원 가능한지 간단 체크:
4) 설명
매일 자정 자동 백업, 특정 테이블만, 커스텀 포맷 -Fc, 압축 -Z 9, 보관 30일
Comments