class NixAdm::Backup
Base backup class. Provides centralized logging facility and job start/stop templates.
Attributes
Public Class Methods
NixAdm::Command::new
# File src/lib/nixadm/backup.rb, line 12 def initialize(host, options = {}) super(host, options) if not $backup_dir.nil? @logfile = File.open("#{$backup_dir}/backup.log", 'w') end end
Public Instance Methods
# File src/lib/nixadm/backup.rb, line 30 def endBackup() end
# File src/lib/nixadm/backup.rb, line 20 def run() startBackup() yield endBackup() end
# File src/lib/nixadm/backup.rb, line 26 def startBackup() end
Protected Instance Methods
# File src/lib/nixadm/backup.rb, line 116 def backup_dir(dir) return "#{$backup_dir}/#{dir}" end
# File src/lib/nixadm/backup.rb, line 40 def cd(path) Dir.chdir(path) end
# File src/lib/nixadm/backup.rb, line 209 def endDestZfsReplication(slave_object) return success() end
# File src/lib/nixadm/backup.rb, line 213 def endSourceZfsReplication(master_object) return success() end
Replicates ZFS
filesystem(s) on from host to backup hosts
@param host The main host we are backing up @param filesystems An array of ZFS
filesystems to replicate
@param targets An array or hashes defining target hosts to replicate to. It has the following form:
targets = [ { host: 'root@midway', root: 'pool' }, { host: 'root@backup.c21bowman.com', root: 'pool' }, { host: 'root@arc2', root: 'pool' } ]
# File src/lib/nixadm/backup.rb, line 132 def replicate(host, objects, targets) admin = NixAdm::ZFS::Admin.new(host) objects.each do |object| # First look for matching filesystem master_object = admin.object(object) if master_object.nil? raise "Error: #{object} does not exist" end startSourceZfsReplication master_object failed = false targets.each do | target | if target[:host].nil? or target[:pool].nil? raise "Invalid arguments: missing host or pool" end slave = NixAdm::ZFS::Host.new(target[:host]) pool = slave.pool(target[:pool]) attempts = 0 while attempts < 2 slave_object = pool.object(master_object.name) break if not slave_object.nil? # If we get here, it means the remote object does not exists. If this # is a filesystem, we must first create it. If it's a zvol, we do # nothing. $stderr.puts "Dest ZFS entity '#{pool.name}/#{master_object.name}' " + "does not exist ... creating" if master_object.is_a?(NixAdm::ZFS::Filesystem) if pool.createFilesystem(master_object.name) == false msg = "Error: ZFS entity '#{pool.name}/#{master_object.name}' " + \ "does not exist and could not be created" raise msg end elsif master_object.is_a?(NixAdm::ZFS::Volume) if pool.createVolume(master_object.name) == false msg = "Error: ZFS entity '#{pool.name}/#{master_object.name}' " + \ "does not exist and could not be created" raise msg end end attempts += 1 end if block_given? yield master_object, slave_object end if admin.replicate(master_object, slave_object) == true endDestZfsReplication slave_object else failed = true msg = "FAILED: #{job} - #{admin.status.msg}" log msg, admin.status.val end end if failed == false endSourceZfsReplication master_object end end end
# File src/lib/nixadm/backup.rb, line 44 def replicationStatus(mfs, sfs) h1 = mfs.pool.host.name p1 = mfs.pool.name f1 = mfs.name h2 = sfs.pool.host.name p2 = sfs.pool.name f2 = sfs.name return "replicating #{h1}:#{p1}/#{f1} -> #{h2}:#{p2}/#{f2}" end
Run general shell command and log output
# File src/lib/nixadm/backup.rb, line 56 def run_command(command) sys = NixAdm::Pipeline.new() sys.classic(command) do |stdin, stdout| stdin.close() while true line = stdout.gets break if line.nil? @logfile.write(line) end end end
# File src/lib/nixadm/backup.rb, line 205 def startSourceZfsReplication(master_object) return success() end
Executes a command, captures output into file @param command A command or array containing a pipeline of commands @param filename The name (path) of the file to dump database to @param zip If true, gzip the file.
# File src/lib/nixadm/backup.rb, line 81 def store(host, command, filename, zip: false) sys = NixAdm::Pipeline.new() pipeline = nil sys.debug = debug() # If the host we are performing this command on is the same host, don't # resolve with host as secong argument, as this just makes that host ssh # back into itself to run the command. if @host == host pipeline = resolveCommand(command) else pipeline = resolveCommand(command, host) end sys.run([pipeline]) do |stdin, stdout| stdin.close() dumpfile = File.open(filename, 'wb') while true data = stdout.read(1024) break if data == nil dumpfile.write(data) end dumpfile.close() end if zip == true # Compress the file sys.run "gzip -f #{filename}" end end
Trim snaphots on given filesystem
@param host The host name @param filesystems A list of filesystems to trim snapshots
# File src/lib/nixadm/backup.rb, line 221 def trim(host, objects) admin = NixAdm::ZFS::Admin.new(host) objects.each do |object| master_object = admin.object(object) if not master_object.nil? master_object.trimSnapshots() end end end
Executes a command, captures output into file and zips the file @param command A command or array containing a pipeline of commands @param filename The name (path) of the file to dump database to
# File src/lib/nixadm/backup.rb, line 73 def zipStore(host, command, filename) store host, command, filename, zip: true end