Is there a rake task to back up data in your database?

Is there a rake task to back up data in your database?

I already have a backup of my schema, but I want to back up the data. This is a small MySQL database.

+4
source share
7 answers

The script below is a simplified version taken from eycap , in particular from 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 

Edit: Yes, I guess I missed the idea that you were looking for a rake task, not a capistrano task, but I don't have a rake on hand, sorry.

+5
source

I don't have a rake task to backup my MySQL db, but I wrote a script in Ruby to do this only for my WordPress DB:

 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 

You should be able to take this and do a little cropping to insert your username / password / dbname in order to get it and work for you. I put it in my crontab to work every day as well, and there should not be too much work to convert it to run as a rake task, as it already has Ruby code (maybe a good training exercise).

Tell us how this happens!

+1
source

There are a few solutions already on google. Am I going to suggest that you use activerecord as your orme?

If you use rails, you can look at the Rakefile that it uses for activerecord in \ ruby ​​\ lib \ ruby ​​\ gems \ 1.8 \ gems \ rails-2.0.2- \ lib \ tasks \ database.rake. This gave me a lot of information on how to extend the overall Rakefile.

You can take the capistrano tasks that thelsdj provides and add them to your rake file. Then modify it a bit so that it uses an activerecord connection to the database.

+1
source

There is a plugin called "mysql tasks" just for it. This is just a rakefile - I found it very easy to use.

+1
source

Just in case, when people are still surfing for solutions, we are currently using the ar_fixtures plugin to backup our db, as well as parts of the solution.

It provides rake db:fixtures:dump tasks. This spills everything in YAML into the test / fixtures, so it can be loaded again with db:fixtures:load .

We use this for backups before each click of a button on production. We also used this when switching from sqlite3 to Postgres, which is very subtle, because the incompatibility between SQL dialects is mostly hidden.

All the best, D

+1
source

Be sure to add the --routines option to mysqldump if you have any stored procs in your database so that they also support them.

0
source

There is my rake task for mysql backup and cyclic cyclic backup.

 #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 
0
source

Source: https://habr.com/ru/post/1276433/


All Articles