
개요
Cloud를 사용하는 경우 서비스(자동 스냅샷등)으로 제공하는 경우도 있지만 이번은 자체 서버에 세팅된 MySQL 8.4 를 일일 백업하는 방법에 대해 기술하겠습니다.
목적
개발서버용으로 클라우드가 아닌 별도 서버세팅을 진행해야하는 경우가 생겼습니다.
임시로 필요한 경우도 있고, 뭐 회사 사정이나 상황에 따라 필요한 경우가 있는데 제 경우가 그렇습니다.
MySQL8.4 에 대한 세팅은 아래 글을 통해 확인하실 수 있습니다.
https://god-logger.tistory.com/191
Ubuntu 24.04 (24.04.2) 에서 MySQL 8.4 (8.4.6) 설치 가이드
MySQL 설치요즘은 도커(Docker)나 쿠버네티스(Kubernetes) 같은 컨테이너 기반 배포가 대세를 이루고 있습니다.개발자들 사이에서도 이러한 빠르고 유연한 배포 방식이 당연시되고 있죠.컨테이너를 사
god-logger.tistory.com
구현 사항
구현 사항은 아래와 같습니다.
- 30일이 지난 백업 덤프 파일은 삭제
- 일일별 덤프파일 생성되며 시간은 오전12시에 하루 한번만 덤프
당연히 상황에 따라 스크립트를 수정하여 하루한번이 아닌 여러번도 진행이 가능합니다.
스크립트
LLM의 도움을 받아 손쉽게 쉘스크립트를 작성하였습니다.
파일위치 : /usr/local/bin/mysql_backup.sh
#!/bin/bash
# ----------------------------
# 설정
# ----------------------------
DB_NAME="데이터베이스명" # 백업할 DB 이름
DB_USER="계정명" # MySQL 사용자
DB_PASS="비밀번호" # MySQL 비밀번호
BACKUP_DIR="/sqlDump" # 백업 저장 디렉토리
RETENTION_DAYS=30 # 보관 기간 (일)
# ----------------------------
# 날짜별 파일 이름
# ----------------------------
DATE=$(date +%F) # YYYY-MM-DD
BACKUP_FILE="$BACKUP_DIR/dump_$DATE.sql"
# ----------------------------
# 백업 수행
# ----------------------------
mysqldump -u $DB_USER -p$DB_PASS $DB_NAME > "$BACKUP_FILE"
if [ $? -eq 0 ]; then
echo "$(date '+%F %T') - Backup successful: $BACKUP_FILE"
else
echo "$(date '+%F %T') - Backup FAILED!"
fi
# ----------------------------
# 오래된 백업 삭제
# ----------------------------
find "$BACKUP_DIR" -type f -name "dump_*.sql" -mtime +$RETENTION_DAYS -exec rm -f {} \;
echo "$(date '+%F %T') - Old backups older than $RETENTION_DAYS days deleted."
일부러 바로 실행할 수 있도록 usr/local/bin에 두었지만, 제 경우 Proxmox 로 만든 컨테이너이기에 루트권한으로만 접속되어 사용한거지 일반적으로 꼭 /usr/local/bin에 넣을 필욘 없습니다.
크론탭을 통해 정해진 시간마다 스크립트가 실행되도록 수정하겠습니다.
crontab -e
가장 맨 마지막에 아래와 같이 수정합니다.
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
0 0 * * * /usr/local/bin/mysql_backup.sh >> /var/log/mysql_backup.log 2>&1
결과에 대한 로그(stdout,stderr)는 /var/log/mysql_backup.log에 기록됩니다.
'데이터베이스 > Mysql' 카테고리의 다른 글
| Ubuntu 24.04 (24.04.2) 에서 MySQL 8.4 (8.4.6) 설치 가이드 (0) | 2025.03.05 |
|---|---|
| [Docker] 우분투 기반 컨테이너에서 Mysql 8.0 client 설치 (0) | 2024.09.03 |
| innodb: unable to lock ./ibdata1 error: 11 해결 방안 (1) | 2024.08.23 |
| Mysql 8.0 Public key retrieval is not allowed 이슈 (1) | 2023.12.06 |