是否有用于备份数据库中数据的rake任务?
我已经备份了架构,但我想备份数据。这是一个小型的MySQL数据库。
答案 0 :(得分:5)
以下脚本是摘自eycap的简化版,具体来自this file。
set :dbuser "user"
set :dbhost "host"
set :database "db"
namespace :db do
desc "Get the database password from user"
task :get_password do
set(:dbpass) do
Capistrano::CLI.ui.ask "Enter mysql password: "
end
end
task :backup_name, :only => { :primary => true } do
now = Time.now
run "mkdir -p #{shared_path}/db_backups"
backup_time = [now.year,now.month,now.day,now.hour,now.min,now.sec].join('-')
set :backup_file, "#{shared_path}/db_backups/#{database}-snapshot-#{backup_time}.sql"
end
desc "Dump database to backup file"
task :dump, :roles => :db, :only => {:primary => true} do
backup_name
run "mysqldump --add-drop-table -u #{dbuser} -h #{dbhost} -p#{dbpass} #{database} | bzip2 -c > #{backup_file}.bz2"
end
end
编辑:是的,我想我错过了你正在寻找rake任务而不是capistrano任务的观点,但我手头没有rake,对不起。
答案 1 :(得分:1)
我没有用于备份MySQL数据库的rake任务,但是我确实在Ruby中编写了一个脚本来为我的WordPress数据库执行此操作:
filename = 'wp-config.php'
def get_db_info(file)
username = nil
password = nil
db_name = nil
file.each { |line|
if line =~ /'DB_(USER|PASSWORD|NAME)', '([[:alnum:]]*)'/
if $1 == "USER"
username = $2
elsif $1 == "PASSWORD"
password = $2
elsif $1 == "NAME"
db_name = $2
end
end
}
if username.nil? || password.nil? || db_name.nil?
puts "[backup_db][bad] couldn't get all needed info"
exit
end
return username, password, db_name
end
begin
config_file = open("#{filename}")
rescue Errno::ENOENT
puts "[backup_db][bad] File '#{filename}' didn't exist"
exit
else
puts "[backup_db][good] File '#{filename}' existed"
end
username, password, db_name = get_db_info(config_file)
sql_dump_info = `mysqldump --user=#{username} --password=#{password} #{dbname}`
puts sql_dump_info
您应该能够对此进行一些温和的修剪,并输入您的用户名/密码/ dbname来启动并为您工作。我把它放在我的crontab中以便每天运行,并且将它转换为rake任务并不应该太多工作,因为它已经是Ruby代码(也可能是一个很好的学习练习)。
告诉我们它是怎么回事!
答案 2 :(得分:1)
Google上已有a few solutions。我猜你正在使用activerecord作为你的orm?
如果你正在运行rails,那么你可以在\ ruby \ lib \ ruby \ gems \ 1.8 \ gems \ rails-2.0.2- \ lib \ tasks \ database.rake中查看它用于activerecord的Rakefile。这给了我很多关于如何扩展泛型Rakefile的信息。
您可以执行thelsdj提供的capistrano任务,并将其添加到您的rake文件中。然后稍微修改它,以便它使用与数据库的activerecord连接。
答案 3 :(得分:1)
有一个名为“mysql tasks”的插件,只是google for it。它只是一个rakefile - 我发现它很容易使用。
答案 4 :(得分:1)
万一人们还在寻找解决方案,我们目前使用ar_fixtures插件备份我们的数据库,以及解决方案的一部分。
它提供了rake db:fixtures:dump
任务。这会将YAML中的所有内容吐出到测试/装置中,因此可以使用db:fixtures:load
再次加载它。
我们使用此功能在每个功能推送到生产之前进行备份。我们在从sqlite3迁移到Postgres时也使用了这一点 - 由于SQL方言之间的不兼容性大部分是隐藏的,因此非常有用。
一切顺利,D
答案 5 :(得分:0)
如果您的数据库中有任何存储过程,请确保将“--routines”参数添加到mysqldump,以便它们备份它们。
答案 6 :(得分:0)
我的rake任务是备份mysql,并循环旋转备份。
#encoding: utf-8
#require 'fileutils'
namespace :mls do
desc 'Create of realty_dev database backup'
task :backup => :environment do
backup_max_records = 4
datestamp = Time.now.strftime("%Y-%m-%d_%H-%M")
backup_dir = File.join(Rails.root, ENV['DIR'] || 'backups', 'db')
backup_file_name = "#{datestamp}_#{Rails.env}_dump.sql"
backup_file_path = File.join(backup_dir, "#{backup_file_name}")
FileUtils.mkdir_p(backup_dir)
#database processing
db_config = ActiveRecord::Base.configurations[Rails.env]
system "mysqldump -u#{db_config['username']} -p#{db_config['password']} -i -c -q #{db_config['database']} > #{backup_file_path}"
raise 'Unable to make DB backup!' if ($?.to_i > 0)
# sql dump file compression
system "gzip -9 #{backup_file_path}"
# backup rotation
dir = Dir.new(backup_dir)
backup_all_records = dir.entries.sort[2..-1].reverse
puts "Created backup: #{backup_file_name}.gz"
#redundant records
backup_del_records = backup_all_records[backup_max_records..-1] || []
# backup deleting too old records
for backup_del_record in backup_del_records
FileUtils.rm_rf(File.join(backup_dir, backup_del_record))
end
puts "Deleted #{backup_del_records.length} old backups, #{backup_all_records.length - backup_del_records.length} backups available"
puts "Backup passed"
end
end
=begin
run by this command: " rake db:backup RAILS_ENV="development" "
=end