- #首先,安装backup:
- $ gem install backup
- #在项目目录下执行:
- $ backup generate --databases='mysql' --archives --storages='ftp' --compressors='gzip' --path='config'
把config目录下生成的config.rb重命名为backup.rb,我的配置如下:
- 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
- archive :my_archive do |archive|
- archive.add File.expand_path("./public/system/")
- end
- database MySQL do |db|
- db.name = config[RAILS_ENV]["database"]
- db.username = config[RAILS_ENV]["username"]
- db.password = config[RAILS_ENV]["password"]
- db.host = config[RAILS_ENV]["host"]
- db.port = config[RAILS_ENV]["port"]
- db.socket = config[RAILS_ENV]["socket"]
- db.skip_tables = ['sessions']
- db.additional_options = ['--quick', '--single-transaction']
- end
- store_with FTP do |server|
- server.username = 'test'
- server.password = 'test'
- server.ip = '192.168.1.101'
- server.port = 21
- server.path = '~/backups/'
- server.keep = 5
- end
- compress_with Gzip do |compression|
- compression.best = true
- compression.fast = false
- end
- end
说明:
1、public/system/下是由paperclip上传的图片
2、FTP的配置信息最好也像数据库的配置那样,配置在另外一个yml文件中,并且这个文件不要放入版本控制系统中,我这只是一个测试所以无所谓
在lib/tasks目录下新建bakcup.rake如下:
- namespace :db do
- desc "My Backup"
- task :backup do
- sh "backup perform --trigger my_backup --config_file 'config/backup.rb'"
- end
- end
在项目根目录下执行这条命令进行备份:
- $ rake db:backup
配合whenever 自动备份效果更佳!