Created
February 20, 2014 05:07
-
-
Save yoshiso/9107370 to your computer and use it in GitHub Desktop.
Mysqlバックアップrakeタスクを作ってwheneverで回すまで ref: http://qiita.com/yoshiso/items/e48126c2feaf747f3b25
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'dotenv/tasks' | |
namespace :backup do | |
desc "nightly backup" | |
task :nightly => :dotenv do | |
timestamp = Time.now.strftime("%Y-%m-%d_%H-%M-%S") | |
backup_file = "tmp/backups/#{timestamp}_dump.sql.gz" | |
`mkdir -p tmp/backups` | |
`mysqldump -u #{ENV['DB_USERNAME']} -p#{ENV['DB_PASSWORD']} #{ENV['DB_NAME']} | gzip -c > #{backup_file}` | |
send_to_s3_nightly backup_file | |
end | |
desc "weekly backup" | |
task :weekly => :dotenv do | |
timestamp = Time.now.strftime("%Y-%m-%d_%H-%M-%S") | |
backup_file = "tmp/backups/#{timestamp}_dump.sql.gz" | |
`mkdir -p tmp/backups` | |
`mysqldump -u #{ENV['DB_USERNAME']} -p#{ENV['DB_PASSWORD']} #{ENV['DB_NAME']} | gzip -c > #{backup_file}` | |
send_to_s3_weekly backup_file | |
delete_olds | |
end | |
def delete_olds(num = 10) | |
bucket = get_bucket | |
bucket.objects.with_prefix("backups/nightly/").each do |backup| | |
datestr = backup.key.match(/[0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{2}-[0-9]{2}-[0-9]{2}/)[0] | |
date = DateTime.strptime(datestr,"%Y-%m-%d_%H-%M-%S") | |
if (Date.today - num).to_datetime > date | |
backup.delete | |
end | |
end | |
bucket.objects.with_prefix("backups/weekly/").each do |backup| | |
datestr = backup.key.match(/[0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{2}-[0-9]{2}-[0-9]{2}/)[0] | |
date = DateTime.strptime(datestr,"%Y-%m-%d_%H-%M-%S") | |
if (Date.today - num * 7).to_datetime > date | |
backup.delete | |
end | |
end | |
end | |
def send_to_s3_nightly(file_path) | |
bucket = get_bucket | |
file_name = File.basename(file_path) | |
bucket.objects["backups/nightly/#{file_name}"].write(File.open("#{file_path}")) | |
end | |
def send_to_s3_weekly(file_path) | |
bucket = get_bucket | |
file_name = File.basename(file_path) | |
bucket.objects["backups/weekly/#{file_name}"].write(File.open("#{file_path}")) | |
end | |
def get_bucket | |
s3 = AWS::S3.new( | |
access_key_id: ENV["AWS_ACCESS_KEY_ID"], | |
secret_access_key: ENV["AWS_SECRET_ACCESS_KEY"], | |
s3_endpoint: ENV['S3_HOST_NAME'] | |
) | |
bucket = s3.buckets[ENV['S3_BUCKET_NAME']] | |
bucket = s3.buckets.create(ENV['S3_BUCKET_NAME']) unless bucket.exists? | |
bucket | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+require "whenever/capistrano" | |
+SSHKit.config.command_map[:whenever] = "bundle exec whenever" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set :whenever_environment, "#{fetch(:stage)}" #wheneverでの環境指定に必要 | |
set :whenever_identifier, ->{ "#{fetch(:application)}_#{fetch(:stage)}" } #cronjobのユニークを保つために必要 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cd path/to/project_dir/ | |
wheneverize . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bundle exec cap production deploy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+ gem 'whenever', :require => false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 出力先のログファイルの指定 | |
set :output, 'log/crontab.log' | |
# ジョブの実行環境の指定 | |
set :environment, :production | |
# 毎日 am4:30のスケジューリング | |
every 1.day, :at => '4:30 am', :roles => [:db] do | |
rake "backup:nightly" | |
end | |
# 日曜日のpm12時にスケジューリング | |
every :monday, :at => '4am', :roles => [:db] do | |
rake "backup:weekly" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment