Skip to content

Instantly share code, notes, and snippets.

@jacopen
Last active June 9, 2016 22:51
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacopen/6209094 to your computer and use it in GitHub Desktop.
Save jacopen/6209094 to your computer and use it in GitHub Desktop.
gem backupを使ってみる

gem backup

これって何

その名の通り、backupを取るための便利なgem https://github.com/meskyanichi/backup

  • データのdump
    • ローカルファイルやディレクトリだけでなく、MySQL、Postgres、MongoDB、Redis、RiakからDumpできる
  • データの圧縮
    • gzip
  • バックアップ先へのアップロード
    • ローカルファイルシステム、S3、Dropbox、FTP/SFTP/SCPなど
  • バックアップ完了後の通知
    • メール、Twitter、Hipchat、Campfire、HTTP POSTなどなど

が、このgemひとつで出来る。

インストール方法

gem install backup

Gemfileに書くのはやめたほうがよい、らしい。

まずは使ってみる

ファイルのバックアップを取る

file_backup.rb

# encoding: utf-8

Backup::Model.new(:file_backup, 'Description for file_backup') do

  archive :dumped_data do |archive|
   archive.add '/path/to/backup/dump.sql'
  end

  store_with S3 do |s3|
    s3.access_key_id      = 'accesskey'
    s3.secret_access_key  = 'secretkey'
    s3.region             = 'ap-northeast-1'
    s3.bucket             = 'backup.udcp.info'
    s3.path               = '/backups'
    s3.keep               = 3
  end

  compress_with Gzip
end

backup perform -t file_backup -c file_backup.rb

ディレクトリのバックアップを取る

dir_backup.rb

# encoding: utf-8

Backup::Model.new(:dir_backup, 'Description for directory backup') do

  archive :dir do |archive|
   archive.add '/path/to/backup'
  end

  store_with S3 do |s3|
    s3.access_key_id      = 'accesskey'
    s3.secret_access_key  = 'secretkey'
    s3.region             = 'ap-northeast-1'
    s3.bucket             = 'backup.udcp.info'
    s3.path               = '/backups'
    s3.keep               = 3
  end

  compress_with Gzip
end

DBのバックアップを取る

mysql_backup.rb

# encoding: utf-8

Backup::Model.new(:mysql_backup, 'Description for mysql backup') do

  database MySQL do |db|
    db.name               = "db_a" 
    db.username           = "hoge" 
    db.password           = "fuga" 
    db.host               = "localhost" 
    db.port               = 3306
    db.socket             = "/var/lib/mysql/mysql.sock" 
    db.additional_options = ["--quick", "--single-transaction"]
  end

  store_with S3 do |s3|
    s3.access_key_id      = 'accesskey'
    s3.secret_access_key  = 'secretkey'
    s3.region             = 'ap-northeast-1'
    s3.bucket             = 'backup.udcp.info'
    s3.path               = '/backups'
    s3.keep               = 3
  end

  compress_with Gzip
end

もう一歩踏み込んだ使い方

メール通知

  notify_by Mail do |mail|
    mail.on_success           = true
    mail.on_warning           = true
    mail.on_failure           = true

    mail.delivery_method      = :sendmail
    mail.from                 = 'jacopen@example.com'
    mail.to                   = 'notification@example.com'
  end

Twitter通知

  notify_by Twitter do |tweet|
    tweet.consumer_key       = "my_consumer_key"
    tweet.consumer_secret    = "my_consumer_secret"
    tweet.oauth_token        = "my_oauth_token"
    tweet.oauth_token_secret = "my_oauth_token_secret"
  end

バックアップファイルの容量分割

split_into_chunks_of num で、num(MB)ごとにバックアップファイルが分割される。

  split_into_chunks_of 250
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment