Class: Backup::RemoteData

Inherits:
Archive
  • Object
show all
Includes:
Utilities::Helpers, SSHKit::DSL
Defined in:
lib/backup/remote_data.rb

Defined Under Namespace

Classes: DSL, Error

Instance Attribute Summary collapse

Attributes inherited from Archive

#name, #options

Instance Method Summary collapse

Constructor Details

#initialize(model, name, &block) ⇒ RemoteData

Returns a new instance of RemoteData.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/backup/remote_data.rb', line 31

def initialize(model, name, &block)
  @model   = model
  @name    = name.to_s
  @options = {
    :sudo        => false,
    :root        => false,
    :paths       => [],
    :excludes    => [],
    :tar_options => ''
  }

  DSL.new(@options).instance_eval(&block)

  #
  self.server_host = @options[:server_host]
  self.server_ssh_user = @options[:server_ssh_user]
  self.server_ssh_password = @options[:server_ssh_password]
  self.server_path = @options[:server_path]
  self.server_command = @options[:server_command]
end

Instance Attribute Details

#server_commandObject

Returns the value of attribute server_command.



25
26
27
# File 'lib/backup/remote_data.rb', line 25

def server_command
  @server_command
end

#server_hostObject

server options



20
21
22
# File 'lib/backup/remote_data.rb', line 20

def server_host
  @server_host
end

#server_pathObject

Returns the value of attribute server_path.



24
25
26
# File 'lib/backup/remote_data.rb', line 24

def server_path
  @server_path
end

#server_ssh_keyObject

Returns the value of attribute server_ssh_key.



23
24
25
# File 'lib/backup/remote_data.rb', line 23

def server_ssh_key
  @server_ssh_key
end

#server_ssh_passwordObject

Returns the value of attribute server_ssh_password.



22
23
24
# File 'lib/backup/remote_data.rb', line 22

def server_ssh_password
  @server_ssh_password
end

#server_ssh_userObject

Returns the value of attribute server_ssh_user.



21
22
23
# File 'lib/backup/remote_data.rb', line 21

def server_ssh_user
  @server_ssh_user
end

Instance Method Details

#perform!Object



52
53
54
55
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
# File 'lib/backup/remote_data.rb', line 52

def perform!
  Logger.info "Creating Archive '#{ name }'..."

  # local archive
  path = File.join(Config.tmp_path, @model.trigger, 'archives')
  FileUtils.mkdir_p(path)

  extension = 'tar'
  #temp_archive_file = File.join(path, "#{ name }.#{ extension }")

  remote = Backup::Remote::Command.new


  Dir.mktmpdir do |temp_dir|
    temp_local_file = File.join("#{temp_dir}", File.basename(server_path))
    #temp_local_file = File.join(path, File.basename(server_path))
    #temp_local_file = Tempfile.new("").path+"."+File.extname(server_path)

    remote_archive_file = server_path

    # generate backup on remote server
    cmd_remote = server_command
    res_generate = remote.run_ssh_cmd(
        server_host, server_ssh_user, server_ssh_password,
        cmd_remote)

    if res_generate[:res]==0
      raise 'Cannot create backup on server'
    end

    # download backup
    res_download = remote.ssh_download_file(
        server_host, server_ssh_user, server_ssh_password,
        remote_archive_file, temp_local_file)

    if res_download[:res]==0
      raise 'Cannot download file from server'
    end

    # delete archive on server
    res_delete = remote.run_ssh_cmd(
        server_host, server_ssh_user, server_ssh_password,
        "rm #{remote_archive_file}")


    # process archive locally

    pipeline = Pipeline.new

    #temp_tar_root= tar_root
    temp_tar_root= temp_dir
    pipeline.add(
        "#{ tar_command } #{ tar_options } -cPf - -C #{temp_tar_root } #{ File.basename(temp_local_file) }",
        tar_success_codes
    )

    extension = 'tar'
    @model.compressor.compress_with do |command, ext|
      pipeline << command
      extension << ext
    end if @model.compressor

    pipeline << "#{ utility(:cat) } > '#{ File.join(path, "#{ name }.#{ extension }") }'"

    #puts "commands: #{pipeline.commands}"
    #exit

    pipeline.run


    if pipeline.success?
      Logger.info "Archive '#{ name }' Complete!"
    else
      raise Error, "Failed to Create Archive '#{ name }'\n" +
          pipeline.error_messages
    end

  end
end