1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
47
48
49
50 import sys
51 import os
52 import logging
53 import datetime
54
55
56 from CedarBackup3.util import deriveDayOfWeek
57 from CedarBackup3.actions.util import checkMediaState
58 from CedarBackup3.actions.constants import DIR_TIME_FORMAT, STAGE_INDICATOR
59 from CedarBackup3.actions.store import writeImage, writeStoreIndicator, consistencyCheck
60
61
62
63
64
65
66 logger = logging.getLogger("CedarBackup3.log.actions.rebuild")
67
68
69
70
71
72
73
74
75
76
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.warning("Warning: the rebuild action is not fully supported on Mac OS X.")
104 logger.warning("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)
109 stagingDirs = _findRebuildDirs(config)
110 writeImage(config, True, stagingDirs)
111 if config.store.checkData:
112 if sys.platform == "darwin":
113 logger.warning("Warning: consistency check cannot be run successfully on Mac OS X.")
114 logger.warning("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
124
125
126
127
128
129
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