def initialise(self): """ Initialise the repo. """ if vc.is_init(self.backup_path, [self.contents_path], self.index_path, self.identity_string): print(self.already_init_msg) return False return vc.init(self.backup_path, [self.contents_path], self.index_path, self.identity_string)
def backup(self): """ Backup into .backup/contents """ if not vc.is_init(self.backup_path, [self.contents_path], self.index_path, self.identity_string): print(self.not_exist_msg) return False files = os.listdir() files.remove(self.backup_path) return vc.cp(zip(files,files),".",self.contents_path)
def restore(self): """ Remove the current directory's contents and replace it with .backup/contents """ if not vc.is_init(self.backup_path, [self.contents_path], self.index_path, self.identity_string): print(self.not_exist_msg) return False del_files = os.listdir() del_files.remove(self.backup_path) vc.rm(del_files,".") files = os.listdir(self.contents_path) vc.cp(zip(files,files),self.contents_path,".") return True
def backup(self): """ Backup into .backup/contents/x where x is the number of repos plus one. """ if not vc.is_init(self.backup_path, [self.contents_path], self.index_path, self.identity_string): print(self.not_exist_msg) return False backup_no = str(len(self.get_backups())+1) os.mkdir(os.path.join(self.contents_path,backup_no)) files = os.listdir() files.remove(self.backup_path) vc.cp(zip(files,files),".",os.path.join(self.contents_path,backup_no)) with open(self.index_path,"a") as f: f.write("\n%s;%s"%(backup_no,str(datetime.now()))) return True
def backup(self): if not vc.is_init(self.backup_path, self.direds, self.index_path, self.identity_string): print(self.not_exist_msg) return False backup_no = str(len(self.get_backups())+1) # this is terrible, but for now I'm lazy # Now we go about hashing things and checking if we have them already. existing = set(os.listdir(self.contents_path)) current_files = [] directory_structure = [] for dirname,subdirs,fnames in os.walk("."): # Let's not recurse into .hfk if self.backup_path in subdirs: subdirs.remove(self.backup_path) # for every file in the current directory, grab its hash for f in fnames: current_files.append(os.path.join(dirname,f)) # we also need to store the directory structure for d in subdirs: directory_structure.append(os.path.join(dirname,d)) pairs = [(f,self.careful_hash(f)) for f in current_files] # Let's create the catalogue file first, a simple dump of pairs catalogue = open(os.path.join(self.revisions_path,backup_no),"w") catalogue.write('\n'.join([p[0]+' '+p[1] for p in pairs])) catalogue.close() # and we need to dump the dir structure structure = open(os.path.join(self.revisions_path,backup_no+"d"),"w") structure.write('\n'.join(directory_structure)) structure.close() # Copy accross all the files listed in pairs but not currently in files/ files = [] for fle,hsh in pairs: if not hsh in existing: files.append((fle,hsh)) print("--> "+fle+" is new or has changed, storing") if files == []: print("No changes, bailing.") return True vc.cp(files,".",self.contents_path) with open(self.index_path,"a") as f: f.write("\n%s;%s"%(backup_no,str(datetime.now()))) return True
def restore(self,revision=-1): if not vc.is_init(self.backup_path, [self.contents_path], self.index_path, self.identity_string): print(self.not_exist_msg) return False # If -1, find the last revision if revision==-1: revision==len(open(self.index_path,"r").read().split('\n')) # pull up the catalogue of files and directory structure files catalogue = os.path.join(self.revisions_path,str(revision)) structure = os.path.join(self.revisions_path,str(revision)+"d") if not (os.path.exists(catalogue) and os.path.exists(structure)): print(self.no_existing_backup) return False # STEVE: Do we want to remove all files in the current dir? del_files = os.listdir(".") del_files.remove(self.backup_path) vc.rm(del_files, ".") # rebuild structure from the file dirfile = open(structure,'r') dirs = dirfile.read().split('\n') dirfile.close() for d in dirs: os.mkdir(d) # generate pairs of files and hashes (actually file names in files/) catfile = open(catalogue,'r') files = [(p[1],p[0]) for p in [line.split(' ') for line in catfile.read().split('\n')]] catfile.close() # simply copy them to the current directory vc.cp(files,self.contents_path,".") return True
def restore(self,backup_no=-1): """ Remove the current directory's contents and replace it with backup number selected """ if not vc.is_init(self.backup_path, [self.contents_path], self.index_path, self.identity_string): print(self.not_exist_msg) return False if backup_no==-1: backup_no = len(self.get_backups()) backup_file_path = os.path.join(self.contents_path, str(backup_no)) if backup_no == 0 or not os.path.exists(backup_file_path): print(self.no_existing_backup) return False del_files = os.listdir(".") del_files.remove(self.backup_path) vc.rm(del_files, ".") files = os.listdir(backup_file_path) return vc.cp(zip(files,files),backup_file_path,".")
def initialise(self): if vc.is_init(self.backup_path, self.direds, self.index_path, self.identity_string): print(self.already_init_msg) return False return vc.init(self.backup_path, self.direds, self.index_path, self.identity_string)