def prepare(tasks): if not _quiet: kits.stdoutCR('Preparing...') rm_oldbaks = [] for task in tasks: dst_dir = os.path.dirname(task['bak']) rm_oldbaks.append('[[ ! -d {} ]] && mkdir {}; rm -rf {}/*'.format(dst_dir, dst_dir, task['bak'])) rm_oldbaks_cmd = ['ssh'] if config['ssh_key'] is not None: rm_oldbaks_cmd.extend(['-i', '"{}"'.format(config['ssh_key'])]) rm_oldbaks_cmd.append('{}@{}'.format(config['backup_user'], config['backup_server'])) rm_oldbaks_cmd.append('"{}"'.format('; '.join(rm_oldbaks))) rm_oldbaks_cmd = ' '.join(rm_oldbaks_cmd) try: subprocess.check_call(rm_oldbaks_cmd, shell=True) except KeyboardInterrupt, e: kits.exit('\n\nCanceled.')
rm_oldbaks.append('[[ ! -d {} ]] && mkdir {}; rm -rf {}/*'.format(dst_dir, dst_dir, task['bak'])) rm_oldbaks_cmd = ['ssh'] if config['ssh_key'] is not None: rm_oldbaks_cmd.extend(['-i', '"{}"'.format(config['ssh_key'])]) rm_oldbaks_cmd.append('{}@{}'.format(config['backup_user'], config['backup_server'])) rm_oldbaks_cmd.append('"{}"'.format('; '.join(rm_oldbaks))) rm_oldbaks_cmd = ' '.join(rm_oldbaks_cmd) try: subprocess.check_call(rm_oldbaks_cmd, shell=True) except KeyboardInterrupt, e: kits.exit('\n\nCanceled.') except Exception, e: logger.error(e) else: if not _quiet: kits.stdoutCR('Prepare, done.\n\n') def backup(task): if not _quiet: kits.stdout('Backup {}'.format(task['name'])) try: task['filter'].extend(config['global_filter']) rsync = kits.rsync.RSync(task['src'], task['dst'], sshkey=config['ssh_key'], backup_dir=task['bak'], filter_rule=task['filter'], quiet=_quiet) # print(rsync.toCmd('--stats', '--dry-run')) # return total, size, elapsed = rsync.run(exact_progress=_exact_progress) if size != 0: logger.info('%s backup finished. Num: %d, Size: %s Elapsed: %s, Speed: %s, Source: %s', task['name'], total, kits.util.hrData(size), kits.util.hrTime(elapsed), kits.util.hrData(float(size) / elapsed), task['src'])