Class: Backup::RemoteArchive

Inherits:
Archive
  • Object
show all
Includes:
Utilities::Helpers, SSHKit::DSL
Defined in:
lib/backup/remote_archive.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) ⇒ RemoteArchive

Adds a new Archive to a Backup Model.

Backup::Model.new(:my_backup, 'My Backup') do
  remote_archive :my_archive do |archive|
    archive.add 'path/to/archive'
    archive.add '/another/path/to/archive'
    archive.exclude 'path/to/exclude'
    archive.exclude '/another/path/to/exclude'
  end
end

All paths added using ‘add` or `exclude` will be expanded to their full paths from the root of the filesystem. Files will be added to the tar archive using these full paths, and their leading `/` will be preserved (using tar’s ‘-P` option).

/path/to/pwd/path/to/archive/...
/another/path/to/archive/...

When a ‘root` path is given, paths to add/exclude are taken as relative to the `root` path, unless given as absolute paths.

Backup::Model.new(:my_backup, 'My Backup') do
  archive :my_archive do |archive|
    archive.root '~/my_data'
    archive.add 'path/to/archive'
    archive.add '/another/path/to/archive'
    archive.exclude 'path/to/exclude'
    archive.exclude '/another/path/to/exclude'
  end
end

This directs ‘tar` to change directories to the `root` path to create the archive. Unless paths were given as absolute, the paths within the archive will be relative to the `root` path.

path/to/archive/...
/another/path/to/archive/...

For absolute paths added to this archive, the leading ‘/` will be preserved. Take note that when archives are extracted, leading `/` are stripped by default, so care must be taken when extracting archives with mixed relative/absolute paths.



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
# File 'lib/backup/remote_archive.rb', line 70

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)

  # server options
  self.server_host = @options[:server_host]
  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
end

Instance Attribute Details

#server_backup_pathObject

Returns the value of attribute server_backup_path.



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

def server_backup_path
  @server_backup_path
end

#server_hostObject

server options



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

def server_host
  @server_host
end

#server_ssh_optionsObject

Returns the value of attribute server_ssh_options.



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

def server_ssh_options
  @server_ssh_options
end

Instance Method Details

#perform!Object



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
# File 'lib/backup/remote_archive.rb', line 102

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

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


  #
  remote = Backup::Remote::Command.new

  pipeline = Pipeline.new
  with_files_from(paths_to_package) do |files_from|
    # upload to server
    res_upload = remote.ssh_upload_file(server_host, server_ssh_options, files_from, files_from)

    if res_upload[:res]==0
      raise "Cannot upload file to server - #{files_from}"
    end

    #
    pipeline.add(
      "#{ tar_command } #{ tar_options } -cPf -#{ tar_root } " +
      "#{ paths_to_exclude } -T '#{ files_from }'",
      tar_success_codes
    )

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

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


    #pipeline.run

    # generate backup on remote server
    cmd_remote = pipeline.commands.join(" | ")

    #puts "remote cmd: #{cmd_remote}"
    #exit


    res_generate = remote.run_ssh_cmd(server_host, server_ssh_options, 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_options, remote_archive_file, archive_file)

    #puts "res: #{res_download}"

    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}")

  end

  Logger.info "Archive '#{ name }' Complete!"

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