/usr/share/pyshared/CedarBackup2/tools/span.py is in cedar-backup2 2.27.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 | # -*- coding: iso-8859-1 -*-
# vim: set ft=python ts=3 sw=3 expandtab:
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
# C E D A R
# S O L U T I O N S "Software done right."
# S O F T W A R E
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
# Copyright (c) 2007-2008,2010 Kenneth J. Pronovici.
# All rights reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License,
# Version 2, as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# Copies of the GNU General Public License are available from
# the Free Software Foundation website, http://www.gnu.org/.
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
# Author : Kenneth J. Pronovici <pronovic@ieee.org>
# Language : Python 2 (>= 2.7)
# Project : Cedar Backup, release 2
# Purpose : Spans staged data among multiple discs
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
########################################################################
# Notes
########################################################################
"""
Spans staged data among multiple discs
This is the Cedar Backup span tool. It is intended for use by people who stage
more data than can fit on a single disc. It allows a user to split staged data
among more than one disc. It can't be an extension because it requires user
input when switching media.
Most configuration is taken from the Cedar Backup configuration file,
specifically the store section. A few pieces of configuration are taken
directly from the user.
@author: Kenneth J. Pronovici <pronovic@ieee.org>
"""
########################################################################
# Imported modules and constants
########################################################################
# System modules
import sys
import os
import logging
import tempfile
# Cedar Backup modules
from CedarBackup2.release import AUTHOR, EMAIL, VERSION, DATE, COPYRIGHT
from CedarBackup2.util import displayBytes, convertSize, mount, unmount
from CedarBackup2.util import UNIT_SECTORS, UNIT_BYTES
from CedarBackup2.config import Config
from CedarBackup2.filesystem import BackupFileList, compareDigestMaps, normalizeDir
from CedarBackup2.cli import Options, setupLogging, setupPathResolver
from CedarBackup2.cli import DEFAULT_CONFIG, DEFAULT_LOGFILE, DEFAULT_OWNERSHIP, DEFAULT_MODE
from CedarBackup2.actions.constants import STORE_INDICATOR
from CedarBackup2.actions.util import createWriter
from CedarBackup2.actions.store import writeIndicatorFile
from CedarBackup2.actions.util import findDailyDirs
from CedarBackup2.util import Diagnostics
########################################################################
# Module-wide constants and variables
########################################################################
logger = logging.getLogger("CedarBackup2.log.tools.span")
#######################################################################
# SpanOptions class
#######################################################################
class SpanOptions(Options):
"""
Tool-specific command-line options.
Most of the cback command-line options are exactly what we need here --
logfile path, permissions, verbosity, etc. However, we need to make a few
tweaks since we don't accept any actions.
Also, a few extra command line options that we accept are really ignored
underneath. I just don't care about that for a tool like this.
"""
def validate(self):
"""
Validates command-line options represented by the object.
There are no validations here, because we don't use any actions.
@raise ValueError: If one of the validations fails.
"""
pass
#######################################################################
# Public functions
#######################################################################
#################
# cli() function
#################
def cli():
"""
Implements the command-line interface for the C{cback-span} script.
Essentially, this is the "main routine" for the cback-span script. It does
all of the argument processing for the script, and then also implements the
tool functionality.
This function looks pretty similiar to C{CedarBackup2.cli.cli()}. It's not
easy to refactor this code to make it reusable and also readable, so I've
decided to just live with the duplication.
A different error code is returned for each type of failure:
- C{1}: The Python interpreter version is < 2.7
- C{2}: Error processing command-line arguments
- C{3}: Error configuring logging
- C{4}: Error parsing indicated configuration file
- C{5}: Backup was interrupted with a CTRL-C or similar
- C{6}: Error executing other parts of the script
@note: This script uses print rather than logging to the INFO level, because
it is interactive. Underlying Cedar Backup functionality uses the logging
mechanism exclusively.
@return: Error code as described above.
"""
try:
if map(int, [sys.version_info[0], sys.version_info[1]]) < [2, 7]:
sys.stderr.write("Python 2 version 2.7 or greater required.\n")
return 1
except:
# sys.version_info isn't available before 2.0
sys.stderr.write("Python 2 version 2.7 or greater required.\n")
return 1
try:
options = SpanOptions(argumentList=sys.argv[1:])
except Exception, e:
_usage()
sys.stderr.write(" *** Error: %s\n" % e)
return 2
if options.help:
_usage()
return 0
if options.version:
_version()
return 0
if options.diagnostics:
_diagnostics()
return 0
if options.stacktrace:
logfile = setupLogging(options)
else:
try:
logfile = setupLogging(options)
except Exception as e:
sys.stderr.write("Error setting up logging: %s\n" % e)
return 3
logger.info("Cedar Backup 'span' utility run started.")
logger.info("Options were [%s]", options)
logger.info("Logfile is [%s]", logfile)
if options.config is None:
logger.debug("Using default configuration file.")
configPath = DEFAULT_CONFIG
else:
logger.debug("Using user-supplied configuration file.")
configPath = options.config
try:
logger.info("Configuration path is [%s]", configPath)
config = Config(xmlPath=configPath)
setupPathResolver(config)
except Exception, e:
logger.error("Error reading or handling configuration: %s", e)
logger.info("Cedar Backup 'span' utility run completed with status 4.")
return 4
if options.stacktrace:
_executeAction(options, config)
else:
try:
_executeAction(options, config)
except KeyboardInterrupt:
logger.error("Backup interrupted.")
logger.info("Cedar Backup 'span' utility run completed with status 5.")
return 5
except Exception, e:
logger.error("Error executing backup: %s", e)
logger.info("Cedar Backup 'span' utility run completed with status 6.")
return 6
logger.info("Cedar Backup 'span' utility run completed with status 0.")
return 0
#######################################################################
# Utility functions
#######################################################################
####################
# _usage() function
####################
def _usage(fd=sys.stderr):
"""
Prints usage information for the cback script.
@param fd: File descriptor used to print information.
@note: The C{fd} is used rather than C{print} to facilitate unit testing.
"""
fd.write("\n")
fd.write(" Usage: cback-span [switches]\n")
fd.write("\n")
fd.write(" Cedar Backup 'span' tool.\n")
fd.write("\n")
fd.write(" This Cedar Backup utility spans staged data between multiple discs.\n")
fd.write(" It is a utility, not an extension, and requires user interaction.\n")
fd.write("\n")
fd.write(" The following switches are accepted, mostly to set up underlying\n")
fd.write(" Cedar Backup functionality:\n")
fd.write("\n")
fd.write(" -h, --help Display this usage/help listing\n")
fd.write(" -V, --version Display version information\n")
fd.write(" -b, --verbose Print verbose output as well as logging to disk\n")
fd.write(" -c, --config Path to config file (default: %s)\n" % DEFAULT_CONFIG)
fd.write(" -l, --logfile Path to logfile (default: %s)\n" % DEFAULT_LOGFILE)
fd.write(" -o, --owner Logfile ownership, user:group (default: %s:%s)\n" % (DEFAULT_OWNERSHIP[0], DEFAULT_OWNERSHIP[1]))
fd.write(" -m, --mode Octal logfile permissions mode (default: %o)\n" % DEFAULT_MODE)
fd.write(" -O, --output Record some sub-command (i.e. tar) output to the log\n")
fd.write(" -d, --debug Write debugging information to the log (implies --output)\n")
fd.write(" -s, --stack Dump a Python stack trace instead of swallowing exceptions\n")
fd.write("\n")
######################
# _version() function
######################
def _version(fd=sys.stdout):
"""
Prints version information for the cback script.
@param fd: File descriptor used to print information.
@note: The C{fd} is used rather than C{print} to facilitate unit testing.
"""
fd.write("\n")
fd.write(" Cedar Backup 'span' tool.\n")
fd.write(" Included with Cedar Backup version %s, released %s.\n" % (VERSION, DATE))
fd.write("\n")
fd.write(" Copyright (c) %s %s <%s>.\n" % (COPYRIGHT, AUTHOR, EMAIL))
fd.write(" See CREDITS for a list of included code and other contributors.\n")
fd.write(" This is free software; there is NO warranty. See the\n")
fd.write(" GNU General Public License version 2 for copying conditions.\n")
fd.write("\n")
fd.write(" Use the --help option for usage information.\n")
fd.write("\n")
##########################
# _diagnostics() function
##########################
def _diagnostics(fd=sys.stdout):
"""
Prints runtime diagnostics information.
@param fd: File descriptor used to print information.
@note: The C{fd} is used rather than C{print} to facilitate unit testing.
"""
fd.write("\n")
fd.write("Diagnostics:\n")
fd.write("\n")
Diagnostics().printDiagnostics(fd=fd, prefix=" ")
fd.write("\n")
############################
# _executeAction() function
############################
# pylint: disable=W0613
def _executeAction(options, config):
"""
Implements the guts of the cback-span tool.
@param options: Program command-line options.
@type options: SpanOptions object.
@param config: Program configuration.
@type config: Config object.
@raise Exception: Under many generic error conditions
"""
print ""
print "================================================"
print " Cedar Backup 'span' tool"
print "================================================"
print ""
print "This the Cedar Backup span tool. It is used to split up staging"
print "data when that staging data does not fit onto a single disc."
print ""
print "This utility operates using Cedar Backup configuration. Configuration"
print "specifies which staging directory to look at and which writer device"
print "and media type to use."
print ""
if not _getYesNoAnswer("Continue?", default="Y"):
return
print "==="
print ""
print "Cedar Backup store configuration looks like this:"
print ""
print " Source Directory...: %s" % config.store.sourceDir
print " Media Type.........: %s" % config.store.mediaType
print " Device Type........: %s" % config.store.deviceType
print " Device Path........: %s" % config.store.devicePath
print " Device SCSI ID.....: %s" % config.store.deviceScsiId
print " Drive Speed........: %s" % config.store.driveSpeed
print " Check Data Flag....: %s" % config.store.checkData
print " No Eject Flag......: %s" % config.store.noEject
print ""
if not _getYesNoAnswer("Is this OK?", default="Y"):
return
print "==="
(writer, mediaCapacity) = _getWriter(config)
print ""
print "Please wait, indexing the source directory (this may take a while)..."
(dailyDirs, fileList) = _findDailyDirs(config.store.sourceDir)
print "==="
print ""
print "The following daily staging directories have not yet been written to disc:"
print ""
for dailyDir in dailyDirs:
print " %s" % dailyDir
totalSize = fileList.totalSize()
print ""
print "The total size of the data in these directories is %s." % displayBytes(totalSize)
print ""
if not _getYesNoAnswer("Continue?", default="Y"):
return
print "==="
print ""
print "Based on configuration, the capacity of your media is %s." % displayBytes(mediaCapacity)
print ""
print "Since estimates are not perfect and there is some uncertainly in"
print "media capacity calculations, it is good to have a \"cushion\","
print "a percentage of capacity to set aside. The cushion reduces the"
print "capacity of your media, so a 1.5% cushion leaves 98.5% remaining."
print ""
cushion = _getFloat("What cushion percentage?", default=4.5)
print "==="
realCapacity = ((100.0 - cushion)/100.0) * mediaCapacity
minimumDiscs = (totalSize/realCapacity) + 1
print ""
print "The real capacity, taking into account the %.2f%% cushion, is %s." % (cushion, displayBytes(realCapacity))
print "It will take at least %d disc(s) to store your %s of data." % (minimumDiscs, displayBytes(totalSize))
print ""
if not _getYesNoAnswer("Continue?", default="Y"):
return
print "==="
happy = False
while not happy:
print ""
print "Which algorithm do you want to use to span your data across"
print "multiple discs?"
print ""
print "The following algorithms are available:"
print ""
print " first....: The \"first-fit\" algorithm"
print " best.....: The \"best-fit\" algorithm"
print " worst....: The \"worst-fit\" algorithm"
print " alternate: The \"alternate-fit\" algorithm"
print ""
print "If you don't like the results you will have a chance to try a"
print "different one later."
print ""
algorithm = _getChoiceAnswer("Which algorithm?", "worst", [ "first", "best", "worst", "alternate", ])
print "==="
print ""
print "Please wait, generating file lists (this may take a while)..."
spanSet = fileList.generateSpan(capacity=realCapacity, algorithm="%s_fit" % algorithm)
print "==="
print ""
print "Using the \"%s-fit\" algorithm, Cedar Backup can split your data" % algorithm
print "into %d discs." % len(spanSet)
print ""
counter = 0
for item in spanSet:
counter += 1
print "Disc %d: %d files, %s, %.2f%% utilization" % (counter, len(item.fileList),
displayBytes(item.size), item.utilization)
print ""
if _getYesNoAnswer("Accept this solution?", default="Y"):
happy = True
print "==="
counter = 0
for spanItem in spanSet:
counter += 1
if counter == 1:
print ""
_getReturn("Please place the first disc in your backup device.\nPress return when ready.")
print "==="
else:
print ""
_getReturn("Please replace the disc in your backup device.\nPress return when ready.")
print "==="
_writeDisc(config, writer, spanItem)
_writeStoreIndicator(config, dailyDirs)
print ""
print "Completed writing all discs."
############################
# _findDailyDirs() function
############################
def _findDailyDirs(stagingDir):
"""
Returns a list of all daily staging directories that have not yet been
stored.
The store indicator file C{cback.store} will be written to a daily staging
directory once that directory is written to disc. So, this function looks
at each daily staging directory within the configured staging directory, and
returns a list of those which do not contain the indicator file.
Returned is a tuple containing two items: a list of daily staging
directories, and a BackupFileList containing all files among those staging
directories.
@param stagingDir: Configured staging directory
@return: Tuple (staging dirs, backup file list)
"""
results = findDailyDirs(stagingDir, STORE_INDICATOR)
fileList = BackupFileList()
for item in results:
fileList.addDirContents(item)
return (results, fileList)
##################################
# _writeStoreIndicator() function
##################################
def _writeStoreIndicator(config, dailyDirs):
"""
Writes a store indicator file into daily directories.
@param config: Config object.
@param dailyDirs: List of daily directories
"""
for dailyDir in dailyDirs:
writeIndicatorFile(dailyDir, STORE_INDICATOR,
config.options.backupUser,
config.options.backupGroup)
########################
# _getWriter() function
########################
def _getWriter(config):
"""
Gets a writer and media capacity from store configuration.
Returned is a writer and a media capacity in bytes.
@param config: Cedar Backup configuration
@return: Tuple of (writer, mediaCapacity)
"""
writer = createWriter(config)
mediaCapacity = convertSize(writer.media.capacity, UNIT_SECTORS, UNIT_BYTES)
return (writer, mediaCapacity)
########################
# _writeDisc() function
########################
def _writeDisc(config, writer, spanItem):
"""
Writes a span item to disc.
@param config: Cedar Backup configuration
@param writer: Writer to use
@param spanItem: Span item to write
"""
print ""
_discInitializeImage(config, writer, spanItem)
_discWriteImage(config, writer)
_discConsistencyCheck(config, writer, spanItem)
print "Write process is complete."
print "==="
def _discInitializeImage(config, writer, spanItem):
"""
Initialize an ISO image for a span item.
@param config: Cedar Backup configuration
@param writer: Writer to use
@param spanItem: Span item to write
"""
complete = False
while not complete:
try:
print "Initializing image..."
writer.initializeImage(newDisc=True, tmpdir=config.options.workingDir)
for path in spanItem.fileList:
graftPoint = os.path.dirname(path.replace(config.store.sourceDir, "", 1))
writer.addImageEntry(path, graftPoint)
complete = True
except KeyboardInterrupt, e:
raise e
except Exception, e:
logger.error("Failed to initialize image: %s", e)
if not _getYesNoAnswer("Retry initialization step?", default="Y"):
raise e
print "Ok, attempting retry."
print "==="
print "Completed initializing image."
# pylint: disable=W0613
def _discWriteImage(config, writer):
"""
Writes a ISO image for a span item.
@param config: Cedar Backup configuration
@param writer: Writer to use
"""
complete = False
while not complete:
try:
print "Writing image to disc..."
writer.writeImage()
complete = True
except KeyboardInterrupt, e:
raise e
except Exception, e:
logger.error("Failed to write image: %s", e)
if not _getYesNoAnswer("Retry this step?", default="Y"):
raise e
print "Ok, attempting retry."
_getReturn("Please replace media if needed.\nPress return when ready.")
print "==="
print "Completed writing image."
def _discConsistencyCheck(config, writer, spanItem):
"""
Run a consistency check on an ISO image for a span item.
@param config: Cedar Backup configuration
@param writer: Writer to use
@param spanItem: Span item to write
"""
if config.store.checkData:
complete = False
while not complete:
try:
print "Running consistency check..."
_consistencyCheck(config, spanItem.fileList)
complete = True
except KeyboardInterrupt, e:
raise e
except Exception, e:
logger.error("Consistency check failed: %s", e)
if not _getYesNoAnswer("Retry the consistency check?", default="Y"):
raise e
if _getYesNoAnswer("Rewrite the disc first?", default="N"):
print "Ok, attempting retry."
_getReturn("Please replace the disc in your backup device.\nPress return when ready.")
print "==="
_discWriteImage(config, writer)
else:
print "Ok, attempting retry."
print "==="
print "Completed consistency check."
###############################
# _consistencyCheck() function
###############################
def _consistencyCheck(config, fileList):
"""
Runs a consistency check against media in the backup device.
The function mounts the device at a temporary mount point in the working
directory, and then compares the passed-in file list's digest map with the
one generated from the disc. The two lists should be identical.
If no exceptions are thrown, there were no problems with the consistency
check.
@warning: The implementation of this function is very UNIX-specific.
@param config: Config object.
@param fileList: BackupFileList whose contents to check against
@raise ValueError: If the check fails
@raise IOError: If there is a problem working with the media.
"""
logger.debug("Running consistency check.")
mountPoint = tempfile.mkdtemp(dir=config.options.workingDir)
try:
mount(config.store.devicePath, mountPoint, "iso9660")
discList = BackupFileList()
discList.addDirContents(mountPoint)
sourceList = BackupFileList()
sourceList.extend(fileList)
discListDigest = discList.generateDigestMap(stripPrefix=normalizeDir(mountPoint))
sourceListDigest = sourceList.generateDigestMap(stripPrefix=normalizeDir(config.store.sourceDir))
compareDigestMaps(sourceListDigest, discListDigest, verbose=True)
logger.info("Consistency check completed. No problems found.")
finally:
unmount(mountPoint, True, 5, 1) # try 5 times, and remove mount point when done
#########################################################################
# User interface utilities
########################################################################
def _getYesNoAnswer(prompt, default):
"""
Get a yes/no answer from the user.
The default will be placed at the end of the prompt.
A "Y" or "y" is considered yes, anything else no.
A blank (empty) response results in the default.
@param prompt: Prompt to show.
@param default: Default to set if the result is blank
@return: Boolean true/false corresponding to Y/N
"""
if default == "Y":
prompt = "%s [Y/n]: " % prompt
else:
prompt = "%s [y/N]: " % prompt
answer = raw_input(prompt)
if answer in [ None, "", ]:
answer = default
if answer[0] in [ "Y", "y", ]:
return True
else:
return False
def _getChoiceAnswer(prompt, default, validChoices):
"""
Get a particular choice from the user.
The default will be placed at the end of the prompt.
The function loops until getting a valid choice.
A blank (empty) response results in the default.
@param prompt: Prompt to show.
@param default: Default to set if the result is None or blank.
@param validChoices: List of valid choices (strings)
@return: Valid choice from user.
"""
prompt = "%s [%s]: " % (prompt, default)
answer = raw_input(prompt)
if answer in [ None, "", ]:
answer = default
while answer not in validChoices:
print "Choice must be one of %s" % validChoices
answer = raw_input(prompt)
return answer
def _getFloat(prompt, default):
"""
Get a floating point number from the user.
The default will be placed at the end of the prompt.
The function loops until getting a valid floating point number.
A blank (empty) response results in the default.
@param prompt: Prompt to show.
@param default: Default to set if the result is None or blank.
@return: Floating point number from user
"""
prompt = "%s [%.2f]: " % (prompt, default)
while True:
answer = raw_input(prompt)
if answer in [ None, "" ]:
return default
else:
try:
return float(answer)
except ValueError:
print "Enter a floating point number."
def _getReturn(prompt):
"""
Get a return key from the user.
@param prompt: Prompt to show.
"""
raw_input(prompt)
#########################################################################
# Main routine
########################################################################
if __name__ == "__main__":
sys.exit(cli())
|