liquid_backup 0.1.4 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.4
1
+ 0.2.0
@@ -0,0 +1,45 @@
1
+ module LiquidBackup
2
+ class Job
3
+ module Database
4
+ def Database.included(parent)
5
+ parent.class_eval <<-CALLBACK
6
+ include Database::StepCallbacks
7
+ set_callback :upload, :before do |object|
8
+ compress_database
9
+ end
10
+
11
+ set_callback :backup, :before do |object|
12
+ FileUtils.mkdir_p File.join(current_backup_path,'databases')
13
+ end
14
+
15
+ set_callback :upload, :after do
16
+ FileUtils.rm_rf(File.join(current_backup_path,'databases'))
17
+ end
18
+ CALLBACK
19
+ end
20
+
21
+ module StepCallbacks
22
+ def compress_database
23
+ return unless @_fire_database_callbacks
24
+ source = File.join(current_backup_path,'databases')
25
+ destination = File.join(current_backup_path,'databases.tar.gz')
26
+
27
+ compress(source, destination)
28
+ end
29
+ end
30
+
31
+ def database(name, options = {})
32
+ @_fire_database_callbacks = true
33
+
34
+ destination = File.join(current_backup_path,'databases', name + '_' + Date.today.strftime("%Y%m%d%H%M%S") + '.sql')
35
+
36
+ `/usr/local/pgsql/bin/pg_dump -U postgres #{name} > #{destination}`
37
+ end
38
+ end
39
+
40
+ include LiquidBackup::Job::Database
41
+ end
42
+ end
43
+
44
+
45
+
@@ -0,0 +1,44 @@
1
+ module LiquidBackup
2
+ class Job
3
+ module Folder
4
+ def Folder.included(parent)
5
+ parent.class_eval <<-CALLBACK
6
+ include Folder::StepCallbacks
7
+ set_callback :upload, :before do |object|
8
+ compress_folder
9
+ end
10
+
11
+ set_callback :backup, :before do |object|
12
+ FileUtils.mkdir_p File.join(current_backup_path,'folders')
13
+ end
14
+
15
+ set_callback :upload, :after do
16
+ FileUtils.rm_rf(File.join(current_backup_path,'folders'))
17
+ end
18
+ CALLBACK
19
+ end
20
+
21
+ module StepCallbacks
22
+ def compress_folder
23
+ return unless @_fire_database_callbacks
24
+
25
+ source = File.join(current_backup_path,'folders')
26
+ destination = File.join(current_backup_path,'folders.tar.gz')
27
+
28
+ compress(source, destination)
29
+ end
30
+ end
31
+
32
+ def folder(path, options = {})
33
+ @_fire_database_callbacks = true
34
+
35
+ source = File.join(application_directory,'current',path)
36
+ destination = File.join(current_backup_path,'folders',path.gsub('/','_') + '.tar.gz')
37
+
38
+ compress(source, destination)
39
+ end
40
+ end
41
+
42
+ include LiquidBackup::Job::Folder
43
+ end
44
+ end
@@ -1,4 +1,9 @@
1
+ require 'active_support/concern'
2
+ require 'active_support/callbacks'
3
+
1
4
  class LiquidBackup::Job
5
+ include ActiveSupport::Callbacks
6
+
2
7
  attr_accessor :script_path
3
8
  attr_accessor :application_directory
4
9
  attr_accessor :backups_path
@@ -8,51 +13,50 @@ class LiquidBackup::Job
8
13
  attr_accessor :destination_handler
9
14
  attr_accessor :destination_location
10
15
 
16
+ define_callbacks :upload, :backup
17
+
11
18
  def initialize(script_path)
12
19
  @script_path = script_path
13
20
  @application_directory = script_path.gsub('/current/config/backup.rb','')
14
21
  @backups_path = File.join(application_directory,'backups')
15
22
  @current_backup_path = File.join(backups_path,'current')
16
23
  @previous_backups_path = File.join(backups_path,'previous')
17
-
24
+ end
25
+
26
+ set_callback :backup, :before do
18
27
  create_backup_folders
28
+ archive
19
29
  end
20
30
 
21
31
  def perform
22
- archive
23
-
24
32
  eval(File.read(script_path), binding)
25
-
26
- finalize
27
- upload
28
33
  end
29
34
 
30
35
  def backup
31
- yield
36
+ run_callbacks :backup do
37
+ yield
38
+ end
39
+
40
+ run_callbacks :upload do
41
+ upload
42
+ end
43
+ end
44
+
45
+ set_callback :upload, :after do
46
+ cleanup
32
47
  end
33
48
 
34
49
  private
35
-
36
50
  def archive
37
51
  archive_files = Dir.glob(File.join(current_backup_path,'*.tar.gz'))
38
52
  last_run = File.mtime(current_backup_path).strftime("%Y%m%d%H%M%S")
39
53
  last_run_destination = File.join(previous_backups_path,last_run)
40
54
 
41
- FileUtils.mkdir_p last_run_destination
55
+ create_folder last_run_destination
42
56
  FileUtils.mv archive_files, last_run_destination
43
57
  end
44
58
 
45
- def finalize
46
- finalize_folders
47
- finalize_databases
48
-
49
- cleanup
50
- end
51
-
52
59
  def cleanup
