-
Notifications
You must be signed in to change notification settings - Fork 675
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented a Ruby on Rails 3 compatible generator for Backup.
- Loading branch information
Michael van Rooijen
committed
Sep 20, 2010
1 parent
28a1ff3
commit d7455f6
Showing
6 changed files
with
365 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module Backup | ||
module Environment | ||
module RailsConfiguration | ||
|
||
if defined?(Rails.root) | ||
# Sets BACKUP_PATH equal to RAILS_ROOT | ||
BACKUP_PATH = Rails.root.to_s | ||
|
||
# Sets DB_CONNECTION_SETTINGS to false | ||
DB_CONNECTION_SETTINGS = false | ||
end | ||
|
||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
Description: | ||
Backup's Ruby on Rails 3 Generator | ||
|
||
Example: | ||
rails generate backup | ||
|
||
This will create: | ||
* lib/tasks/backup.rake | ||
* config/backup.rb | ||
* db/migrate/<DateTime>_create_backup_tables.rb |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
class BackupGenerator < Rails::Generators::Base | ||
source_root File.expand_path('../templates', __FILE__) | ||
|
||
def copy_files | ||
copy_file 'backup.rake', 'lib/tasks/backup.rake' | ||
copy_file 'backup.rb', 'config/backup.rb' | ||
|
||
unless Dir["#{Rails.root}/db/migrate/*create_backup_tables.rb"].any? | ||
copy_file 'create_backup_tables.rb', "db/migrate/#{Time.now.utc.strftime("%Y%m%d%H%M%S")}_create_backup_tables.rb" | ||
end | ||
|
||
puts message | ||
end | ||
|
||
def message | ||
<<-MESSAGE | ||
============================================================== | ||
Backup's files have been generated! | ||
============================================================== | ||
1: Migrate the database! | ||
rake db:migrate | ||
2: Set up some "Backup Settings" inside the backup configuration file! | ||
config/backup.rb | ||
3: Run the backups! Enjoy. | ||
rake backup:run trigger="your-specified-trigger" | ||
For More Information: | ||
http://github.com/meskyanichi/backup | ||
============================================================== | ||
MESSAGE | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
namespace :backup do | ||
|
||
task :boot => :environment do | ||
Backup::System.boot! | ||
end | ||
|
||
desc "Run Backup Procedure." | ||
task :run => :boot do | ||
puts "Running: #{ENV['trigger']}." | ||
Backup::Setup.new(ENV['trigger'], @backup_procedures).initialize_adapter | ||
end | ||
|
||
desc "Finds backup records by trigger" | ||
task :find => :boot do | ||
puts "Finding backup records with trigger: #{ENV['trigger']}." | ||
backup = Backup::Setup.new(ENV['trigger'], @backup_procedures) | ||
records = backup.procedure.record_class.all( :conditions => {:trigger => ENV['trigger']} ) | ||
|
||
if ENV['table'].eql?("true") | ||
puts Hirb::Helpers::AutoTable.render(records) | ||
else | ||
records.each do |record| | ||
puts record.to_yaml | ||
end | ||
end | ||
end | ||
|
||
desc "Truncates all records for the specified \"trigger\", excluding the physical files on s3 or the remote server." | ||
task :truncate => :boot do | ||
puts "Truncating backup records with trigger: #{ENV['trigger']}." | ||
Backup::Record::Base.destroy_all :trigger => ENV['trigger'] | ||
end | ||
|
||
desc "Truncates everything." | ||
task :truncate_all => :boot do | ||
puts "Truncating all backup records." | ||
Backup::Record::Base.destroy_all | ||
end | ||
|
||
desc "Destroys all records for the specified \"trigger\", including the physical files on s3 or the remote server." | ||
task :destroy => :boot do | ||
puts "Destroying backup records with trigger: #{ENV['trigger']}." | ||
backup = Backup::Setup.new(ENV['trigger'], @backup_procedures) | ||
backup.procedure.record_class.destroy_all_backups( backup.procedure, ENV['trigger'] ) | ||
end | ||
|
||
desc "Destroys all records for the specified \"trigger\", including the physical files on s3 or the remote server." | ||
task :destroy_all => :boot do | ||
puts "Destroying all backup records." | ||
backup = Backup::Setup.new(false, @backup_procedures) | ||
backup.procedures.each do |backup_procedure| | ||
backup_procedure.record_class.destroy_all_backups( backup_procedure, backup_procedure.trigger ) | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,219 @@ | ||
# Backup Configuration File | ||
# | ||
# Use the "backup" block to add backup settings to the configuration file. | ||
# The argument before the "do" in (backup "argument" do) is called a "trigger". | ||
# This acts as the identifier for the configuration. | ||
# | ||
# In the example below we have a "mysql-backup-s3" trigger for the backup setting. | ||
# All the configuration is done inside this block. To initialize the backup process for this block, | ||
# you invoke it using the following rake task: | ||
# | ||
# rake backup:run trigger="mysql-backup-s3" | ||
# | ||
# You can add as many backup block settings as you want, just be sure every trigger is unique and you can run | ||
# each of them separately. | ||
# | ||
# ADAPTERS | ||
# - MySQL | ||
# - PostgreSQL | ||
# - SQLite | ||
# - Archive | ||
# - Custom | ||
# | ||
# STORAGE METHODS | ||
# - S3 (Amazon) | ||
# - CF (Rackspace Cloud Files) | ||
# - SCP (Remote Server) | ||
# - FTP (Remote Server) | ||
# - SFTP (Remote Server) | ||
# - LOCAL (Local Server) | ||
# | ||
# GLOBAL OPTIONS | ||
# - Keep Backups (keep_backups) | ||
# - Encrypt With Pasword (encrypt_with_password) | ||
# - Notify (notify) | ||
# | ||
# This is the "decrypt" command for all encrypted backups: | ||
# sudo backup --decrypt /path/to/encrypted/file | ||
# | ||
# Each Backup Setting can contain: | ||
# - 1 Adapter | ||
# - 1 Storage Method | ||
# - Multiple Global Options | ||
# | ||
# The combination of these, however, do not matter! So experiment with it. | ||
# | ||
# You can also let Backup notify you by email on successfully created backups. | ||
# - Just uncomment the block of code below (notifier_settings) and fill in your credentials. | ||
# - Then for set "notify" to "true" in each (backup) block you wish to be notified of. | ||
# | ||
# For more information on "Backup", please refer to the wiki on github | ||
# http://wiki.github.com/meskyanichi/backup/configuration-file | ||
|
||
|
||
# Notifier | ||
# Uncomment this if you want to enable notification by email on successful backup runs | ||
# You will also have to set "notify true" inside each backup block below to enable it for that particular backup | ||
# notifier_settings do | ||
# | ||
# to "example1@gmail.com" | ||
# from "example2@gmail.com" | ||
# | ||
# smtp do | ||
# host "smtp.gmail.com" | ||
# port "587" | ||
# username "example1@gmail.com" | ||
# password "example1password" | ||
# authentication "plain" | ||
# domain "localhost.localdomain" | ||
# tls true | ||
# end | ||
# | ||
# end | ||
|
||
|
||
# Initialize with: | ||
# rake backup:run trigger='mysql-backup-s3' | ||
backup 'mysql-backup-s3' do | ||
|
||
adapter :mysql do | ||
user 'user' | ||
password 'password' | ||
database 'database' | ||
|
||
# skip_tables ['table1', 'table2', 'table3'] | ||
# | ||
# options do | ||
# host '123.45.678.90' | ||
# port '80' | ||
# socket '/tmp/socket.sock' | ||
# end | ||
# additional_options '--single-transaction --quick' | ||
end | ||
|
||
storage :s3 do | ||
access_key_id 'access_key_id' | ||
secret_access_key 'secret_access_key' | ||
bucket '/bucket/backups/mysql/' | ||
use_ssl true | ||
end | ||
|
||
keep_backups 25 | ||
encrypt_with_password 'password' | ||
notify false | ||
|
||
end | ||
|
||
# Initialize with: | ||
# rake backup:run trigger='mysql-backup-cloudfiles' | ||
backup 'mysql-backup-cloudfiles' do | ||
|
||
adapter :mysql do | ||
user 'user' | ||
password 'password' | ||
database 'database' | ||
end | ||
|
||
storage :cloudfiles do | ||
username 'username' | ||
api_key 'api_key' | ||
container 'mysql_backup' | ||
end | ||
end | ||
|
||
# Initialize with: | ||
# rake backup:run trigger='postgresql-backup-s3' | ||
backup 'postgresql-backup-scp' do | ||
|
||
adapter :postgresql do | ||
user 'user' | ||
database 'database' | ||
|
||
# skip_tables ['table1', 'table2', 'table3'] | ||
|
||
# options do | ||
# host '123.45.678.90' | ||
# port '80' | ||
# socket '/tmp/socket.sock' | ||
# end | ||
# additional_options '--clean --blobs' | ||
end | ||
|
||
storage :scp do | ||
ip 'example.com' | ||
user 'user' | ||
password 'password' | ||
path '/var/backups/postgresql/' | ||
end | ||
|
||
keep_backups :all | ||
encrypt_with_password false | ||
notify false | ||
|
||
end | ||
|
||
|
||
# Initialize with: | ||
# rake backup:run trigger='archive-backup-ftp' | ||
backup 'archive-backup-ftp' do | ||
|
||
adapter :archive do | ||
files ["#{RAILS_ROOT}/log", "#{RAILS_ROOT}/db"] | ||
end | ||
|
||
storage :ftp do | ||
ip 'example.com' | ||
user 'user' | ||
password 'password' | ||
path '/var/backups/archive/' | ||
end | ||
|
||
keep_backups 10 | ||
encrypt_with_password false | ||
notify false | ||
|
||
end | ||
|
||
|
||
# Initialize with: | ||
# rake backup:run trigger='custom-backup-sftp' | ||
backup 'custom-backup-sftp' do | ||
|
||
adapter :custom do | ||
commands \ | ||
[ "mysqldump [options] [database] > :tmp_path/my_mysql_dump.sql", | ||
"pg_dump [options] [database] > :tmp_path/my_postgresql_dump.sql", | ||
"any_other_db_format [options] [database] > :tmp_path/my_any_other_db_format.sql" ] | ||
end | ||
|
||
storage :sftp do | ||
ip 'example.com' | ||
user 'user' | ||
password 'password' | ||
path '/var/backups/custom/' | ||
end | ||
|
||
keep_backups :all | ||
encrypt_with_password 'password' | ||
notify false | ||
|
||
end | ||
|
||
|
||
# Initializ with: | ||
# rake backup:run trigger='sqlite-backup-local' | ||
backup 'sqlite-backup-local' do | ||
|
||
adapter :sqlite do | ||
database "#{RAILS_ROOT}/db/production.sqlite3" | ||
end | ||
|
||
storage :local do | ||
path "/path/to/storage/location/" | ||
end | ||
|
||
keep_backups :all | ||
encrypt_with_password false | ||
notify false | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class CreateBackupTables < ActiveRecord::Migration | ||
def self.up | ||
create_table :backup do |t| | ||
t.string :trigger | ||
t.string :adapter | ||
t.string :filename | ||
t.string :md5sum | ||
t.string :path | ||
t.string :bucket | ||
t.string :type | ||
t.timestamps | ||
end | ||
end | ||
|
||
def self.down | ||
drop_table :backup | ||
end | ||
end |