def test_getBackupsForArgs_all(self): args = fn.parseWordArgs(["from", "all"]) backups = fn.getBackupsForArgs(args, fn.getBackups(self._backupHome)) self.assertEqual( len(backups), 3, "Incorrect number of backups returned: {0}".format(len(backups)))
def checkForDeleteOldest(commandArgs, bkHome): """ Check if the option to delete the oldest backup was provided, and delete the oldest backup. """ if commandArgs.deleteOldest: if len(fn.getBackups(bkHome)) > 1: fn.deleteBackup(bkHome, fn.getOldestBackup(bkHome)) raise CleanExitException()
def checkMaxImages(backupHome, maxImages): """ When the number of backup images are greater than the max provided in settings, mark the extra images to be deleted. """ backups = fn.getBackups(backupHome) if len(backups.keys()) > maxImages: toRemove = list(backups.keys())[:len(backups.keys()) - maxImages] for id in toRemove: fn.markBackupForDeletion(backupHome, backups[id])
def organize(backupHome): """ Reduce the backups to a frequency of daily for those that are older than one day, and to weekly for those that are older than 31 days. """ catagorized = _catagorize(fn.getBackups(backupHome).values()) for backup in _getExtraDailyBackups(catagorized["daily"]): fn.markBackupForDeletion(backupHome, backup) for backup in _getExtraWeeklyBackups(catagorized["weekly"]): fn.markBackupForDeletion(backupHome, backup)
def listFiles(args, settings): """ Main function for the list option. """ wordArgs = fn.parseWordArgs(args) home = fn.getBackupHome(settings.backupLocation, settings.hostName) backups = fn.getBackupsForArgs(wordArgs, fn.getBackups(home)) backupWd = fn.removeAltRoot(settings.root, os.getcwd()) for id_ in backups.keys(): items = fn.expandGlob(home, backups[id_], backupWd, wordArgs["glob"]) for item in items: _outputItemInfo(id_, fn.getInfo(item))
def remove(args, settings): """ Main function for the remove option. """ wordArgs = fn.parseWordArgs(args) _validateArgs(wordArgs) home = fn.getBackupHome(settings.backupLocation, settings.hostName) backups = fn.getBackupsForArgs(wordArgs, fn.getBackups(home)) backupWd = fn.removeAltRoot(settings.root, os.getcwd()) for id_ in backups.keys(): items = fn.expandGlob(home, backups[id_], backupWd, wordArgs["glob"]) for item in items: _delete(item)
def checkForKeepDays(backupHome, keepDays): """ When the number of days a backup exists is greater than keepDays, those older backups are deleted. Do not delete any backups if keepDays is zero. """ def isOld(backup): dt = fn.toDateTime(backup) expire = datetime.now() - timedelta(days=keepDays) return dt <= expire backups = fn.getBackups(backupHome).values() toRemove = filter(isOld, backups) for backup in toRemove: fn.markBackupForDeletion(backupHome, backup)
def info(args, settings): """ Main function for the info option. """ backupHome = fn.getBackupHome(settings.backupLocation, settings.hostName) backupCount = len(fn.getBackups(backupHome)) dtFormat = "%m/%d/%Y %H:%M:%S" lastBackupRan = fn.toDateTime( fn.getLatestBackup(backupHome)).strftime(dtFormat) driveUsagePercentage = fn.getDriveUsagePercentage(backupHome) fmt = "{0:<20} {1}" print(fmt.format("Backup location:", settings.backupLocation)) print(fmt.format("Host Name:", settings.hostName)) print(fmt.format("Number of Backups:", backupCount)) print(fmt.format("Last Backup Ran:", lastBackupRan)) print(fmt.format("Drive Capacity:", "{0}%".format(driveUsagePercentage)))
def test_getBackups(self): backups = fn.getBackups(self._backupHome) self.assertEqual( len(backups), 3, "Incorrect number of backups returned: {0}".format(len(backups)))
def test_getBackupsForArgs_oneId(self): args = fn.parseWordArgs(["from", "backup", "2"]) backups = fn.getBackupsForArgs(args, fn.getBackups(self._backupHome)) self.assertEqual(backups[2], "2012-05-18-160013")