class MTik_backup
MikroTik backup class noinspection RubyArgCount
Public Class Methods
new(config)
click to toggle source
Initialize
# File lib/mikrotik-backup.rb, line 8 def initialize(config) @config = config @config.each do |c| unless c[:port].is_a? Integer c[:port] = 22 end end logger end
Public Instance Methods
backup()
click to toggle source
Backup MTik config
# File lib/mikrotik-backup.rb, line 30 def backup @config.each do |config| connect_to_host(config[:host],config[:port],config[:user],config[:password]) backup_config(config[:name],config[:format]) end return true end
backup_and_download()
click to toggle source
Backup and download MTik config
# File lib/mikrotik-backup.rb, line 45 def backup_and_download @config.each do |config| connect_to_host(config[:host],config[:port],config[:user],config[:password]) backup_config(config[:name],config[:format]) download_backup(config[:host],config[:port],config[:user],config[:password],config[:name],config[:path],config[:format]) end return true end
download()
click to toggle source
Only download MTik config
# File lib/mikrotik-backup.rb, line 38 def download @config.each do |config| download_backup(config[:host],config[:port],config[:user],config[:password],config[:name],config[:path],config[:format]) end return true end
logger(log_level = Logger::ERROR,log_file = nil)
click to toggle source
Setting for Ruby Logger
# File lib/mikrotik-backup.rb, line 18 def logger(log_level = Logger::ERROR,log_file = nil) if log_file.nil? @log = Logger.new(STDOUT) else @log = Logger.new(log_file) end @log.level = log_level @log.formatter = proc { |severity, datetime, progname, msg| "[#{datetime.strftime("%Y-%m-%d %H:%M:%S")}] #{severity}: #{msg}\n" } end
Private Instance Methods
backup_config(name,format='binary')
click to toggle source
Backup function, send SSH command
# File lib/mikrotik-backup.rb, line 72 def backup_config(name,format='binary') @log.info("Backup MikroTik configuration") if format == 'binary' send_command("/system backup save name=#{name}") elsif format == 'script' send_command("/export file=#{name}") end @ssh_connect.close end
connect_to_host(host,port,user,password,sftp=false)
click to toggle source
Function connect to SSH/SFTP host
# File lib/mikrotik-backup.rb, line 57 def connect_to_host(host,port,user,password,sftp=false) begin if sftp @log.info("SFTP connect to host #{host}:#{port}") @ssh_connect = Net::SFTP.start(host,user,:password=>password,:port=>port) else @log.info("SSH connect to host #{host}:#{port}") @ssh_connect = Net::SSH.start(host,user,:password=>password,:port=>port) end rescue Exception => error @log.error("#{error}") exit end end
download_backup(host,port,user,password,name,path,format)
click to toggle source
Download backup file from host
# File lib/mikrotik-backup.rb, line 82 def download_backup(host,port,user,password,name,path,format) connect_to_host(host,port,user,password,true) if format == 'binary' local_file = path+name+".backup" remote_file = name+".backup" elsif format== 'script' local_file = path+name+".rsc" remote_file = name+".rsc" end download_file(remote_file,local_file) @ssh_connect.close(@ssh_connect) end
download_file(remote_file, local_file)
click to toggle source
Download file from host
# File lib/mikrotik-backup.rb, line 103 def download_file(remote_file, local_file) begin @log.info("Download file #{remote_file} to #{local_file}") @ssh_connect.download!(remote_file,local_file) rescue Exception => error @log.error("#{error}") exit end end
send_command(command)
click to toggle source
Send command to host (SSH)
# File lib/mikrotik-backup.rb, line 95 def send_command(command) begin @ssh_connect.exec!(command) rescue Exception => error @log.error("#{error}") end end