module Backup::Utilities

Constants

NAMES
UTILITY

Public Class Methods

configure(&block) click to toggle source

Configure the path to system utilities used by Backup.

Backup will attempt to locate any required system utilities using a which command call. If a utility can not be found, or you need to specify an alternate path for a utility, you may do so in your config.rb file using this method.

Backup supports both GNU and BSD utilities. While Backup uses these utilities in a manner compatible with either version, the tar utility requires some special handling with respect to +Archive+s. Backup will attempt to detect if the tar command found (or set here) is GNU or BSD. If for some reason this fails, this may be set using the tar_dist command shown below.

Backup::Utilities.configure do
  # General Utilites
  tar      '/path/to/tar'
  tar_dist :gnu   # or :bsd
  cat      '/path/to/cat'
  split    '/path/to/split'
  sudo     '/path/to/sudo'
  chown    '/path/to/chown'
  hostname '/path/to/hostname'

  # Compressors
  gzip    '/path/to/gzip'
  bzip2   '/path/to/bzip2'

  # Database Utilities
  mongo       '/path/to/mongo'
  mongodump   '/path/to/mongodump'
  mysqldump   '/path/to/mysqldump'
  pg_dump     '/path/to/pg_dump'
  pg_dumpall  '/path/to/pg_dumpall'
  redis_cli   '/path/to/redis-cli'
  riak_admin  '/path/to/riak-admin'

  # Encryptors
  gpg     '/path/to/gpg'
  openssl '/path/to/openssl'

  # Syncer and Storage
  rsync   '/path/to/rsync'
  ssh     '/path/to/ssh'

  # Notifiers
  sendmail  '/path/to/sendmail'
  exim      '/path/to/exim'
  send_nsca '/path/to/send_nsca'
  zabbix_sender '/path/to/zabbix_sender'
end

These paths may be set using absolute paths, or relative to the working directory when Backup is run.

# File lib/backup/utilities.rb, line 104
def configure(&block)
  DSL.instance_eval(&block)
end
gnu_tar?() click to toggle source
# File lib/backup/utilities.rb, line 113
def gnu_tar?
  return @gnu_tar unless @gnu_tar.nil?
  @gnu_tar = !!run("#{ utility(:tar) } --version").match(/GNU/)
end
tar_dist(val) click to toggle source
# File lib/backup/utilities.rb, line 108
def tar_dist(val)
  # the acceptance tests need to be able to reset this to nil
  @gnu_tar = val.nil? ? nil : val == :gnu
end

Private Class Methods

command_name(command) click to toggle source

Returns the name of the command name from the given command line. This is only used to simplify log messages.

# File lib/backup/utilities.rb, line 141
def command_name(command)
  parts = []
  command = command.split(' ')
  command.shift while command[0].to_s.include?('=')
  parts << command.shift.split('/')[-1]
  if parts[0] == 'sudo'
    until command.empty?
      part = command.shift
      if part.include?('/')
        parts << part.split('/')[-1]
        break
      else
        parts << part
      end
    end
  end
  parts.join(' ')
end
reset!() click to toggle source
# File lib/backup/utilities.rb, line 207
def reset!
  UTILITY.clear
  @gnu_tar = nil
end
run(command) click to toggle source

Runs a system command

All messages generated by the command will be logged. Messages on STDERR will be logged as warnings.

If the command fails to execute, or returns a non-zero exit status an Error will be raised.

Returns STDOUT

# File lib/backup/utilities.rb, line 170
      def run(command)
        name = command_name(command)
        Logger.info "Running system utility '#{ name }'..."

        begin
          out, err = '', ''
          ps = Open4.popen4(command) do |pid, stdin, stdout, stderr|
            stdin.close
            out, err = stdout.read.strip, stderr.read.strip
          end
        rescue Exception => e
          raise Error.wrap(e, "Failed to execute '#{ name }'")
        end

        if ps.success?
          unless out.empty?
            Logger.info(
              out.lines.map {|line| "#{ name }:STDOUT: #{ line }" }.join
            )
          end

          unless err.empty?
            Logger.warn(
              err.lines.map {|line| "#{ name }:STDERR: #{ line }" }.join
            )
          end

          return out
        else
          raise Error, <<-EOS
            '#{ name }' failed with exit status: #{ ps.exitstatus }
            STDOUT Messages: #{ out.empty? ? 'None' : "\n#{ out }" }
            STDERR Messages: #{ err.empty? ? 'None' : "\n#{ err }" }
          EOS
        end
      end
utility(name) click to toggle source

Returns the full path to the specified utility. Raises an error if utility can not be found in the system’s $PATH

# File lib/backup/utilities.rb, line 123
      def utility(name)
        name = name.to_s.strip
        raise Error, 'Utility Name Empty' if name.empty?

        UTILITY[name] ||= %x[which '#{ name }' 2>/dev/null].chomp
        raise Error, <<-EOS if UTILITY[name].empty?
          Could not locate '#{ name }'.
          Make sure the specified utility is installed
          and available in your system's $PATH, or specify it's location
          in your 'config.rb' file using Backup::Utilities.configure
        EOS

        UTILITY[name].dup
      end