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  # Copyright (C) Zenoss, Inc. 2007, 2009, all rights reserved.
      5  #
      6  # This content is made available according to terms specified in
      7  # License.zenoss under the directory where your Zenoss product is installed.
      8  #
      9  ##############################################################################
     10
     11
     12  __doc__='''ZenBackupBase
     13
     14  Common code for zenbackup.py and zenrestore.py
     15  '''
     16  import tempfile
     17  from subprocess import Popen, PIPE
     18
     19  from zope.component import getUtility
     20  from Products.ZenUtils.ZodbFactory import IZodbFactoryLookup
     21  from Products.ZenUtils.GlobalConfig import globalConfToDict
     22  from CmdBase import CmdBase
     23
     24
     25  BACKUP_DIR = 'zenbackup'
     26  CONFIG_FILE = 'backup.settings'
     27  CONFIG_SECTION = 'zenbackup'
     28
     29 -class ZenBackupBase(CmdBase):
     30      doesLogging = False
     31
     32
     33 -    def __init__(self, noopts=0):
     34          CmdBase.__init__(self, noopts)
     35
     36 -    def msg(self, msg):
     37          '''
     38          If --verbose then send msg to stdout
     39          '''
     40          if self.options.verbose:
     41              print(msg)
     42
     43
     44 -    def buildOptions(self):
     45          """
     46          Command-line options setup
     47          """
     48          CmdBase.buildOptions(self)
     49          connectionFactory = getUtility(IZodbFactoryLookup).get()
     50          connectionFactory.buildOptions(self.parser)
     51          self.parser.add_option('-v', '--verbose',
     52                                 dest="verbose",
     53                                 default=False,
     54                                 action='store_true',
     55                                 help='Send progress messages to stdout.')
     56          self.parser.add_option('--temp-dir',
     57                                 dest="tempDir",
     58                                 default=None,
     59                                 help='Directory to use for temporary storage.')
     60          self.parser.add_option('--dont-fetch-args',
     61                                  dest='fetchArgs',
     62                                  default=True,
     63                                  action='store_false',
     64                                  help='By default MySQL connection information'
     65                                      ' is retrieved from Zenoss if not'
     66                                      ' specified and if Zenoss is available.'
     67                                      ' This disables fetching of these values'
     68                                      ' from Zenoss.')
     69          self.parser.add_option('--zepdbname',
     70                                 dest='zepdbname',
     71                                 default='zenoss_zep',
     72                                 help='ZEP database name.'
     73                                  ' By default this will be fetched from Zenoss'
     74                                  ' unless --dont-fetch-args is set.'),
     75          self.parser.add_option('--zepdbuser',
     76                                 dest='zepdbuser',
     77                                 default='zenoss',
     78                                 help='ZEP database username.'
     79                                  ' By default this will be fetched from Zenoss'
     80                                  ' unless --dont-fetch-args is set.'),
     81          self.parser.add_option('--zepdbpass',
     82                                 dest='zepdbpass',
     83                                 default='zenoss',
     84                                 help='ZEP database password.'
     85                                  ' By default this will be fetched from Zenoss'
     86                                  ' unless --dont-fetch-args is set.'),
     87          self.parser.add_option('--zepdbhost',
     88                                 dest='zepdbhost',
     89                                 default='localhost',
     90                                 help='ZEP database server host.'
     91                                  ' By default this will be fetched from Zenoss'
     92                                  ' unless --dont-fetch-args is set.'),
     93          self.parser.add_option('--zepdbport',
     94                                 dest='zepdbport',
     95                                 default='3306',
     96                                 help='ZEP database server port number.'
     97                                  ' By default this will be fetched from Zenoss'
     98                                  ' unless --dont-fetch-args is set.'),
     99          self.parser.add_option('--compress-transport',
    100                                 dest="compressTransport",
    101                                 default=True,
    102                                 help='Compress transport for MySQL backup/restore.'
    103                                 ' True by default, set to False to disable over'
    104                                 ' fast links that do not benefit from compression.')
    105
    106
    107 -    def getPassArg(self, optname='zodb_password'):
    108          """
    109          Return string to be used as the -p (including the "-p")
    110          to MySQL commands.
    111
    112          @return: password and flag
    113          @rtype: string
    114          """
    115          password = getattr(self.options, optname, None)
    116          if not password:
    117              return []
    118          return ['-p%s' % password]
    119
    120
    121 -    def getTempDir(self):
    122          """
    123          Return directory to be used for temporary storage
    124          during backup or restore.
    125
    126          @return: directory name
    127          @rtype: string
    128          """
    129          if self.options.tempDir:
    130              dir = tempfile.mkdtemp('', '', self.options.tempDir)
    131          else:
    132              dir = tempfile.mkdtemp()
    133          return dir
    134
    135
    136 -    def readZEPSettings(self):
    137          '''
    138          Read in and store the ZEP DB configuration options
    139          to the 'options' object.
    140          '''
    141          globalSettings = globalConfToDict()
    142          zepsettings = {
    143              'zep-user': 'zepdbuser',
    144              'zep-host': 'zepdbhost',
    145              'zep-db': 'zepdbname',
    146              'zep-password': 'zepdbpass',
    147              'zep-port': 'zepdbport'
    148          }
    149
    150          for key in zepsettings:
    151              if key in globalSettings:
    152                  value = str(globalSettings[key])
    153                  setattr(self.options, zepsettings[key], value)
    154
    155
    156 -    def runCommand(self, cmd=[], obfuscated_cmd=None):
    157          """
    158          Execute a command and return the results, displaying pre and
    159          post messages.
    160
    161          @parameter cmd: command to run
    162          @type cmd: list
    163          @return: results of the command (output, warnings, returncode)
    164          """
    165          if obfuscated_cmd:
    166              self.log.debug(' '.join(obfuscated_cmd))
    167          else:
    168              self.log.debug(' '.join(cmd))
    169          proc = Popen(cmd, stdout=PIPE, stderr=PIPE)
    170          output, warnings = proc.communicate()
    171          if proc.returncode:
    172              self.log.warn(warnings)
    173          self.log.debug(output or 'No output from command')
    174          return (output, warnings, proc.returncode)
    175