53
- FileUtils.rm_rf File.join(current_backup_path,'folders')
54
- FileUtils.rm_rf File.join(current_backup_path,'databases')
55
-
56
60
  previous_backups = Dir.glob(File.join(previous_backups_path,'*')).sort
57
61
 
58
62
  if previous_backups.length > LiquidBackup::KEEP
@@ -61,20 +65,6 @@ class LiquidBackup::Job
61
65
  FileUtils.rm_rf backups_to_prune
62
66
  end
63
67
  end
64
-
65
- def finalize_folders
66
- source = File.join(current_backup_path,'folders')
67
- destination = File.join(current_backup_path,'folders.tar.gz')
68
-
69
- compress(source, destination)
70
- end
71
-
72
- def finalize_databases
73
- source = File.join(current_backup_path,'databases')
74
- destination = File.join(current_backup_path,'databases.tar.gz')
75
-
76
- compress(source, destination)
77
- end
78
68
 
79
69
  def upload
80
70
  files_to_backup = Dir.glob(File.join(current_backup_path,'*.tar.gz'))
@@ -86,28 +76,17 @@ class LiquidBackup::Job
86
76
  end
87
77
 
88
78
  def create_backup_folders
89
- FileUtils.mkdir_p backups_path
90
- FileUtils.mkdir_p current_backup_path
91
- FileUtils.mkdir_p File.join(current_backup_path,'folders')
92
- FileUtils.mkdir_p File.join(current_backup_path,'databases')
93
- FileUtils.mkdir_p previous_backups_path
79
+ create_folder backups_path, current_backup_path, previous_backups_path
80
+ end
81
+
82
+ def create_folder(*name)
83
+ name.each{|f| FileUtils.mkdir_p f}
94
84
  end
95
85
 
96
86
  def destination(name, location)
97
87
  @destination_handler = name
98
88
  @destination_location = location
99
89
  end
100
-
101
- def folder(path, options = {})
102
- source = File.join(application_directory,'current',path)
103
- destination = File.join(current_backup_path,'folders',path.gsub('/','_') + '.tar.gz')
104
-
105
- compress(source, destination)
106
- end
107
-
108
- def database(name, options = {})
109
- destination = File.join(current_backup_path,'databases', name + '_' + Date.today.strftime("%Y%m%d%H%M%S") + '.sql')
110
-
111
- `/usr/local/pgsql/bin/pg_dump -U postgres #{name} > #{destination}`
112
- end
113
- end
90
+ end
91
+
92
+ Dir.glob(File.join(File.dirname(__FILE__),'job','modules','*_module.rb')).each {|s| require s}
@@ -3,17 +3,23 @@ class LiquidBackup::Manager
3
3
  include Singleton
4
4
 
5
5
  attr_accessor :backup_handler
6
+ attr_reader :backup_jobs
6
7
 
7
8
  def initialize
8
9
  @backup_handler = {}
10
+ @backup_jobs = []
9
11
  end
10
12
 
11
13
  def collect_jobs(pattern)
12
- @backup_jobs = []
13
14
  Dir.glob(pattern).each do |backupjob|
14
15
  @backup_jobs << LiquidBackup::Job.new(backupjob)
15
16
  end
16
- @backup_jobs
17
+ self
18
+ end
19
+
20
+ def perform_all
21
+ @backup_jobs.each {|j| j.perform}
22
+ self
17
23
  end
18
24
 
19
25
  def use(name, handler)
data/spec/backup_spec.rb CHANGED
@@ -3,11 +3,20 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
3
3
  describe LiquidBackup do
4
4
 
5
5
  describe LiquidBackup::Manager do
6
- it "should return a list of backup jobs" do
7
- jobs = LiquidBackup::Manager.collect_jobs(File.join(File.dirname(__FILE__),'fs_root/home/**/current/config/backup.rb'))
8
- jobs.should be_an(Array)
6
+ before(:all) do
7
+ @jobs_path = File.join(File.dirname(__FILE__),'fs_root/home/**/current/config/backup.rb')
8
+ LiquidBackup::Manager.collect_jobs(@jobs_path)
9
9
 
10
- jobs.first.should be_an(LiquidBackup::Job)
10
+ end
11
+
12
+ it "should store the list of jobs" do
13
+ LiquidBackup::Manager.collect_jobs(@jobs_path)
14
+ LiquidBackup::Manager.backup_jobs.should be_an(Array)
15
+ end
16
+
17
+ it "should return itself to allow chaining" do
18
+ lbm = LiquidBackup::Manager.collect_jobs(@jobs_path)
19
+ lbm.should eql LiquidBackup::Manager.instance()
11
20
  end
12
21
  end
13
22
 
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
- - 4
9
- version: 0.1.4
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Dan Williams
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-03-04 00:00:00 -04:00
17
+ date: 2010-03-05 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -62,6 +62,8 @@ files:
62
62
  - lib/liquid_backup.rb
63
63
  - lib/liquid_backup/handler/s3.rb
64
64
  - lib/liquid_backup/job.rb
65
+ - lib/liquid_backup/job/modules/database_module.rb
66
+ - lib/liquid_backup/job/modules/folder_module.rb
65
67
  - lib/liquid_backup/manager.rb
66
68
  - spec/backup_spec.rb
67
69
  - spec/fs_root/home/user1/app1/production/current/public/system/important_files/file1.txt