Class: Backupsss::Tar

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

Overview

The Tar class is used for creating a tar archive.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(src, dest, compress_archive = true) ⇒ Tar

Returns a new instance of Tar.



8
9
10
11
12
# File 'lib/backupsss/tar.rb', line 8

def initialize(src, dest, compress_archive = true)
  @src              = src
  @dest             = dest
  @compress_archive = compress_archive
end

Instance Attribute Details

#compress_archiveObject (readonly)

Returns the value of attribute compress_archive.



6
7
8
# File 'lib/backupsss/tar.rb', line 6

def compress_archive
  @compress_archive
end

#destObject (readonly)

Returns the value of attribute dest.



6
7
8
# File 'lib/backupsss/tar.rb', line 6

def dest
  @dest
end

#srcObject (readonly)

Returns the value of attribute src.



6
7
8
# File 'lib/backupsss/tar.rb', line 6

def src
  @src
end

Instance Method Details

#filenameObject



53
54
55
# File 'lib/backupsss/tar.rb', line 53

def filename
  dest.split('/').last
end

#makeObject



14
15
16
17
18
# File 'lib/backupsss/tar.rb', line 14

def make
  return unless valid_dest? && valid_src?
  _, err, status = Open3.capture3("#{tar_command} #{dest} #{src}")
  File.open(dest) if valid_exit?(status, err) && valid_file?
end

#messagesObject



38
39
40
41
42
43
# File 'lib/backupsss/tar.rb', line 38

def messages
  {
    no_file:   'ERROR: Tar destination file does not exist.',
    zero_byte: 'ERROR: Tar destination file is 0 bytes.'
  }
end

#tar_commandObject



57
58
59
# File 'lib/backupsss/tar.rb', line 57

def tar_command
  compress_archive ? 'tar -zcvf' : 'tar -cvf'
end

#valid_dest?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/backupsss/tar.rb', line 45

def valid_dest?
  dir_exists?(dest_dir) && dest_writable?
end

#valid_exit?(status, err) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
28
29
30
# File 'lib/backupsss/tar.rb', line 20

def valid_exit?(status, err)
  output = []
  output << "command.......#{tar_command}"
  output << "stderr........#{err}" unless err.empty?
  output << "status........#{status}"
  output << "exit code.....#{status.to_i}"
  $stdout.puts output.join("\n")

  return true if success_cases(status.to_i, err)
  raise "ERROR: #{tar_command} exited #{status.to_i}"
end

#valid_file?Boolean

Returns:

  • (Boolean)


32
33
34
35
36
# File 'lib/backupsss/tar.rb', line 32

def valid_file?
  raise messages[:no_file] unless File.exist?(dest)
  raise messages[:zero_byte] if File.size(dest).zero?
  true
end

#valid_src?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/backupsss/tar.rb', line 49

def valid_src?
  dir_exists?(src) && src_readable?
end