Skip to content

Instantly share code, notes, and snippets.

@alvin2ye
Forked from RobinWu/backup.rake
Last active September 24, 2015 19:58
Show Gist options
  • Save alvin2ye/801680 to your computer and use it in GitHub Desktop.
Save alvin2ye/801680 to your computer and use it in GitHub Desktop.
backup.rake for rails 1.2.x
require 'fileutils'
require 'enumerator' # support rails 1.2.5
require 'date'
namespace :backup do
desc 'crontab -e'
task :crontab do
puts <<-CRONTAB
m(0-59) h(0-23) d(1-31) m(1-12) w(0-6 0=Sunday) command
0 3 * * * cd /var/www/<project> && /usr/bin/rake backup:log:analyze APIKEY=* RAILS_ENV=production > /dev/null 2>&1
5 3 * * * cd /var/www/<project> && /usr/bin/rake backup:log:do RAILS_ENV=production > /dev/null 2>&1
9 3 * * * cd /var/www/<project> && /usr/bin/rake backup:db:mysql RAILS_ENV=production > /dev/null 2>&1
20 3 * * * cd /var/www/<project> && /usr/bin/rake backup:db:clear DAYS_AGO=7 > /dev/null 2>&1
CRONTAB
end
desc 'backup log'
namespace :log do
desc 'rake backup:log:analyze APIKEY=* RAILS_ENV=production'
task :analyze => :environment do
backup_path = File.join(RAILS_ROOT, 'backup', 'report', "#{Date.today.year}-#{Date.today.month}")
FileUtils.mkdir_p(backup_path) unless File.exist?(backup_path)
filename = File.join(backup_path, "#{Time.now.strftime("%Y%m%d%H%M%S")}.txt")
case (defined?(Rails.version) ? Rails.version : RAILS_GEM_VERSION)
when /3\.\d\.\d/
request_num = `cat log/#{RAILS_ENV}.log | grep "Started" | wc -l`
request_num_by_ip = `cat log/#{RAILS_ENV}.log | grep "Started" | awk '{print $5}' | sort | uniq -c`
when /[2|1]\.\d\.\d/
request_num = `cat log/#{RAILS_ENV}.log | grep "Processing" | wc -l`
request_num_by_ip = `cat log/#{RAILS_ENV}.log | grep "Processing " | awk '{print (match($4, /^js/) ? $6 : $4)}' | sort | uniq -c`
else
raise "Log analyze not support Rails #{Rails.version} version"
end
cmd = <<-CMD
echo "Report Version: V1.0\n所有访问数量:#{request_num}每个IP的访问次数:\n#{request_num_by_ip}" > #{filename}
CMD
`#{cmd}`
unless ENV['APIKEY'].blank?
cmd = "curl -X POST -F 'report_file=@#{filename}' http://report.agideo.com:8002/api?key=#{ENV['APIKEY']}"
`#{cmd}`
end
end
desc 'rake backup:log:all'
task :all do
backup_path = File.join(RAILS_ROOT, 'backup', 'log', "#{Date.today.year}-#{Date.today.month}")
FileUtils.mkdir_p(backup_path) unless File.exist?(backup_path)
filename = File.join(backup_path, "log_#{Time.now.strftime("%Y%m%d%H%M%S")}.tar.gz")
s3_folder = "f22/#{Date.today.year}-#{Date.today.month}"
basename = File.basename(filename)
s3_file = "#{s3_folder}/#{basename}"
cmd = <<-CMD
tar -czvf #{filename} log/*.log
CMD
`#{cmd}`
cmd = "s3cmd put #{filename} s3://bak-ascenta-wiki/#{s3_file}"
puts cmd
`#{cmd}`
Rake::Task["log:clear"].invoke if File.size?(filename)
end
end
desc 'backup db'
namespace :db do
desc 'rake backup:db:mysql RAILS_ENV=production'
task :mysql => :environment do
backup_path = File.join(RAILS_ROOT, 'backup', 'db', "#{Date.today.year}-#{Date.today.month}")
FileUtils.mkdir_p(backup_path) unless File.exist?(backup_path)
tmp_filename = File.join(RAILS_ROOT, 'backup', 'db', 'tmp.sql')
filename = File.join(backup_path, "db_#{RAILS_ENV}_#{Time.now.strftime("%Y%m%d%H%M%S")}.tar.gz")
s3_folder = "f22/#{Date.today.year}-#{Date.today.month}"
basename = File.basename(filename)
s3_file = "#{s3_folder}/#{basename}"
# 获取数据库信息
db_config = YAML.load_file("#{RAILS_ROOT}/config/database.yml")[RAILS_ENV].symbolize_keys
net_buffer_length = ActiveRecord::Base.connection.execute("SHOW VARIABLES LIKE 'net_buffer_length';").to_enum.to_a[0][1]
max_allowed_packet = ActiveRecord::Base.connection.execute("SHOW VARIABLES LIKE 'max_allowed_packet';").to_enum.to_a[0][1]
cmd = <<-CMD
mysqldump -u#{db_config[:username]} -p'#{db_config[:password]}' \
--skip-opt --create-option --set-charset --default-character-set=utf8 \
-e --max_allowed_packet=#{max_allowed_packet} --net_buffer_length=#{net_buffer_length} #{db_config[:database]} > #{tmp_filename}
tar -czvf #{filename} backup/db/tmp.sql
rm -f #{tmp_filename}
CMD
`#{cmd}`
cmd = "s3cmd put #{filename} s3://bak-ascenta-wiki/#{s3_file}"
puts cmd
`#{cmd}`
end
desc 'backup clear mysql, rake backup:db:clear DAYS_AGO=*, default is 7 days'
task :clear do
backup_path = File.join(RAILS_ROOT, 'backup', 'db')
days_ago = ENV['DAYS_AGO']
days_ago = 7 unless days_ago
cmd = "find #{backup_path} -mtime +#{days_ago} -type f -exec rm {} \\;"
`#{cmd}`
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment