Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions deploy/shared/db-backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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-)
Expand Down Expand Up @@ -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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 7일 보관 정책을 개수 기준으로 바꾸지 마세요

medium: 커밋 설명은 백업을 7일 보관하는 변경인데, 여기서는 실행 주기와 무관하게 최신 7개만 남깁니다. 서버 crontab이 하루 1회보다 자주 돌거나 운영자가 같은 날 수동 재실행하면 7일이 지나지 않은 백업까지 삭제되어 복구 지점이 의도보다 빠르게 사라질 수 있으니, 기존처럼 날짜/mtime 기준으로 RETENTION_DAYS=7을 적용하는 방식으로 유지하는 것을 권장합니다.

Useful? React with 👍 / 👎.

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" \
Expand Down
Loading