Backup to various strage(Amazon S3,Dropbox and so on) compressed and notice by mail or Twitter.
In this article I'll show you the way to backup database with Rails.
Supported Databases
- MySQL
- PostgreSQL
- MongoDB
- Redis
Supported Strage
- Amazon S3
- Rackspace Cloud Files
- Dropbox
- Remote server(FTP SFTP SCP RSync)
Ruby version
- Ruby 1.9.2
- Ruby 1.8.7
- Ruby Enterprise Edition 1.8.7
Installing
gem install backup
Crating configure file
backup gem is not only for Rails, so it creates configure file in ~/Backup/config.rb.
It is out of Rails app dir, so generate with '--path' option and create in rails_app_dir/config directory.
Move to Rails app dir and hit the command.
backup generate --path='config/' --databases='mysql' --storages='ftp' --compressors='gzip'
*Options--databases:database type (MySQL、PostgreSQL)
--storages:strage (Amazon S3、FTP)
--compressors:compressor type
Details:Generator
And config/config.rb will be generated.
Edit 'config.rb'
Using Backup With Rails
# # Backup # Generated Template # # For more information: # # View the Git repository at https://github.com/meskyanichi/backup # View the Wiki/Documentation at https://github.com/meskyanichi/backup/wiki # View the issue log at https://github.com/meskyanichi/backup/issues # # When you're finished configuring this configuration file, # you can run it from the command line by issuing the following command: # # $ backup -t my_backup [-c] #Set database name,password refer to 'database.yml' database_yml = File.expand_path('../config/database.yml', __FILE__) RAILS_ENV = ENV['RAILS_ENV'] || 'development' require 'yaml' config = YAML.load_file(database_yml) Backup::Model.new(:my_backup, 'My Backup') do database MySQL do |db| db.name = config[RAILS_ENV]["database"] db.username = config[RAILS_ENV]["username"] db.password = config[RAILS_ENV]["password"] db.host = "localhost" db.port = 3306 db.socket = "/tmp/mysql.sock" db.skip_tables = [] end #Refer to Databases if want to use other database. store_with FTP do |server| server.username = 'my_username' server.password = 'my_password' server.ip = '123.45.678.90' server.port = 21 server.path = '~/backups/' server.keep = 5 #number of backups end #Refer to Storages if want to use other strage.compress_with Gzip do |compression| compression.best = true compression.fast = false end end
configure 'schedule.rb' for whenever gem
whenever is a ruby gem which allow you to set up cron easier.
Backup daily at 5:00 am.
every 1.days, :at=>'05:00' do command "backup perform --trigger my_backup --config_file 'rails_app/current/config'" end*Options
--trigger the name which wrote in 'config.rb' Backup::Model.new(:my_backup, 'My Backup') do ...
--config_file path to 'config.rb'
After deploy to your production server, 'backup' will automatically backup your database at the time which you set on 'schedule.rb'.
No comments:
Post a Comment