def run(opts): print notice #formatter = logging.Formatter('%(asctime)s:%(name)s:%(levelname)s: %(message)s') backup_file=os.path.realpath(opts['backup-file']) backup_dir=os.path.dirname(backup_file) name="".join(os.path.basename(backup_file).split('.')[0:-1]) destination=os.path.realpath(opts['dest']) bparse = backup_parser(backup_file) recover_date = max(bparse.backups.keys()) archives = bparse.backups[recover_date] oldcwd = os.getcwd() os.chdir(destination) dlist = generate_datelist(bparse.backups) for i,d in enumerate(dlist): print str(i) + ") " + d print "" sys.stdout.write( "Select a date to restore from (0): ") cin = sys.stdin.readline().rstrip('\n') if cin == "": choice = 0 else: choice = int(cin) recover_date = dlist[choice] logging.debug(recover_date) sys.stdout.write("Restore from " + recover_date + " (y/N) ") cin = sys.stdin.readline().rstrip('\n') if cin != "y": return snarfile = backup_dir + "/" + name + '-' + recover_date + ".snar" base_back = None for k in bparse.backups.keys(): if k <= recover_date: if base_back is None or base_back < k: base_back = k logging.debug("base_back " + base_back) archives = [] for arc in bparse.backups[base_back]: logging.debug(arc) if strip_date(arc) <= recover_date: archives.append(arc) archives.sort() for arc in archives: tar_args = ['tar', '-xjvf', backup_dir +"/" + arc, '-g', snarfile] call(tar_args) os.chdir(oldcwd)
def run(opts): print notice backup_file=os.path.realpath(opts['backup-file']) logging.debug("Using %s", backup_file) backup_dir=os.path.dirname(backup_file) name="".join(os.path.basename(backup_file).split('.')[0:-1]) bparse = backup_parser(backup_file) backup_dates = bparse.backups.keys() backup_dates.sort() for i,b in enumerate(backup_dates): print str(i) + ') ' + b print "" sys.stdout.write("Select backup to delete (default 0): ") ui = sys.stdin.readline().rstrip() if ui == "": choice = 0 else: choice = int(ui) sys.stdout.write("Deleting backup with date: " + backup_dates[choice] + " (y/N) ") ui = sys.stdin.readline().lower().rstrip('\n') if ui != 'y': print "Canceling" print ui return else: print "Deleting..." archives = bparse.backups[backup_dates[choice]] snarfile = backup_dir + "/" + name + '-' + backup_dates[choice] + ".snar" oldcwd = os.getcwd() os.chdir(backup_dir) for arc in archives: try: os.remove(arc) logging.info("rm " + arc) except OSError: logging.warning("Archive missing, perhaps from an failed delete earlier") logging.info("rm " + snarfile) try: os.remove(snarfile) except OSError: logging.warning("Snar file already deleted") print "Writing backup file" bparse.remove_backup(backup_dates[choice]) bparse.write_backup() os.chdir(oldcwd)
def init_filenames(self): self.bparse = backup_parser('{0}/{1}.backup'.format(self.dest, self.name)) now= datetime.datetime.now() self.date = now.strftime("%Y-%m-%dT%H:%M:%S") self.tar_name = "{0}/{1}-{2}-{3}.tar".format(self.dest, self.name, self.backup_type, self.date) self.snar_name = "{0}/{1}-{2}.snar".format(self.dest, self.name, self.get_base_backup_date())