From 3fa800c9eda68b6292010c3ce2fc1d91924edbf6 Mon Sep 17 00:00:00 2001 From: Kimdonghwan Date: Sun, 14 Jun 2026 17:34:42 +0900 Subject: [PATCH] =?UTF-8?q?chore:=20DB=20=EB=B0=B1=EC=97=85=EC=9D=84=20?= =?UTF-8?q?=EC=B5=9C=EA=B7=BC=207=EA=B0=9C=EB=A7=8C=20=EB=B3=B4=EA=B4=80(?= =?UTF-8?q?=EA=B0=9C=EC=88=98=20=EA=B8=B0=EC=A4=80)=ED=95=98=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PROD 백업 보관 정책을 날짜 기준(30일) → 최근 7개 보관(개수 기준)으로 변경. 백업 주기와 무관하게 항상 최신 7개만 유지합니다. - RETENTION_DAYS → RETENTION_COUNT (=7) - 로컬: ls -1t 최신순 정렬 후 7개 초과분 삭제 - Object Storage: 파일명(날짜순) 정렬 후 (전체-7)개 오래된 파일 삭제 main 에 백업 스크립트 변경만 단독 반영(앱 코드 변경 없음)하기 위한 커밋입니다. Co-Authored-By: Claude Opus 4.8 (1M context) --- deploy/shared/db-backup.sh | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/deploy/shared/db-backup.sh b/deploy/shared/db-backup.sh index 0ef6bc03..3b894437 100644 --- a/deploy/shared/db-backup.sh +++ b/deploy/shared/db-backup.sh @@ -3,7 +3,7 @@ DATE=$(date +%Y-%m-%d_%H%M) BACKUP_DIR="/home/souzip-prod/backups" BACKUP_FILE="$BACKUP_DIR/souzip-$DATE.dump" BUCKET="souzip-db-backup" -RETENTION_DAYS=30 +RETENTION_COUNT=7 if [ -f "/home/souzip-prod/souzip/deploy/prod/.env" ]; then export PROD_POSTGRES_USER=$(grep '^PROD_POSTGRES_USER=' /home/souzip-prod/souzip/deploy/prod/.env | cut -d= -f2-) @@ -42,21 +42,23 @@ if [ $? -ne 0 ]; then fi echo "[INFO] Object Storage 업로드 완료" -# 로컬 오래된 백업 삭제 -find $BACKUP_DIR -name "souzip-*.dump" -mtime +$RETENTION_DAYS -delete - -# Object Storage 오래된 백업 삭제 -s3cmd $S3CMD_OPTS ls s3://$BUCKET/ | awk '{print $4}' | while read file; do - file_date=$(echo $file | grep -oP '\d{4}-\d{2}-\d{2}') - if [ ! -z "$file_date" ]; then - days_old=$(( ( $(date +%s) - $(date -d "$file_date" +%s) ) / 86400 )) - if [ $days_old -gt $RETENTION_DAYS ]; then - s3cmd $S3CMD_OPTS del $file - echo "[INFO] 오래된 백업 삭제 - $file" - fi - fi +# 로컬 백업은 최신순으로 RETENTION_COUNT 개만 보관하고 나머지 삭제 +ls -1t $BACKUP_DIR/souzip-*.dump 2>/dev/null | tail -n +$((RETENTION_COUNT + 1)) | while read -r file; do + rm -f "$file" + echo "[INFO] 오래된 로컬 백업 삭제 - $file" done +# Object Storage 백업도 최신순으로 RETENTION_COUNT 개만 보관 (파일명이 날짜순이라 정렬=시간순) +remote_files=$(s3cmd $S3CMD_OPTS ls s3://$BUCKET/ | awk '{print $4}' | grep 'souzip-.*\.dump$' | sort) +remote_total=$(printf '%s\n' "$remote_files" | grep -c .) +remote_delete_count=$((remote_total - RETENTION_COUNT)) +if [ "$remote_delete_count" -gt 0 ]; then + printf '%s\n' "$remote_files" | head -n "$remote_delete_count" | while read -r file; do + s3cmd $S3CMD_OPTS del "$file" + echo "[INFO] 오래된 백업 삭제 - $file" + done +fi + echo "[INFO] DB 백업 프로세스 완료 - $DATE" curl -s -H "Content-Type: application/json" \