Class: Backup::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/backup.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_path, cloud = false, *args) ⇒ Instance

Returns a new instance of Instance.



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/backup.rb', line 13

def initialize(root_path, cloud = false, *args)
  if cloud
    @file_item = Backup::FileItem.for :cloud, *args
  else
    @file_item = Backup::FileItem.for :local
  end

  @hostname = Socket.gethostname
  @_root_path = @root_path
  @root_path = "#{root_path}/#{@hostname}"
  @timestamp = Backup::Timestamp.create
end

Instance Attribute Details

#compressionObject

Returns the value of attribute compression.



11
12
13
# File 'lib/backup.rb', line 11

def compression
  @compression
end

#file_itemObject (readonly)

Returns the value of attribute file_item.



11
12
13
# File 'lib/backup.rb', line 11

def file_item
  @file_item
end

#hostnameObject

Returns the value of attribute hostname.



11
12
13
# File 'lib/backup.rb', line 11

def hostname
  @hostname
end

#root_pathObject (readonly)

Returns the value of attribute root_path.



11
12
13
# File 'lib/backup.rb', line 11

def root_path
  @root_path
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



11
12
13
# File 'lib/backup.rb', line 11

def timestamp
  @timestamp
end

Instance Method Details

#create!(local_path, increment = false, purge_previous = false) ⇒ Object



43
44
45
46
# File 'lib/backup.rb', line 43

def create! local_path, increment = false, purge_previous = false
  jar = Jar.new @file_item, @root_path, local_path, @key
  jar.save increment, @compression, purge_previous
end

#jar_versions(jar) ⇒ Object



52
53
54
# File 'lib/backup.rb', line 52

def jar_versions jar
  Jar.jar_versions @file_item, @root_path, jar, !!jar[/^[0-9a-z]{32}$/]
end

#jarsObject



48
49
50
# File 'lib/backup.rb', line 48

def jars
  Jar.all @file_item, @root_path
end

#restore_jar_to(hash, timestamp, to) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/backup.rb', line 56

def restore_jar_to(hash, timestamp, to)
  files = Jar.fetch_index_for(@file_item, @root_path, hash, timestamp)
  puts_fail "Jar doesn't exists" if files.nil?

  unless files[:checksum].nil?
    if @key.nil? or
       @key.decrypt(Base64.decode64(files[:checksum])) != timestamp

      puts_fail "Checksum does not match."
    end
    files.delete :checksum
  end

  unless files[:compression].nil?
    compression = Archive.new files[:compression].to_sym
    files.delete :compression
  else
    compression = nil
  end

  pbar = ProgressBar.new(
    "Restoring",
    files.keys.length
  )
  pbar.bar_mark = '*'

  files.keys.sort.each do |file|
    restore_file = File.join(to, file)
    current_file = files[file]

    if current_file[:checksum].nil?
      try_create_dir restore_file

      File.chmod current_file[:mode], restore_file
      File.chown current_file[:uid], current_file[:gid], restore_file

      file_ok = @file_item.stat(restore_file)[restore_file]

      check_mode(restore_file, file_ok[:mode], current_file[:mode])
      check_rights(
        restore_file,
        file_ok[:uid],
        file_ok[:gid],
        current_file[:uid],
        current_file[:gid]
      )
    else
      try_create_dir(File.dirname restore_file)

      begin
        File.open(restore_file, "wb") do |f|
          f.chmod current_file[:mode]
          f.chown current_file[:uid], current_file[:gid]

          remote_path = "#{@root_path}/#{hash}/#{current_file[:timestamp]}"
          remote_path += "/#{@file_item.file_hash file}"

          data = @file_item.read_file(remote_path)
          data = @key.decrypt_from_stream data if @key

          unless compression.nil?
            data = compression.decompress(data).read
          end

          f.print data
        end

        file_ok = @file_item.stat(restore_file)[restore_file]

        check_mode(restore_file, file_ok[:mode], current_file[:mode])
        check_rights(
          restore_file,
          file_ok[:uid],
          file_ok[:gid],
          current_file[:uid],
          current_file[:gid]
        )
      rescue Errno::EACCES
        puts_fail "Permission denied for #{restore_file.dark_green}"
      end
    end

    pbar.inc
  end

  pbar.finish
end

#rsa_key(path, size) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/backup.rb', line 35

def rsa_key path, size
  if File.exists? path
    @key = Crypto::Key.from_file(path, size)
  else
    @key = nil
  end
end