Package Products :: Package ZenUtils :: Module ZenBackupBase |
[hide private]
[frames] | no frames]

# Source Code for Module Products.ZenUtils.ZenBackupBase

      1  #! /usr/bin/env python
      2  ###########################################################################
      3  #
      4  # This program is part of Zenoss Core, an open source monitoring platform.
      5  # Copyright (C) 2007, 2009 Zenoss Inc.
      6  #
      7  # This program is free software; you can redistribute it and/or modify it
      8  # under the terms of the GNU General Public License version 2 as published by
      9  # the Free Software Foundation.
     10  #
     11  # For complete information please visit: http://www.zenoss.com/oss/
     12  #
     13  ###########################################################################
     14
     15
     16  __doc__='''ZenBackupBase
     17
     18  Common code for zenbackup.py and zenrestore.py
     19  '''
     20  import tempfile
     21  from subprocess import Popen, PIPE
     22
     23  from CmdBase import CmdBase
     24
     25
     26  BACKUP_DIR = 'zenbackup'
     27  CONFIG_FILE = 'backup.settings'
     28  CONFIG_SECTION = 'zenbackup'
     29                        # cmd-line flag, default, zem attribute
     30  CONFIG_FIELDS = (   ('dbname', 'events', 'database'),
     31                      ('dbuser', 'root', 'username'),
     32                      ('dbpass', '', 'password'),
     33                      ('dbhost', 'localhost', 'host'),
     34                      ('dbport', '3306', 'port'),
     35                      )
     36
     37
     38 -class ZenBackupBase(CmdBase):
     39      doesLogging = False
     40
     41
     42 -    def __init__(self, noopts=0):
     43          CmdBase.__init__(self, noopts)
     44
     45 -    def msg(self, msg):
     46          '''
     47          If --verbose then send msg to stdout
     48          '''
     49          if self.options.verbose:
     50              print(msg)
     51
     52
     53 -    def buildOptions(self):
     54          """
     55          Command-line options setup
     56          """
     57          CmdBase.buildOptions(self)
     58          self.parser.add_option('-v', '--verbose',
     59                                 dest="verbose",
     60                                 default=False,
     61                                 action='store_true',
     62                                 help='Send progress messages to stdout.')
     63          self.parser.add_option('--temp-dir',
     64                                 dest="tempDir",
     65                                 default=None,
     66                                 help='Directory to use for temporary storage.')
     67
     68
     69 -    def getPassArg(self):
     70          '''
     71          Return string to be used as the -p (including the "-p")
     72          to MySQL commands.
     73
     74          @return: password and flag
     75          @rtype: string
     76          '''
     77          if self.options.dbpass == None:
     78              return ''
     79          return '--password="%s"' % self.options.dbpass
     80
     81
     82 -    def getTempDir(self):
     83          '''
     84          Return directory to be used for temporary storage
     85          during backup or restore.
     86
     87          @return: directory name
     88          @rtype: string
     89          '''
     90          if self.options.tempDir:
     91              dir = tempfile.mkdtemp('', '', self.options.tempDir)
     92          else:
     93              dir = tempfile.mkdtemp()
     94          return dir
     95
     96
     97 -    def runCommand(self, cmd=[], obfuscated_cmd=None):
     98          """
     99          Execute a command and return the results, displaying pre and
    100          post messages.
    101
    102          @parameter cmd: command to run
    103          @type cmd: list
    104          @return: results of the command (output, warnings, returncode)
    105          """
    106          if obfuscated_cmd:
    107              self.log.debug(' '.join(obfuscated_cmd))
    108          else:
    109              self.log.debug(' '.join(cmd))
    110
    111          proc = Popen(cmd, stdout=PIPE, stderr=PIPE)
    112          output, warnings = proc.communicate()
    113          if proc.returncode:
    114              self.log.warn(warnings)
    115          self.log.debug(output or 'No output from command')
    116          return (output, warnings, proc.returncode)
    117
    118 -    def runMysqlCmd(self, sql, switchDB=False):
    119          """
    120          Run a command that executes SQL statements in MySQL.
    121          Return true if the command was able to complete, otherwise
    122          (eg permissions or login error), return false.
    123
    124          @parameter sql: an executable SQL statement
    125          @type sql: string
    126          @parameter switchDB: use -D options.dbname to switch to a database?
    127          @type switchDB: boolean
    128          @return: boolean
    129          @rtype: boolean
    130          """
    131          cmd = ['mysql', '-u', self.options.dbuser]
    132
    133          obfuscatedCmd = None
    134          if self.options.dbpass:
    135              cmd.append('--password=%s' % self.options.dbpass)
    136
    137          if self.options.dbhost and self.options.dbhost != 'localhost':
    138              cmd.append('--host=%s' % self.options.dbhost)
    139          if self.options.dbport and self.options.dbport != '3306':
    140              cmd.append('--port=%s' % self.options.dbport)
    141
    142          if switchDB:
    143              cmd.extend(['-D', self.options.dbname])
    144
    145          cmd.extend(['-e', sql])
    146
    147          if self.options.dbpass:
    148              obfuscatedCmd = cmd[:]
    149              obfuscatedCmd[3] = '--pasword=%s' % ('*' * 8)
    150
    151          result = self.runCommand(cmd, obfuscated_cmd=obfuscatedCmd)
    152          if result[2]: # Return code from the command
    153              return False
    154          return True
    155