Amazon EC2/S3を使ってみた - 5.S3をファイルシステムとして扱う

by Christopher Chan


さて、なんだかすっかりシリーズ化してきてしまいました。

今日は、Amazon EC2からS3をファイルシステムとしてマウントする方法を紹介します。


Amazon EC2ではインスタンスを停止させてしまうと、保存したディスクの内容が失われてしまうことは先日のエントリ(d:id:rx7:20080425:p3)で書いた通りで、一般的にはS3といったストレージに必要なデータを運用で保存させる必要が出てきます。


で、世には色々とS3へのバックアップツールがあり、以下のページのリストが参考になりますが、今は"S3Sync"あたりがメジャーなところでしょうか。

  • s3sync.rb is written in Ruby as a sort of rsync clone to replace the perl script s3sync which is now abandonware. Given that I already use rsync for much of my backup system, this is highly appealing.
  • Backup Manager appears to now have S3 support as of version 0.7.3. It's a command-line tool for Linux (and likely other Unix-like systems).
  • s3DAV isn't exactly a backup tool. It's provides a WebDAV front-end (or "virtual filesystem") to S3 storage, so you could use many other backup tools with S3. Recent versions of Windows and Mac OS have WebDAV support built-in. Java is required for s3DAV.
  • S3 Backup is an Open Source tool for backing up to S3. It's currently available only for Windows. Mac and Linux versions appear to be planned. The UI is built on wxWidgets.
  • duplicity is a free Unix tool that uses S3 and the librsync library. It is written in Python but not considered suitable for backing up important data quite yet.
  • S3 Solutions is a list of other S3 related tools on the Amazon Developer Connection.
  • Brackup is a backup tool written by Brad Fitzpatrick (of LiveJournal, SixApart, memcached, perlbal, etc...). It's written in Perl, fairly new, and doesn't have a lot in the way of documentation yet.
  • Jungle Disk provides clients for Mac, Windows, and Linux. It also offers a local WebDAV server.

A List of Amazon S3 Backup Tools (by Jeremy Zawodny)


で、上記のリストには含まれていないのですが、先日MOONGIFTさんのところで紹介されていた「s3fs」はS3をファイルシステムとして扱うことができるとのこと。

各種ライブラリは存在するが、最も使い勝手の良い方法はこれだろう。ファイルシステム並みに使うのだ。

今回紹介するオープンソース・ソフトウェアはs3fs、Amazon S3向けのFUSEだ。

FUSEを使うことで、通常のファイルシステムと同じ感覚でAmazon S3が利用できるようになる。インスタンスを落とすことで消失すると困るデータはs3fsを使ってマウントしたファイルシステムに入れるか、定期的なバッチでコピーしたりすれば良い。

MOONGIFT: >> Amazon S3をマウントする「s3fs」:オープンソースを毎日紹介


これは便利そう!ってことで早速試してみた。

"s3fs"のインストール

もちろんですが、EC2の仮想サーバ上での作業です。

今回は、Amazon EC2の公式イメージであるFedora8(ec2-public-images/fedora-8-i386-base-v1.06.manifest.xml)上にインストールしてみました。

尚、FUSEなので、fuse-develパッケージが必要ですが、上記のイメージには既にインストールされていました。

# wget http://s3fs.googlecode.com/files/s3fs-r130-source.tar.gz
# tar zxvf s3fs-r130-source.tar.gz

まずは、ダウンロード&解凍。

