mysqldump, AWS CLI, crontab을 이용하여 주기적으로 MySQL을 백업하는 Docker Image를 만들어 보겠습니다.
backup.sh
1 2 3
| TODAY=$(date +"%Y-%m-%d") mysqldump -h database -u $MYSQL_USER -p$MYSQL_PASSWORD $MYSQL_DATABASE > "$MYSQL_DATABASE-$TODAY.sql" aws s3 cp "$MYSQL_DATABASE-$TODAY.sql" s3://my-s3-bucket-name
|
Dockerfile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| FROM python:3.7-alpine
WORKDIR /root
RUN apk update
RUN apk add --no-cache tzdata ENV TZ='Asia/Seoul'
RUN pip3 install awscli --upgrade --user RUN ln -s /root/.local/bin/aws /usr/bin/aws
RUN apk add --no-cache mysql-client
RUN echo "0 6 * * * /root/backup.sh" | crontab -
ADD backup.sh /root/backup.sh RUN chmod +x /root/backup.sh
ENTRYPOINT crond -f -L /dev/stdout
|
Docker Command
1 2 3 4 5 6 7 8
| $ docker run -d \ -e MYSQL_USER=[MySQL User] \ -e MYSQL_PASSWORD=[MySQL Password] \ -e MYSQL_DATABASE=[MySQL Database] \ -e AWS_ACCESS_KEY_ID=[AWS Access Key ID] \ -e AWS_SECRET_ACCESS_KEY=[AWS Secret Access Key] \ -e AWS_DEFAULT_REGION=[AWS Default Region] \ mysql-backup
|