是否有用于备份数据库中数据的rake任务?

是否有用于备份数据库中数据的rake任务?

我已经备份了我的架构,但我想备份数据。 这是一个小型的MySQL数据库。

以下脚本是从eycap获取的简化版本,特别是来自此文件 。

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,对不起。

我没有rake任务来备份我的MySQL数据库,但我确实在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代码(也可能是一个很好的学习练习)。

告诉我们它是怎么回事!

Google上已经有 一些 解决方案 。 我猜你正在使用activerecord作为你的orm?

如果您正在运行rails,那么您可以查看它用于\ ruby​​ \ lib \ ruby​​ \ gems \ 1.8 \ gems \ rails-2.0.2- \ lib \ tasks \ database.rake中的activerecord的Rakefile。 这给了我很多关于如何扩展genericsRakefile的信息。

您可以执行thelsdj提供的capistrano任务,并将其添加到您的rake文件中。 然后稍微修改它,以便它使用与数据库的activerecord连接。

那里有一个名为“mysql tasks”的插件,只是google for it。 它只是一个rakefile – 我发现它很容易使用。

为了防止人们仍然在寻找解决方案,我们目前使用ar_fixtures插件来备份我们的数据库,以及解决方案的一部分。

它提供了rake db:fixtures:dump任务。 这会将YAML中的所有内容吐出到测试/装置中,因此可以使用db:fixtures:load再次db:fixtures:load

我们在每个function推送到生产之前使用它进行备份。 我们在从sqlite3迁移到Postgres时也使用了这一点 – 这非常有用,因为大多数情况下隐藏了SQL方言之间的不兼容性。

一切顺利,D

如果数据库中有任何存储过程,请确保将“–routines”参数添加到mysqldump,以便它也备份它们。

我的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