# cd s3fs
# make
Package libcurl was not found in the pkg-config search path.
Perhaps you should add the directory containing `libcurl.pc'
to the PKG_CONFIG_PATH environment variable
No package 'libcurl' found
make: xml2-config: Command not found
g++ -ggdb -Wall -D_FILE_OFFSET_BITS=64 -I/usr/include/fuse  -pthread -L/lib -lfuse -lrt -ldl     -lcrypto s3fs.cpp -o s3fs
make: g++: Command not found
make: *** [all] Error 127

g++がないってさ・・・。

# yum install gcc-c++

yumでgcc-c++パッケージをインストール。

# make
Package libcurl was not found in the pkg-config search path.
Perhaps you should add the directory containing `libcurl.pc'
to the PKG_CONFIG_PATH environment variable
No package 'libcurl' found
make: xml2-config: Command not found
g++ -ggdb -Wall -D_FILE_OFFSET_BITS=64 -I/usr/include/fuse  -pthread -L/lib -lfuse -lrt -ldl     -lcrypto s3fs.cpp -o s3fs
s3fs.cpp:34:23: error: curl/curl.h: No such file or directory
s3fs.cpp:35:27: error: libxml/parser.h: No such file or directory
s3fs.cpp:36:25: error: libxml/tree.h: No such file or directory
s3fs.cpp:321:25: error: openssl/bio.h: No such file or directory
s3fs.cpp:322:28: error: openssl/buffer.h: No such file or directory
s3fs.cpp:323:25: error: openssl/evp.h: No such file or directory
s3fs.cpp:324:26: error: openssl/hmac.h: No such file or directory
s3fs.cpp:456:25: error: openssl/md5.h: No such file or directory
s3fs.cpp:118: error: 'CURL' was not declared in this scope
s3fs.cpp:118: error: template argument 1 is invalid
s3fs.cpp:118: error: template argument 2 is invalid
s3fs.cpp:118: error: invalid type in declaration before ';' token
s3fs.cpp:121: error: expected initializer before '*' token
s3fs.cpp:119: warning: 'curl_handles_lock' defined but not used
make: *** [all] Error 1

再度コンパイル・・・も、まだ色々足りないみたいで、ずらりとerrorが出ちゃいました。

# yum install curl-devel
# yum install libxml2-devel
# yum install openssl-devel

まぁ、、、足りないものを順番にyumで突っ込んでいくだけなんですが。足りない系のエラーは分かりやすいから良いですね。

# make install
g++ -ggdb -Wall -D_FILE_OFFSET_BITS=64 -I/usr/include/fuse  -pthread -L/lib -lfuse -lrt -ldl    -lcurl   -I/usr/include/libxml2 -lxml2 -lz -lm -lcrypto s3fs.cpp -o s3fs
s3fs.cpp:396: warning: 'size_t readCallback(void*, size_t, size_t, void*)' defined but not used
ok!
cp -f s3fs /usr/bin

さて、"ok!"と出たので、今度は上手くいった模様です。"/usr/bin/s3fs"が存在していればインストール完了!

早速、使ってみる

s3fs上にマウントするための領域を作りましょう。作り方はこの辺(d:id:rx7:20080423:p1)を参考にして下さい。
以下の例では、"rx7/buckup"というバケットがあるものとします。

# mkdir /s3/backup

任意のマウント先となるディレクトリを作成して、、、

# s3fs rx7/backup -o accessKeyId=${Access Key ID} -o secretAccessKey=${Secret Access Key} /s3/backup

こんな感じでマウントします。"rx7/backup"の部分はあくまで例ですので、お作りになったバケット名にあわせてください。


あとは、cpやmv等でファイルを書き込んでみると、S3へファイルが作成されていることが確認できるかと思います。もちろん削除(rmコマンド等)もOK。
あ、ちなみにマウント先は、"ls"コマンドで中身が確認できないので注意が必要です。


公式ドキュメントを読んでいるとローカル内にキャッシュを作って高速化することも可能みたい。

あとは、シンボリックリンク等、まだサポートしていない部分もあって、まだ機能制限付きのようですが、今後の発展に期待ですね。

参考

s3fs - Google Code
http://code.google.com/p/s3fs/
FuseOverAmazon - s3fs - Google Code
http://code.google.com/p/s3fs/wiki/FuseOverAmazon
MOONGIFT:>> Amazon S3をマウントする「s3fs」:オープンソースを毎日紹介
http://www.moongift.jp/2008/04/s3fs/




まとめ

クラウドAMAZON EC2/S3のすべて (ITpro BOOKs)

クラウドAMAZON EC2/S3のすべて (ITpro BOOKs)