0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

RDSのバックアップ(gzip)をS3に作成する&ハウスキープのcronを設定するスクリプト

Last updated at Posted at 2020-12-21

また触った時に忘れないための備忘録

  • 頻度:1日1回(毎朝10時)
  • 格納先:s3://hogehoge-dev/rds_backup/rds_hogehoge_db_yyyymmdd_hhmmss.sql.gz
  • S3のライフサイクル:14日前
    • 対象プレフィックス:rds_backup/

バックアップ用スクリプト

#!/bin/bash -l

HOGEHOGE_DB="hogehoge_db"
HOGEHOGE_DB_USER=""
HOGEHOGE_DB_PASS=""
HOGEHOGE_DB_HOST="hogehoge.ap-northeast-1.rds.amazonaws.com"
DUMP_FILE_NAME="rds_hogehoge_db_`date +%Y%m%d_%H%M%S`.sql.gz"

AWS_PROFILE_NAME="hogehoge-develop" # 事前にprofileを同名で作成しておくこと
S3_DIR="s3://hogehoge-dev/rds_backup/"

mysqldump -u ${HOGEHOGE_DB_USER} -p${HOGEHOGE_DB_PASS} -h ${HOGEHOGE_DB_HOST} ${HOGEHOGE_DB} | gzip > /tmp/${DUMP_FILE_NAME}

/usr/bin/aws s3 cp /tmp/${DUMP_FILE_NAME} ${S3_DIR} --profile ${AWS_PROFILE_NAME}

rm /tmp/${DUMP_FILE_NAME}

定期実行の設定

$ crontab -e
no crontab for ec2-user - using an empty one
crontab: installing new crontab

$ crontab -l
00 10 * * * /home/ec2-user/bin/save_rds_backup_to_s3 > /dev/null 2> /tmp/save_rds_backup_to_s3_error
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?