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

Methods included from Utilities::Helpers

#utility_remote

Constructor Details

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

Returns a new instance of RemoteData.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/backup/remote_data.rb', line 32

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)

  # ssh options
  self.server_host = @options[:server_host]

  # server options
  ssh_options = {}
  v = @options[:server_ssh_user]
  ssh_options[:user] = v if v

  v = @options[:server_ssh_password]
  ssh_options[:password] = v if v

  v = @options[:server_ssh_key]
  ssh_options[:key] = v if v

  v = @options[:server_ssh_port]
  ssh_options[:port] = v if v


  self.server_ssh_options = ssh_options

  # server options
  self.server_path = @options[:server_path]
  self.server_command = @options[:server_command]
  self.script = @options[:script]
  self.temp_dir_path = @options[:temp_dir_path] || '/tmp'
end

Instance Attribute Details

#scriptObject

Returns the value of attribute script.



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

def script
  @script
end

#server_commandObject

Returns the value of attribute server_command.



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

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.



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

def server_path
  @server_path
end

#server_ssh_optionsObject

Returns the value of attribute server_ssh_options.



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

def server_ssh_options
  @server_ssh_options
end

#temp_dir_pathObject

Returns the value of attribute temp_dir_path.



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

def temp_dir_path
  @temp_dir_path
end

Instance Method Details

#perform!Object



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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/backup/remote_data.rb', line 72

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


  remote_archive_file = server_path

  # generate backup on remote server
  remote_script = script

  if remote_script && remote_script!=""
    # use script
    # upload script

    local_script_path = File.join(Config.root_path, remote_script)

    f_remote = Tempfile.new('backup')
    remote_script_path = f_remote.path+"."+File.extname(local_script_path)

    Logger.info "upload script #{local_script_path} --> #{remote_script_path}"

    remote.ssh_upload_file(server_host, server_ssh_options, local_script_path, remote_script_path)

    cmd_remote = "chmod +x #{remote_script_path} && sh #{remote_script_path}"
    res_generate = remote.run_ssh_cmd(server_host, server_ssh_options, cmd_remote)

    # delete temp script
    cmd_delete = "rm -rf #{remote_script_path}"
    res_delete = remote.run_ssh_cmd(server_host, server_ssh_options, cmd_delete)

  else
    # use command
    cmd_remote = server_command
    res_generate = remote.run_ssh_cmd(server_host, server_ssh_options, cmd_remote)
  end

  if res_generate[:res]==0
    raise "Cannot create backup on server: #{res_generate[:error]}"
  end



  #Dir.mktmpdir do |temp_dir|

  #temp_local_file = File.join("#{temp_dir}", File.basename(server_path))

  temp_local_file = File.join("#{temp_dir_path}/#{Time.now.to_i}#{rand(1000)}/", File.basename(server_path))

  #path = File.expand_path "#{Dir.tmpdir}/#{Time.now.to_i}#{rand(1000)}/"
  FileUtils.mkdir_p File.dirname(temp_local_file)

  # download backup
  Logger.info "download from #{remote_archive_file} to #{temp_local_file}"

  res_download = remote.ssh_download_file(server_host, server_ssh_options, 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_options, "rm #{remote_archive_file}")


  # process archive locally

  pipeline = Pipeline.new

  #temp_tar_root= tar_root
  #temp_tar_root= temp_dir_path
  temp_tar_root= File.dirname(temp_local_file)
  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

  if File.exists?(temp_local_file)
    FileUtils.rm_rf(temp_local_file)
    FileUtils.rm_rf(File.dirname(temp_local_file))
  end

end