Class: Backup::RemoteData
- Includes:
- Utilities::Helpers, SSHKit::DSL
- Defined in:
- lib/backup/remote_data.rb
Defined Under Namespace
Instance Attribute Summary collapse
-
#script ⇒ Object
Returns the value of attribute script.
-
#server_command ⇒ Object
Returns the value of attribute server_command.
-
#server_host ⇒ Object
server options.
-
#server_path ⇒ Object
Returns the value of attribute server_path.
-
#server_ssh_key ⇒ Object
Returns the value of attribute server_ssh_key.
-
#server_ssh_password ⇒ Object
Returns the value of attribute server_ssh_password.
-
#server_ssh_user ⇒ Object
Returns the value of attribute server_ssh_user.
-
#temp_dir_path ⇒ Object
Returns the value of attribute temp_dir_path.
Attributes inherited from Archive
Instance Method Summary collapse
-
#initialize(model, name, &block) ⇒ RemoteData
constructor
A new instance of RemoteData.
- #perform! ⇒ Object
Methods included from Utilities::Helpers
Constructor Details
#initialize(model, name, &block) ⇒ RemoteData
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/backup/remote_data.rb', line 33 def initialize(model, name, &block) @model = model @name = name.to_s = { :sudo => false, :root => false, :paths => [], :excludes => [], :tar_options => '' } DSL.new().instance_eval(&block) # self.server_host = [:server_host] self.server_ssh_user = [:server_ssh_user] self.server_ssh_password = [:server_ssh_password] self.server_path = [:server_path] self.server_command = [:server_command] self.script = [:script] self.temp_dir_path = [:temp_dir_path] || '/tmp' end |
Instance Attribute Details
#script ⇒ Object
Returns the value of attribute script.
26 27 28 |
# File 'lib/backup/remote_data.rb', line 26 def script @script end |
#server_command ⇒ Object
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_host ⇒ Object
server options
20 21 22 |
# File 'lib/backup/remote_data.rb', line 20 def server_host @server_host end |
#server_path ⇒ Object
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_key ⇒ Object
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_password ⇒ Object
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_user ⇒ Object
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 |
#temp_dir_path ⇒ Object
Returns the value of attribute temp_dir_path.
27 28 29 |
# File 'lib/backup/remote_data.rb', line 27 def temp_dir_path @temp_dir_path end |
Instance Method Details
#perform! ⇒ 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 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 |
# File 'lib/backup/remote_data.rb', line 56 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_user, server_ssh_password, 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_user, server_ssh_password, cmd_remote) # delete temp script cmd_delete = "rm -rf #{remote_script_path}" res_delete = remote.run_ssh_cmd(server_host, server_ssh_user, server_ssh_password, cmd_delete) else # use command cmd_remote = server_command res_generate = remote.run_ssh_cmd( server_host, server_ssh_user, server_ssh_password, cmd_remote) end if res_generate[:res]==0 raise 'Cannot create backup on server' 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_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_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. end if File.exists?(temp_local_file) FileUtils.rm_rf(temp_local_file) FileUtils.rm_rf(File.dirname(temp_local_file)) end end |