/home/by-natures/dev*

データ界隈で働くエンジニアとしての技術的なメモと、たまに普通の日記。

AmazonS3 を使ってデータをバックアップしてみた

このブログはさくらインターネットの仮想サーバを使っているのですが、バックアップはその仮想サーバ内で cron で回しているだけでした。この前 Hadoop の勉強のために Amazon S3 を設定していたので、せっかくだからとバックアップを S3 に突っ込むようにしてみました。

s3cmd のダウンロードと設定

ローカル環境で Amazon S3 のコマンドが利用できるようにします。最新版のソースはこちらから: http://sourceforge.net/projects/s3tools/files/s3cmd/

[root@bynatures.net] # unzip s3cmd-1.1.0-beta3.zip
[root@bynatures.net] # cd s3cmd-1.1.0-beta3
[root@bynatures.net] # python setup.py install

この後、Amazon S3 をコマンドで利用するための初期設定を行います。初期設定はインタラクティブにアクセスキーやシークレットアクセスキーを求められますので、事前にsecurityCredentials のページでアクセスキーとシークレットアクセスキーを確認しておきます:Amazon Web Services: SecurityCredentials ハイライトされている行が、ユーザが入力を求められている行です。アクセスキーとシークレットアクセスキーの他、HTTPSで通信するか、HTTPで通信するかなど質問されます。私の場合は個人のデータベースですので、そのまま HTTP で進めました。

[root@bynatures.net] # s3cmd --configure

Enter new values or accept defaults in brackets with Enter.
Refer to user manual for detailed description of all options.

Access key and Secret key are your identifiers for Amazon S3
Access Key: (アクセスキーを入れる)
Secret Key: (シークレットアクセスキーを入れる)

Encryption password is used to protect your files from reading
by unauthorized persons while in transfer to S3
Encryption password: (適当に入れる)
Path to GPG program [/usr/bin/gpg]:(そのままEnter)

When using secure HTTPS protocol all communication with Amazon S3
servers is protected from 3rd party eavesdropping. This method is
slower than plain HTTP and can't be used if you're behind a proxy
Use HTTPS protocol [No]: (そのままEnter)

On some networks all internet access must go through a HTTP proxy.
Try setting it here if you can't conect to S3 directly
HTTP Proxy server name:

New settings:
  Access Key: (上で入力したアクセスキーが表示される)
  Secret Key: (上で入力したシークレットアクセスキーが表示される)
  Encryption password: (上で入力した暗号化パスワードが表示される)
  Path to GPG program: /usr/bin/gpg
  Use HTTPS protocol: False
  HTTP Proxy server name:
  HTTP Proxy server port: 0

Test access with supplied credentials? [Y/n] (Y と入力)
Please wait, attempting to list all buckets...
Success. Your access key and secret key worked fine :-)

Now verifying that encryption works...
Success. Encryption and decryption worked fine :-)

Save settings? [y/N] (y と入力)
Configuration saved to '/root/.s3cfg'

これで Amazon S3 を利用するコマンド s3cmd の準備が整いました。

データベースをダンプして Amazon S3 に投げ込む

あとは従来のバックアップスクリプトに、ローカルに保存した後に Amazon S3 に投げ込む処理を追加するだけです。追加したのは10行目と15行目で、10行目がAmazon S3 にダンプファイルをアップロードする処理、15行目が、7日過ぎたダンプファイルを Amazon S3 上から削除するコマンドです。 私の場合は1日1回、下記スクリプトを cron に設定しています。

#!/bin/sh
PERIOD=7
BACKUPDIR='バックアップディレクトリ'
filename=`date +%Y%m%d`

# Execute dump
mysqldump --opt --password='パスワード' -x --all-databases > $BACKUPDIR/$filename.sql
zip $DIRPATH/$filename.sql.zip $BACKUPDIR/$filename.sql
rm -f $BACKUPDIR/$filename.sql
s3cmd put -rr $BACKUPDIR/$filename.sql.zip s3://バケット名/フォルダパス/$filename.sql.zip

# Delete old backups
oldfile=`date --date "$PERIOD days ago" +%Y%m%d`
rm -f $BACKUPDIR/$oldfile.sql.zip
s3cmd del s3://バケット名/フォルダパス/$oldfile.sql.zip

認証が面倒なのかと思ったら、アクセスキーとシークレットアクセスキーがすんなり通ったので本当に簡単でした。同じサーバ内でバックアップを取られている方は、冗長化のために Amazon S3 を利用してみてはどうでしょうか。(でも、あまり大きなファイルを入れ過ぎないように。。)