Skip to content

Commit

Permalink
Moved working out files functionality to backup info class
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Wells committed Apr 27, 2009
1 parent 8294b64 commit 4ea5d57
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 15 deletions.
15 changes: 15 additions & 0 deletions lib/backs3/backup_info.rb
Expand Up @@ -50,6 +50,21 @@ def initialize(backups, options)
@full = @options['force-full'] || first_backup? || @date - @last_full_backup.date > (@options['full'] || 7).days
end

def all_files
if !full && @last_full_backup
backups = @backups.select{|b| b.date >= @last_full_backup.date && b.date <= self.date }
backups << self

rfiles = backups.collect{|b| b.files}.flatten
rfiles.reject! do |first_file|
rfiles.detect{|second_file| second_file.path == first_file.path && second_file.backup_info.date > first_file.backup_info.date }
end
rfiles
else
self.files
end
end

def key
@options['prefix'] + @date.to_s
end
Expand Down
22 changes: 7 additions & 15 deletions lib/backs3/restore.rb
Expand Up @@ -25,20 +25,7 @@ def available(backup_key = nil)
raise "No backup #{backup_key} available"
end

backups = [backup]

if !backup.full && backup.last_full_backup
@backups.each do |b|
backups << b if b.date >= backup.last_full_backup.date && b.date < backup.date
end
end

files = backups.collect{|b| b.files}.flatten
files.reject!{|f| !f.backup_info.respond_to?(:date) }

files.reject! do |first_file|
files.detect{|second_file| first_file.path == second_file.path && second_file.backup_info.date > first_file.backup_info.date }
end
files = backup.all_files

puts "Backup information for #{backup.date}"
files.each do |file|
Expand Down Expand Up @@ -78,7 +65,12 @@ def cat(backup, file)
end
end

def restore(backup, file = nil)
def restore(date, file = nil)
backup = @backups.detect{|b| b.date.to_s == key.to_s}




backup_key = @options['prefix'] + backup.to_s
objects = []

Expand Down
24 changes: 24 additions & 0 deletions spec/backs3/backup_info_spec.rb
Expand Up @@ -61,6 +61,30 @@
end
end

describe 'all_files' do
it 'should just be the list of files in the backup if the backup is full' do
backup = BackupInfo.new(@previous, @options)
backup.full.should == true
backup.all_files.should == backup.files
end

it 'should include files from the last full backup if partial' do
last_full = BackupInfo.new([], @options)
last_full.stub!(:full).and_return(true)

current = BackupInfo.new([last_full], @options)
current.stub!(:full).and_return(false)

file_1 = mock(:file_info, :path => 'test/file_1', :backup_info => last_full)
file_2 = mock(:file_info, :path => 'test/file_2', :backup_info => current)

last_full.stub!(:files).and_return([file_1])
current.stub!(:files).and_return([file_2])

current.all_files.should == [file_1, file_2]
end
end

describe '@full' do
it 'should set full if there is no previous backup specified' do
BackupInfo.new(@previous, @options).full.should == true
Expand Down
7 changes: 7 additions & 0 deletions spec/backs3/restore_spec.rb
Expand Up @@ -25,9 +25,16 @@
@file_mock4
]

@files_mock3 = [
@file_mock4, @file_mock2, @file_mock3
]

@backup_mock1.stub!(:files).and_return(@files_mock1)
@backup_mock2.stub!(:files).and_return(@files_mock2)

@backup_mock1.stub!(:all_files).and_return(@files_mock1)
@backup_mock2.stub!(:all_files).and_return(@files_mock3)

@backup_array = [@backup_mock1, @backup_mock2]
@restore.stub!(:load_backup_info).and_return(@backup_array)
@restore.instance_variable_set('@backups', @backup_array)
Expand Down

0 comments on commit 4ea5d57

Please sign in to comment.