dpickett-awesome_backup 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :minor: 5
3
+ :patch: 1
4
+ :major: 0
@@ -0,0 +1,44 @@
1
+ # Perform backups
2
+ #
3
+ # To automatically transfer backups to your local machine, add:
4
+ # after "backup:create", "backup:download"
5
+ #
6
+ if respond_to?(:_cset)
7
+ _cset(:backup_path) { "#{shared_path}/backups" }
8
+ _cset(:skip_backup_tables, ['sessions'])
9
+ else
10
+ set(:backup_path, "#{shared_path}/backups")
11
+ set(:skip_backup_tables, ['sessions'])
12
+ end
13
+
14
+ namespace :backup do
15
+ def latest
16
+ capture("cd #{current_path}; rake -s db:backup:latest BACKUP_DIR=#{backup_path}").strip
17
+ end
18
+
19
+ desc "Create a backup on the server"
20
+ task :create, :roles => :db, :only => {:primary => true} do
21
+ rails_env = fetch(:rails_env, "production")
22
+ skip_tables = Array(skip_backup_tables).join(',')
23
+ run "cd #{current_path}; rake db:backup:create RAILS_ENV=#{rails_env} BACKUP_DIR=#{backup_path} SKIP_TABLES=#{skip_tables}"
24
+ end
25
+
26
+ desc "Retreive a backup from the server. Gets the latest by default, set :backup_version to specify which version to copy"
27
+ task :download, :roles => :db, :only => {:primary => true} do
28
+ version = fetch(:backup_version, latest)
29
+ run "tar -C #{backup_path} -czf #{backup_path}/#{version}.tar.gz #{version}"
30
+ `mkdir -p backups`
31
+ get "#{backup_path}/#{version}.tar.gz", "backups/#{version}.tar.gz"
32
+ run "rm #{backup_path}/#{version}.tar.gz"
33
+ `tar -C backups -zxf backups/#{version}.tar.gz`
34
+ `rm backups/#{version}.tar.gz`
35
+ end
36
+
37
+ desc "Creates a new remote backup and clones it to the local database"
38
+ task :mirror, :roles => :db, :only => {:primary => true} do
39
+ create
40
+ download
41
+ `rake db:backup:restore`
42
+ end
43
+
44
+ end
@@ -0,0 +1,3 @@
1
+ Capistrano::Configuration.instance(:must_exist).load do
2
+ load File.join(File.dirname(__FILE__), "recipes", "backup.rb")
3
+ end
@@ -0,0 +1,29 @@
1
+ namespace :db do
2
+ namespace :backup do
3
+
4
+ def backup_directory
5
+ ENV['BACKUP_DIR'] ||= File.join(RAILS_ROOT, 'backups')
6
+ end
7
+
8
+ # Set the VERSION to the latest if it's not set
9
+ task :latest do
10
+ last = Dir["#{backup_directory}/*/"].sort.last
11
+ puts ENV['VERSION'] ||= File.basename(last) if last
12
+ end
13
+
14
+ # Setup environment variables for the schema and fixtures tasks
15
+ task :setup => :environment do
16
+ ENV['VERSION'] ||= Time.now.utc.strftime('%Y%m%d%H%M%S')
17
+ backup = File.join(backup_directory, ENV['VERSION'])
18
+ FileUtils.mkdir_p backup
19
+ ENV['FIXTURES_DIR'] = backup
20
+ ENV['SCHEMA'] = File.join(backup, 'schema.rb')
21
+ end
22
+
23
+ desc 'Create a new backup of the database'
24
+ task :create => [:setup, 'db:fixtures:dump', 'db:schema:dump']
25
+
26
+ desc 'Restore a backup of the database. Use VERSION to specify a version other than the latest.'
27
+ task :restore => [:latest, :setup, 'db:schema:load', 'db:fixtures:load']
28
+ end
29
+ end
@@ -0,0 +1,44 @@
1
+ # These tasks replace the built-in Rails tasks for dumping and loading the schema,
2
+ # allowing you to specify the FIXTURES_DIR to use for dumping and loading.
3
+ Rake.application.instance_eval do
4
+ @tasks.delete "db:fixtures:load"
5
+ @tasks.delete "db:fixtures:dump"
6
+ end
7
+
8
+
9
+ namespace :db do
10
+ namespace :fixtures do
11
+ desc "Load fixtures into the current environment's database. Load specific fixtures using FIXTURES=x,y"
12
+ task :load => :environment do
13
+ require 'active_record/fixtures'
14
+ ActiveRecord::Base.establish_connection(RAILS_ENV.to_sym)
15
+ fixtures_dir = ENV['FIXTURES_DIR'] || File.join(RAILS_ROOT, 'test', 'fixtures')
16
+ (ENV['FIXTURES'] ? ENV['FIXTURES'].split(/,/) : Dir.glob(File.join(fixtures_dir, '*.{yml,csv}'))).each do |fixture_file|
17
+ Fixtures.create_fixtures(fixtures_dir, File.basename(fixture_file, '.*'))
18
+ end
19
+ end
20
+
21
+ desc "Create YAML fixtures from data in the current environment's database. Dump specific tables using TABLES=x[,y,z]."
22
+ task :dump => :environment do
23
+ ActiveRecord::Base.establish_connection(RAILS_ENV.to_sym)
24
+ fixtures_dir = ENV['FIXTURES_DIR'] ||
25
+ File.join(RAILS_ROOT, 'test', 'fixtures')
26
+ skip_tables = ENV['SKIP_TABLES'] ? ENV['SKIP_TABLES'].split(/,/) : ["sessions"]
27
+ tables = ENV['TABLES'] ? ENV['TABLES'].split(/,/) : ActiveRecord::Base.connection.tables
28
+ sql = "SELECT * FROM %s LIMIT %d OFFSET %d"
29
+ limit = 500
30
+ (tables - skip_tables).each do |table_name|
31
+ i = "000"
32
+ offset = 0
33
+ File.open("#{fixtures_dir}/#{table_name}.yml", 'w' ) do |file|
34
+ while !(data = ActiveRecord::Base.connection.select_all(sql % [table_name, limit, offset])).empty?
35
+ data.each do |record|
36
+ file.write({"#{table_name}_#{i.succ!}" => record}.to_yaml.sub(/^---.*\n/, ''))
37
+ end
38
+ offset += limit
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
File without changes
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dpickett-awesome_backup
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.1
5
+ platform: ruby
6
+ authors:
7
+ - Collective Ideas
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-01-22 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: ""
17
+ email: dpickett@enlightsolutions.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - VERSION.yml
26
+ - lib/awesome_backup
27
+ - lib/awesome_backup/recipes
28
+ - lib/awesome_backup/recipes/backup.rb
29
+ - lib/awesome_backup/recipes.rb
30
+ - lib/awesome_backup/tasks
31
+ - lib/awesome_backup/tasks/backup.rake
32
+ - lib/awesome_backup/tasks/database.rake
33
+ - lib/awesome_backup.rb
34
+ has_rdoc: false
35
+ homepage: http://github.com/collectiveidea/awesome-backup/tree/master
36
+ post_install_message:
37
+ rdoc_options: []
38
+
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ requirements: []
54
+
55
+ rubyforge_project:
56
+ rubygems_version: 1.2.0
57
+ signing_key:
58
+ specification_version: 2
59
+ summary: Provides Rake and Capistrano tasks for making database backups
60
+ test_files: []
61
+