class SimpleSshBackup::BackupManager
Public Class Methods
new(config)
click to toggle source
# File lib/simple_ssh_backup/backup_manager.rb, line 4 def initialize(config) raise ArgumentError, 'Config is not a config object' unless config.is_a?(Config) @config = config end
Public Instance Methods
backup_all()
click to toggle source
# File lib/simple_ssh_backup/backup_manager.rb, line 10 def backup_all ensure_backup_dir @config.targets.each do |target| backup_target(target) end end
backup_target(target)
click to toggle source
# File lib/simple_ssh_backup/backup_manager.rb, line 18 def backup_target(target) raise ArgumentError, 'target must be a hash' unless target.is_a?(Hash) puts "Processing #{target['name']}" delete_old_backups(target) ensure_backup_dir(target['name']) delete_temp_dir create_temp_dir check_ssh_connection(target) download_to_temp(target) create_archive(target) delete_temp_dir end
Protected Instance Methods
backup_path(name)
click to toggle source
# File lib/simple_ssh_backup/backup_manager.rb, line 39 def backup_path(name) "#{@config.backup_dir}/#{name}" end
check_ssh_connection(target)
click to toggle source
# File lib/simple_ssh_backup/backup_manager.rb, line 51 def check_ssh_connection(target) raise ArgumentError, 'target must be a hash' unless target.is_a?(Hash) ssh = ssh_connection(target) execute_ssh_command(ssh, 'uname -a') end
create_archive(target)
click to toggle source
# File lib/simple_ssh_backup/backup_manager.rb, line 117 def create_archive(target) raise ArgumentError, 'target must be a hash' unless target.is_a?(Hash) target_dir = backup_path(target['name']) temp_path = backup_path('temp') time = Time.new filename = "#{target['name']}_#{time.year}-#{time.month}-#{time.day}_#{time.hour}-#{time.min}-#{time.sec}.tar.gz" `cd #{temp_path} && tar cfz #{target_dir}/#{filename} .` end
create_temp_dir()
click to toggle source
# File lib/simple_ssh_backup/backup_manager.rb, line 93 def create_temp_dir ensure_backup_dir('temp') end
delete_old_backups(target)
click to toggle source
# File lib/simple_ssh_backup/backup_manager.rb, line 129 def delete_old_backups(target) raise ArgumentError, 'target must be a hash' unless target.is_a?(Hash) target_path = backup_path(target['name']) file_list = Dir["#{target_path}/*.tar.gz"].sort_by{ |f| File.mtime(f) }.reverse! amount_to_keep = target['keep_amount'].to_i iterate = 0 file_list.each do |file| iterate += 1 if iterate > amount_to_keep puts "Deleting backup #{File.basename(file)}" FileUtils.remove(file) end end end
delete_temp_dir()
click to toggle source
# File lib/simple_ssh_backup/backup_manager.rb, line 88 def delete_temp_dir temp_path = backup_path('temp') FileUtils.rm_rf(temp_path) end
download_to_temp(target)
click to toggle source
# File lib/simple_ssh_backup/backup_manager.rb, line 97 def download_to_temp(target) raise ArgumentError, 'target must be a hash' unless target.is_a?(Hash) temp_path = backup_path('temp') old_filename = nil progressbar = ::ProgressBar.create(title: 'Preparing download', length: 100, format: "%t: |%B| %P%%") scp = scp_connection(target) scp.download!("#{target['remote_path']}/.", temp_path, :recursive => true) do |_, filename, sent, total| if old_filename != filename old_filename = filename progressbar = ::ProgressBar.create(title: File.basename(filename), length: 100, format: "%t: |%B| %P%%") end progressbar.total = total progressbar.progress = sent end end
ensure_backup_dir(name = '')
click to toggle source
# File lib/simple_ssh_backup/backup_manager.rb, line 43 def ensure_backup_dir(name = '') target = backup_path(name) if File.exists?(target) == false || File.directory?(target) == false FileUtils.mkpath(target) end end
execute_ssh_command(con, cmd)
click to toggle source
# File lib/simple_ssh_backup/backup_manager.rb, line 84 def execute_ssh_command(con, cmd) con.exec!(cmd) end
scp_connection(target)
click to toggle source
# File lib/simple_ssh_backup/backup_manager.rb, line 71 def scp_connection(target) raise ArgumentError, 'target must be a hash' unless target.is_a?(Hash) con = nil if target['auth_method'] == 'publickey' con = Net::SCP.start(target['hostname'], target['username'], :keys => [target['private_key']]) else raise RuntimeError, 'auth_method is not supported' end con end
ssh_connection(target)
click to toggle source
# File lib/simple_ssh_backup/backup_manager.rb, line 58 def ssh_connection(target) raise ArgumentError, 'target must be a hash' unless target.is_a?(Hash) con = nil if target['auth_method'] == 'publickey' con = Net::SSH.start(target['hostname'], target['username'], :keys => [target['private_key']]) else raise RuntimeError, 'auth_method is not supported' end con end