在rails中用backup数据备份-备份篇

###1.安装backup.gem ```ruby backup.gem

Install the latest 4.x version

$ gem install backup -v '~> 4.0' --no-rdoc --no-ri


###2.生成backup的脚本

* 把这个备份放在rails的config目录下了!

* 下面的生成脚本是:mongodb备份、gzip打包,保在dropbox上,成功后发mail通知!
```ruby backup generate:model
$ cd rails_app
$ backup generate:model --trigger my_backup \
    --config-file='./config/backup/my_config.rb' \ #生成目录
    --databases="mongodb" \ #备份的DB mongodb, postgresql, mysql, redis ...
    --storages="dropbox" \  #存储支持 CloudFiles Dropbox Ninefold S3 FTP SCP SFTP RSync Local
    --compressor="gzip" \   #是否压缩
    --notifiers="mail"      #是否mail通知,当然也可用hipchat

# Generated configuration file: 'rails_app/config/backup/my_config.rb'.
# Generated model file: 'rails_app/config/backup/models/my_backup.rb'.

###3.修改配置文件,进行调试

打开config/backup/models/my_backup.rb文件

只留MongoDB与Gzip的脚本,其它的都注掉。然后跑下脚本看看OK不!如下:

```ruby config/backup/models/my_backup.rb Model.new(:my_backup, 'Description for my_backup') do database MongoDB do |db| db.name = "my_database_name" #填上db的名字 #db.username = "my_username" #如果没有把这句话注掉或删掉 #db.password = "my_password" #如果没有把这句话注掉或删掉 db.host = "localhost" db.port = 5432 #这里的5432端口要注意下,默认的mongo的端口应是 27017 db.ipv6 = false db.only_collections = ["only", "these", "collections"] db.additional_options = [] db.lock = false db.oplog = false end

## # Gzip [Compressor] # compress_with Gzip end



###4.运行脚本
```ruby
$ cd rails_app
$ backup perform -t my_backup -c 'config/backup/my_config.rb'

显示这些就成功啦!

ref:

Comments