Last active
February 2, 2016 07:08
-
-
Save nakamura244/37b89a2fa1f7e632a2c9 to your computer and use it in GitHub Desktop.
バックアップ用途にAWSのs3を使う時の小ネタ ref: http://qiita.com/nakamura-tsuyoshi/items/b7eebd33119c73490687
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# 終了ステータスが0以外の時は処理を中断してNGを出力する | |
# jenkinsのjobで実行予定で異常に気づけるように | |
set -e | |
trap 'echo NG' ERR | |
# n世代のバックアップを保存 | |
MAX_SAVE_CNT=7 | |
BACKUP_NAME=`date +"%Y-%m%d-%H%M%S".sql` | |
# BK用途のslaveサーバからmysqldumpでバックアップを取得 | |
# サイズが大きくなればserviceをstopしてバイナリーバックアップに切り替える | |
ssh backup@<slave database hostname> /usr/local/mysql/bin/mysqldump -uroot <database name> > <bk dir>$BACKUP_NAME | |
tar cvzf $BACKUP_NAME.tar.gz $BACKUP_NAME | |
# s3へ格納 | |
s3cmd put -r $BACKUP_NAME.tar.gz s3://<BUCKET_NAME>/<BUCKET_PATH>/ | |
file_count=`ls -1 | grep sql.tar.gz | wc -l` | |
if [ $file_count -gt $MAX_SAVE_CNT ] | |
then | |
# s3にある7世代以降のバックアップを削除 | |
s3cmd del -r s3://<BUCKET_NAME>/<BUCKET_PATH>/`ls -1 | grep sql.tar.gz | head -1` | |
# localのバックアップファイルも削除 | |
rm -rf `ls -1 | grep sql.tar.gz | head -1 | xargs rm -rf` | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
yum info –enablerepo=epel s3cmd | |
. | |
. | |
. | |
Name : s3cmd | |
Arch : x86_64 | |
Version : 1.0.0 | |
Release : 4.1 | |
Size : 296 k |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
yum --enablerepo epel install s3cmd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
s3cmd --configure | |
Access Key: xxx | |
Secret Key: xxx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[root@hostname]# s3cmd ls | |
2015-02-13 03:26 s3://xxxxxxxx |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment