8
6

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 5 years have passed since last update.

mysqlのDBをダンプしてS3にアップロードして1週間くらいで古いのを消す

Posted at

ほとんどが、この記事のパクリインスパイア。
https://qiita.com/taiko19xx/items/215b9943c8aa0d8edcf6

↑こちらでは全DBをわーっとdumpしてるけど、DB毎にやったほうが使い勝手がいいかなーと。

AWS cli install

ここを参考にインストールしましょう。
https://docs.aws.amazon.com/ja_jp/streams/latest/dev/kinesis-tutorial-cli-installation.html

もしくはpipが入ってればこれで一発。

sudo pip install awscli

AWS cli cofigure

IAMでS3をフルアクセスできるユーザを作って、アクセスキー、シークレットをゲットします。
そしてconfigureの設定をしましょう。

aws configure
AWS Access Key ID [None]: KEEEEEYYYYYYYYY
AWS Secret Access Key [None]: 
Default region name [None]: ap-northeast-1
Default output format [None]: json

こんな感じのシェルを用意しましょう。

#!/bin/bash

BACKUP_PATH="/your/backup/dir/db_backup/"
S3_PATH="s3://hoge-aws-db-backup/"
FILE_NAME="mysql_dump_`date +%Y%m%d`.sql.gz"

AWS_CONFIG_FILE="/home/ec2-user/.aws/config"

ARRAY=(hoge_db fuga_db nyamo_db moga_db abeshi_db ohyo_db)

cd $BACKUP_PATH

for item in ${ARRAY[@]}; do
mysqldump --single-transaction -u user -ppassword $item | gzip > $item_$FILE_NAME
done

find $BACKUP_PATH -type f -name "*_mysql_dump_*.sql.gz"  -mtime +6 -daystart | xargs rm -rf

aws s3 sync $BACKUP_PATH $S3_PATH  --delete

ARRAYの中にバックアップしたいDB名を羅列します。

適当な時間にcron設定しましょう。

0 3 * * * /home/ec2-user/bin/db_backup.sh

いーじょー

参考記事)
https://qiita.com/taiko19xx/items/215b9943c8aa0d8edcf6

8
6
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
8
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?