Class: EasyBackup::Resources::FileSystem

Inherits:
Object
  • Object
show all
Includes:
Zip
Defined in:
lib/easy_backup/resources/file_system.rb

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ FileSystem

Returns a new instance of FileSystem.



6
7
8
9
10
# File 'lib/easy_backup/resources/file_system.rb', line 6

def initialize(&block)
  @folders = []
  @files = []
  instance_eval &block if block_given?
end

Instance Method Details

#file(file_name) ⇒ Object



20
21
22
# File 'lib/easy_backup/resources/file_system.rb', line 20

def file(file_name)
  @files << file_name
end

#filesObject



24
25
26
# File 'lib/easy_backup/resources/file_system.rb', line 24

def files
  @files.map { |f| f.is_a?(Proc) ? f.call : f }
end

#folder(path) ⇒ Object



12
13
14
# File 'lib/easy_backup/resources/file_system.rb', line 12

def folder(path)
  @folders << path
end

#foldersObject



16
17
18
# File 'lib/easy_backup/resources/file_system.rb', line 16

def folders
  @folders.map { |f| f.is_a?(Proc) ? f.call : f }
end

#save(resource) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/easy_backup/resources/file_system.rb', line 36

def save(resource)
  folders.each do |folder|
    EasyBackup.configuration.logger.info "[EasyBackup] Saving #{resource} into #{folder}"

    FileUtils.mkpath folder unless Dir.exist? folder
    if Dir.exist? resource
      FileUtils.cp_r resource, "#{folder}/#{File.basename(resource)}"
    else
      FileUtils.cp resource, "#{folder}/#{File.basename(resource)}"
    end
  end
end

#send_to(*storages) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/easy_backup/resources/file_system.rb', line 49

def send_to(*storages)
  if update_zip_file
    EasyBackup.configuration.logger.info "[EasyBackup] Zip #{zip_file}"
    FileUtils.mkpath File.dirname(zip_file) unless Dir.exist? File.dirname(zip_file)
    ZipFile.open(zip_file, ZipFile::CREATE) do |zip|
      (files | folders).each do |resource|
        if Dir.exist? resource
          EasyBackup.configuration.logger.debug "[EasyBackup] add #{resource}"
          Dir.glob("#{resource}/**/**", File::FNM_DOTMATCH).reject{|entry| entry =~ /\/[\.]+\z/}.each do |r|
            EasyBackup.configuration.logger.debug "[EasyBackup] add #{r}" unless Dir.exist? r
            zip.add "#{File.basename(resource)}/#{r.gsub("#{resource}/", '')}", r
          end
        else
          EasyBackup.configuration.logger.debug "[EasyBackup] add #{resource}"
          zip.add File.basename(resource), resource
        end
      end
    end
    storages.each { |s| s.save zip_file }
    FileUtils.rm zip_file
  else
    (files | folders).each do |resource|
      storages.each { |s| s.save resource } if File.exist? resource
    end
  end
end

#zip(file_name) ⇒ Object



28
29
30
# File 'lib/easy_backup/resources/file_system.rb', line 28

def zip(file_name)
  @zip = file_name
end

#zip_fileObject



32
33
34
# File 'lib/easy_backup/resources/file_system.rb', line 32

def zip_file
  @zip_file || update_zip_file
end