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