示例#1
0
    def run(self, backup_name=None):
        if backup_name is None:
            backup_name = time.strftime('%Y-%m-%d_%H-%M-%S')

        start_time = time.time()
        result = self.process_node(self.input_dir)
        end_time = time.time()

        bck_table_file = os.path.join(
            self.index_dir, '{}.bktable'.format(backup_name))
        bck_info_file = os.path.join(
            self.index_dir, '{}.bkinfo'.format(backup_name))

        # todo: the backup info/table file should be written periodically
        # in order to prevent data loss in case something goes wrong..

        backup_speed = self._total_size / (end_time - start_time)

        with open(bck_table_file, 'w') as f:
            f.write(json.dumps(result))

        with open(bck_info_file, 'w') as f:
            f.write(json.dumps({
                'root_dir': self.input_dir,
                'start_time': start_time,
                'end_time': end_time,
                'files_count': self._processed_files_count,
                'exclude_count': self._ignored_files_count,
                'failed_files': self._failed_files,
                'total_size': self._total_size,
                'backup_speed': backup_speed,
            }))

        logger.debug('FILE bck-table {}'.format(bck_table_file))
        logger.debug('FILE bck-info {}'.format(bck_info_file))

        logger.debug('STAT start_time {}'.format(start_time))
        logger.debug('STAT end_time {}'.format(end_time))
        logger.debug('STAT bck_time {}'.format(
            natural_time_interval(end_time - start_time)))
        logger.debug('STAT files_count {}'.format(self._processed_files_count))
        logger.debug('STAT exclude_count {}'.format(self._ignored_files_count))
        logger.debug('STAT failed_files_count {}'.format(
            len(self._failed_files)))
        logger.debug('STAT total_size {}'.format(
            natural_size(self._total_size)))
        logger.debug('STAT backup_speed {}/s'.format(
            natural_size(backup_speed)))

        return result
示例#2
0
    def test_natural_size(self):
        from Branckenstein.utils.humanize import natural_size

        self.assertEqual(natural_size(0), '0 Bytes')
        self.assertEqual(natural_size(0, binary=True), '0 Bytes')
        self.assertEqual(natural_size(0, gnu=True), '0B')
        self.assertEqual(natural_size(0, binary=True, gnu=True), '0B')

        self.assertEqual(natural_size(1), '1 Byte')
        self.assertEqual(natural_size(1, binary=True), '1 Byte')
        self.assertEqual(natural_size(1, gnu=True), '1B')
        self.assertEqual(natural_size(1, binary=True, gnu=True), '1B')

        self.assertEqual(natural_size(10), '10 Bytes')
        self.assertEqual(natural_size(10, binary=True), '10 Bytes')
        self.assertEqual(natural_size(10, gnu=True), '10B')
        self.assertEqual(natural_size(10, binary=True, gnu=True), '10B')

        self.assertEqual(natural_size(1000), '1.0 kB')
        self.assertEqual(natural_size(1000, binary=True), '1000 Bytes')
        self.assertEqual(natural_size(1000, gnu=True), '1.0K')
        self.assertEqual(natural_size(1000, binary=True, gnu=True), '1000B')

        self.assertEqual(natural_size(1024), '1.0 kB')
        self.assertEqual(natural_size(1024, binary=True), '1.0 KiB')
        self.assertEqual(natural_size(1024, gnu=True), '1.0K')
        self.assertEqual(natural_size(1024, binary=True, gnu=True), '1.0K')

        self.assertEqual(natural_size(1000*2), '2.0 kB')
        self.assertEqual(natural_size(1000*2, binary=True), '2.0 KiB')
        self.assertEqual(natural_size(1000*2, gnu=True), '2.0K')
        self.assertEqual(natural_size(1000*2, binary=True, gnu=True), '2.0K')

        self.assertEqual(natural_size(1024*2), '2.0 kB')
        self.assertEqual(natural_size(1024*2, binary=True), '2.0 KiB')
        self.assertEqual(natural_size(1024*2, gnu=True), '2.0K')
        self.assertEqual(natural_size(1024*2, binary=True, gnu=True), '2.0K')

        self.assertEqual(natural_size(1000**2), '1.0 MB')
        self.assertEqual(natural_size(1000**2, binary=True), '976.6 KiB')
        self.assertEqual(natural_size(1000**2, gnu=True), '1.0M')
        self.assertEqual(natural_size(1000**2, binary=True, gnu=True), '976.6K')

        self.assertEqual(natural_size(1024**2), '1.0 MB')
        self.assertEqual(natural_size(1024**2, binary=True), '1.0 MiB')
        self.assertEqual(natural_size(1024**2, gnu=True), '1.0M')
        self.assertEqual(natural_size(1024**2, binary=True, gnu=True), '1.0M')
示例#3
0
def main():
    parser = get_parser()
    options, args = parser.parse_args()

    if options.backup_dir is None:
        parser.print_help()
        return

    if options.help_commands:
        print "Commands:\n" \
              "    list-backups\n" \
              "        Lists all the backups in the given backup directory\n" \
              "    list-files <backup-name>\n" \
              "        List files for the selected backup\n" \
              "    show-info <backup-name>\n" \
              "        Show information about the selected backup\n" \
              "    restore-files <backup-name> <destination> [<base>]\n" \
              "        Restore the selected files (or all) to the\n" \
              "        selected destination.\n"
        return

    try:
        command = args[0]
    except IndexError:
        sys.stderr.write("You must specify a command!\n"
                         "See --help-commands for more info.\n\n")
        return

    restore_tool = FilesystemRestoreTool(
        backup_dir=options.backup_dir,
    )

    if command == 'list-backups':
        for bckname, bckinfo in sorted(restore_tool.list_backups().items()):
            print(
                bckname,
                bckinfo['root_dir'],
                datetime.datetime.fromtimestamp(bckinfo['start_time']).strftime('%F %T'),
                natural_size(bckinfo['total_size']),
                '{} files'.format(bckinfo['files_count'])
            )

    elif command == 'list-files':
        for filename in sorted(restore_tool.list_files(args[1])):
        #for filename in restore_tool.list_files(args[1]):
            print filename

    elif command == 'show-info':
        import pprint
        pprint.pprint(restore_tool.get_info(args[1]))

    elif command == 'restore-files':
        backup_name = args[1]
        destination = args[2]
        try:
            base = args[3]
        except IndexError:
            base = ''
        print "Should restore files from {backup_name} to {destination}," \
              "starting from {base}".format(
                  backup_name=backup_name,
                  destination=destination,
                  base=base)
        print "Does nothing for now.."

    else:
        sys.stderr.write(
            "Unsupported command {}. See --help for more info."
            "".format(command))