最近开始学习备份,用的是 mysql 数据库,asset 方面暂时好像也没有什么需要备份的。
想请教一下,大家都用什么备份方案呢?是直接写 mysql 的脚本之类,还是用 backup+whenever+dropbox?
看到@Rei这篇《用 Backup 和 Whenever 进行备份》,但 dropbox 的话,难道不会被墙吗?我自己的电脑就老是连不上 dropbox。
S3 吧
namespace :db do
namespace :data do
desc "Dump the database data to db/data/[RAILS_ENV]_dump.sql. Specify another file with DB_DUMP=db/data/my_dump.sql"
task :dump => [:environment, :load_config] do
FileUtils.mkdir_p("#{Rails.root}/db/data")
filename = ENV['DB_DUMP'] || "#{Rails.root}/db/data/#{Rails.env}_dump.sql"
config = ActiveRecord::Base.configurations[Rails.env]
case config["adapter"]
when /mysql/
ActiveRecord::Base.establish_connection(config)
File.open(filename, "w+") do |f|
if config["password"].blank?
f << `mysqldump -h#{config["host"] || "localhost"} -u#{config["username"]} -q --single-transaction #{config["database"]}`
else
f << `mysqldump -h#{config["host"] || "localhost"} -u#{config["username"]} -p#{config["password"]} -q --single-transaction #{config["database"]}`
end
end
when /sqlite/
ActiveRecord::Base.establish_connection(config)
File.open(filename, "w+") do |f|
f << `sqlite3 #{config["database"]} .dump`
end
else
raise "Task not supported by '#{config['adapter']}'"
end
end
desc "Backup the database to Amazon S3"
task :backup => :dump do
timestamp = Time.now.strftime('%Y_%m_%d_%H_%M_%S')
backup_filename = "#{Rails.env}_dump_#{timestamp}.sql.gz"
sql_file = "#{Rails.root}/db/data/#{Rails.env}_dump.sql"
Zlib::GzipWriter.open("#{Rails.root}/db/data/#{backup_filename}") do |gz|
gz.write File.read(sql_file)
end
storage = Fog::Storage.new(:provider => 'AWS',
:aws_access_key_id => ENV['AWS_S3_ACCESS_KEY_ID'],
:aws_secret_access_key => ENV['AWS_S3_SECRET_ACCESS_KEY'])
storage.put_object(APP_CONFIG['s3_backup_bucket'],
backup_filename,
File.read("#{Rails.root}/db/data/#{backup_filename}"),
'x-amz-acl' => 'private')
File.delete("#{Rails.root}/db/data/#{backup_filename}") # Clean up our leftovers
end
end
end
#27 楼 @chairy11 刚看到有这个 gem http://rubygems.org/gems/backup_upyun
我自己 blog 用的是七牛,公司用的是阿里云。手头上没有又拍云的帐号,不确定能不能用。你试试看~