Class: Siba::Destination::Dir::DestDir

Inherits:
Object
  • Object
show all
Includes:
FilePlug, LoggerPlug
Defined in:
lib/siba/plugins/destination/dir/dest_dir.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from LoggerPlug

close, create, logger, #logger, opened?

Methods included from FilePlug

#siba_file, siba_file, siba_file=

Constructor Details

#initialize(dir) ⇒ DestDir

Returns a new instance of DestDir.



10
11
12
13
# File 'lib/siba/plugins/destination/dir/dest_dir.rb', line 10

def initialize(dir)
  @dir = siba_file.file_expand_path dir
  test_dir_access
end

Instance Attribute Details

#dirObject

Returns the value of attribute dir.



8
9
10
# File 'lib/siba/plugins/destination/dir/dest_dir.rb', line 8

def dir
  @dir
end

Instance Method Details

#copy_backup_to_dest(path_to_backup) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/siba/plugins/destination/dir/dest_dir.rb', line 15

def copy_backup_to_dest(path_to_backup)
  siba_file.run_this "copy backup to dest" do
    logger.info "Copying backup to destination directory: #{dir}"
    unless siba_file.file_file? path_to_backup
      raise Siba::Error, "Backup file '#{path_to_backup}' does not exist"
    end
    unless siba_file.file_directory? dir
      raise Siba::Error, "Destination directory '#{dir}' does not exist"
    end
    siba_file.file_utils_cp(path_to_backup, dir)
  end
end

#get_backups_list(backup_name) ⇒ Object

Returns an array of two-element arrays: [file_name, mtime]



65
66
67
68
69
70
71
72
73
74
# File 'lib/siba/plugins/destination/dir/dest_dir.rb', line 65

def get_backups_list(backup_name)
  siba_file.run_this do
    Siba::FileHelper.entries(dir).select do |f|
      f =~ /^#{backup_name}/
    end.map do |f|
      mtime = siba_file.file_mtime File.join dir, f
      [f, mtime]
    end
  end
end

#restore_backup_to_dir(backup_name, to_dir) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/siba/plugins/destination/dir/dest_dir.rb', line 28

def restore_backup_to_dir(backup_name, to_dir)
  siba_file.run_this do
    logger.info "Copying backup from destination directory: #{dir}"
    path_to_backup = File.join dir, backup_name
    unless siba_file.file_file? path_to_backup
      raise Siba::Error, "Can not find backup #{path_to_backup}"
    end

    siba_file.file_utils_cp path_to_backup, to_dir
  end
end

#test_dir_accessObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/siba/plugins/destination/dir/dest_dir.rb', line 40

def test_dir_access
  siba_file.run_this "test dir access" do
    # create dest dir
    begin
      siba_file.file_utils_mkpath dir unless siba_file.file_directory? dir
    rescue Exception
      logger.error "Failed to create destination dir '#{dir}'."
      raise
    end

    # copy a test file to dest dir
    begin
      test_file = Siba::TestFiles.prepare_test_file "destination_dir", dir
      raise "Can not find the test file." unless siba_file.file_file? test_file
      siba_file.file_utils_remove_entry_secure test_file
    rescue Exception
      logger.error "Could not write to destination dir '#{dir}'"
      raise
    end

    logger.debug "Access to destination directory is verified"
  end
end