Rackspaceのクラウドに毎日バックアップする方法
会社が火事になったらサーバのとなりにある
バックアップストレージも死んでしまいます。
サーバが死んだ時のことを考えるとおっかないので、
クラウドにバックアップしています。
Amazonでもいいのですが、私はRackspaceのサーバが好きなので、
ストレージもRackspaceのものを使っています。
ストレージの価格はAmazonS3とあまり変わらないと思います。
さて、今回は社内のRedmineをダンプしてRackspaceに
毎日アップロードするという仮定です。
まずRackspaceを契約
とにかく契約してください。w
http://www.rackspacecloud.com/
から契約出来ます。安いので恐れることなかれ。
そこそこやっても数ドルです。
そしてAPIアクセスキーを取得してください。
Your AccountのAPI Accessで取得出来ます。
また、Hosting->Cloud Fileのタブに行き、
Add Containerでコンテナを作ります。
ディレクトリみたいなもんです。
バックアップするやつを作っておきます。
バックアップのスクリプトを準備
今回はRedmineをPostgreSQLで作成しているという仮定です。
という流れです。
#!/usr/bin/env ruby require 'rubygems' require 'cloudfiles' require 'fileutils' require 'tmpdir' CloudContainer="redmine" CloudUser="xibbar" CloudApiKey="abcdxxxxxxxxxxxxxxxxx" Password="passwordforzip" cf=CloudFiles::Connection.new(CloudUser,CloudApiKey) container=cf.container(CloudContainer) tmpdir=File.join(Dir.tmpdir,"bk2cloudfiles") pwd=Dir.pwd redmine_dir="/var/www/rails/redmine" dbname="redmine" FileUtils.rm_rf(tmpdir) FileUtils.mkdir_p(tmpdir) Dir.chdir(tmpdir) time=Time.now timestamp=time.strftime("%Y%m%d") datafile="#{timestamp}-#{dbname}.pgsql" system("pg_dump -O #{dbname} > #{datafile}") FileUtils.cp_r(redmine_dir+"/files",".",:preserve=>true) f=open("|zip -r -P #{Password} - .","r") zipdata=f.read f.close # open("/tmp/redmine.zip","w"){|f|f.write(zipdata)} if cloud_object=container.create_object("#{timestamp}-#{dbname}.zip") if cloud_object.write(zipdata) puts "upload successfully: #{timestamp}-#{dbname}.zip" else STDERR.write "upload failed: #{timestamp}-#{dbname}.zip" end else STDERR.write "upload failed: #{timestamp}-#{dbname}.zip" end
これをcronに登録しておきます。
30 4 * * * /home/redmine/backup_redmine.rb
バックアップしているユーザーのメールをaliasで自分宛にしておくと、
毎日メールが飛んでくるので安心です。
毎日ローカルじゃない場所にバックアップできるってのは
会社が火事になってもデータは戻せるので安心です。