CedarBackup2.actions.rebuild

Package CedarBackup2 :: Package actions :: Module rebuild |
[hide private]
[frames] | no frames]

# Source Code for Module CedarBackup2.actions.rebuild

      1  # -*- coding: iso-8859-1 -*-
      2  # vim: set ft=python ts=3 sw=3 expandtab:
      3  # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
      4  #
      5  #              C E D A R
      6  #          S O L U T I O N S       "Software done right."
      7  #           S O F T W A R E
      8  #
      9  # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
     10  #
     11  # Copyright (c) 2004-2007,2010 Kenneth J. Pronovici.
     12  # All rights reserved.
     13  #
     14  # This program is free software; you can redistribute it and/or
     15  # modify it under the terms of the GNU General Public License,
     16  # Version 2, as published by the Free Software Foundation.
     17  #
     18  # This program is distributed in the hope that it will be useful,
     19  # but WITHOUT ANY WARRANTY; without even the implied warranty of
     20  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
     21  #
     22  # Copies of the GNU General Public License are available from
     23  # the Free Software Foundation website, http://www.gnu.org/.
     24  #
     25  # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
     26  #
     27  # Author   : Kenneth J. Pronovici <pronovic@ieee.org>
     28  # Language : Python 2 (>= 2.7)
     29  # Project  : Cedar Backup, release 2
     30  # Purpose  : Implements the standard 'rebuild' action.
     31  #
     32  # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
     33
     34  ########################################################################
     35  # Module documentation
     36  ########################################################################
     37
     38  """
     39  Implements the standard 'rebuild' action.
     40  @sort: executeRebuild
     41  @author: Kenneth J. Pronovici <pronovic@ieee.org>
     42  """
     43
     44
     45  ########################################################################
     46  # Imported modules
     47  ########################################################################
     48
     49  # System modules
     50  import sys
     51  import os
     52  import logging
     53  import datetime
     54
     55  # Cedar Backup modules
     56  from CedarBackup2.util import deriveDayOfWeek
     57  from CedarBackup2.actions.util import checkMediaState
     58  from CedarBackup2.actions.constants import DIR_TIME_FORMAT, STAGE_INDICATOR
     59  from CedarBackup2.actions.store import writeImage, writeStoreIndicator, consistencyCheck
     60
     61
     62  ########################################################################
     63  # Module-wide constants and variables
     64  ########################################################################
     65
     66  logger = logging.getLogger("CedarBackup2.log.actions.rebuild")
     67
     68
     69  ########################################################################
     70  # Public functions
     71  ########################################################################
     72
     73  ############################
     74  # executeRebuild() function
     75  ############################
     76
     77 -def executeRebuild(configPath, options, config):
     78     """
     79     Executes the rebuild backup action.
     80
     81     This function exists mainly to recreate a disc that has been "trashed" due
     82     to media or hardware problems.  Note that the "stage complete" indicator
     83     isn't checked for this action.
     84
     85     Note that the rebuild action and the store action are very similar.  The
     86     main difference is that while store only stores a single day's staging
     87     directory, the rebuild action operates on multiple staging directories.
     88
     89     @param configPath: Path to configuration file on disk.
     90     @type configPath: String representing a path on disk.
     91
     92     @param options: Program command-line options.
     93     @type options: Options object.
     94
     95     @param config: Program configuration.
     96     @type config: Config object.
     97
     98     @raise ValueError: Under many generic error conditions
     99     @raise IOError: If there are problems reading or writing files.
    100     """
    101     logger.debug("Executing the 'rebuild' action.")
    102     if sys.platform == "darwin":
    103        logger.warn("Warning: the rebuild action is not fully supported on Mac OS X.")
    104        logger.warn("See the Cedar Backup software manual for further information.")
    105     if config.options is None or config.store is None:
    106        raise ValueError("Rebuild configuration is not properly filled in.")
    107     if config.store.checkMedia:
    108        checkMediaState(config.store)  # raises exception if media is not initialized
    109     stagingDirs = _findRebuildDirs(config)
    110     writeImage(config, True, stagingDirs)
    111     if config.store.checkData:
    112        if sys.platform == "darwin":
    113           logger.warn("Warning: consistency check cannot be run successfully on Mac OS X.")
    114           logger.warn("See the Cedar Backup software manual for further information.")
    115        else:
    116           logger.debug("Running consistency check of media.")
    117           consistencyCheck(config, stagingDirs)
    118     writeStoreIndicator(config, stagingDirs)
    119     logger.info("Executed the 'rebuild' action successfully.")
    120
    121
    122  ########################################################################
    123  # Private utility functions
    124  ########################################################################
    125
    126  ##############################
    127  # _findRebuildDirs() function
    128  ##############################
    129
    130 -def _findRebuildDirs(config):
    131     """
    132     Finds the set of directories to be included in a disc rebuild.
    133
    134     A the rebuild action is supposed to recreate the "last week's" disc.  This
    135     won't always be possible if some of the staging directories are missing.
    136     However, the general procedure is to look back into the past no further than
    137     the previous "starting day of week", and then work forward from there trying
    138     to find all of the staging directories between then and now that still exist
    139     and have a stage indicator.
    140
    141     @param config: Config object.
    142
    143     @return: Correct staging dir, as a dict mapping directory to date suffix.
    144     @raise IOError: If we do not find at least one staging directory.
    145     """
    146     stagingDirs = {}
    147     start = deriveDayOfWeek(config.options.startingDay)
    148     today = datetime.date.today()
    149     if today.weekday() >= start:
    150        days = today.weekday() - start + 1
    151     else:
    152        days = 7 - (start - today.weekday()) + 1
    153     for i in range (0, days):
    154        currentDay = today - datetime.timedelta(days=i)
    155        dateSuffix = currentDay.strftime(DIR_TIME_FORMAT)
    156        stageDir = os.path.join(config.store.sourceDir, dateSuffix)
    157        indicator = os.path.join(stageDir, STAGE_INDICATOR)
    158        if os.path.isdir(stageDir) and os.path.exists(indicator):
    159           logger.info("Rebuild process will include stage directory [%s]", stageDir)
    160           stagingDirs[stageDir] = dateSuffix
    161     if len(stagingDirs) == 0:
    162        raise IOError("Unable to find any staging directories for rebuild process.")
    163     return stagingDirs
    164