def backup(self, basedir, include=[], exclude=[]): '''backup yum configuration and repositories''' # first backup the yum config SFile("/etc/yum.conf").copy_to(basedir) # then backup the individual repo files for yum_repo in FileManager.get_all_files(include=['/etc/yum.repos.d']): SFile(yum_repo).copy_to(basedir)
def restore(self, basedir): '''restore yum configuration and repositories''' # return if we cannot find require files if not os.path.isdir(basedir + "/etc/yum.repos.d"): return # first restore yum configuration SFile("etc/yum.conf").copy_to(self.fs_root, path_prefix=basedir) # then restore individual repos for yum_repo in FileManager.get_all_files(include=[basedir + "/etc/yum.repos.d"]): partial_path = yum_repo.replace(basedir + "/" , "") SFile(partial_path).copy_to(self.fs_root, path_prefix=basedir)
def testWriteFilesRecordFile(self): path1 = os.path.join("some", "path") path2 = os.path.join("another", "path") self.dest = os.path.join(os.path.dirname(__file__), "data", "files-out.xml") files = [SFile(path=path1), SFile(path=path2)] files_record_file = FilesRecordFile(self.dest) files_record_file.write(files) contents = FileManager.read_file(self.dest) self.assertEqual( "<files><file>" + path1 + "</file><file>" + path2 + "</file></files>", contents)
def backup(self, basedir, include=[], exclude=[]): """backup the files modified outside the apt package system""" if snap.config.options.log_level_at_least('verbose'): snap.callback.snapcallback.message("Backing up files using apt backend"); if len(include) == 0: include = ['/'] for additional_exclude in ['/proc', '/sys', '/selinux']: if not additional_exclude in exclude: exclude.append(additional_exclude) # remove duplicates include = list(set(include)) exclude = list(set(exclude)) # determine which files have been modified since installation # and copy those to basedir sfiles = [] files = FileManager.get_all_files(include, exclude) for tfile in files: if self.__file_modified(tfile): if snap.config.options.log_level_at_least('verbose'): snap.callback.snapcallback.message("Backing up file " + tfile); sfile = SFile(tfile) sfile.copy_to(basedir) sfiles.append(sfile) # write record file to basedir record = FilesRecordFile(basedir + "/files.xml") record.write(sfiles)
def restore(self, basedir): '''restore the packages from the snapfile''' # if package record file isn't found, simply return if not os.path.isfile(os.path.join(basedir, "packages.xml")): return if snap.config.options.log_level_at_least('verbose'): snap.callback.snapcallback.message("Restoring software on windows") # read files from the record file record = PackagesRecordFile(os.path.join(basedir, "packages.xml")) packages = record.read() # TODO restore registry? # restore program files for pkg_file in FileManager.get_all_files( include=[os.path.join(basedir, "windows-packages")]): partial_path = pkg_file.replace( os.path.join(basedir, "windows-packages") + "\\", "") try: SFile(partial_path).copy_to( path_prefix=os.path.join(basedir, "windows-packages")) except: if snap.config.options.log_level_at_least('normal'): snap.callback.snapcallback.message( "Failed to restore windows package file " + partial_path)
def backup(cls, basedir): # backup the confd files = snap.filemanager.FileManager.get_all_files( include=[d for d in Asterisk.DIRS.itervalues()]) sfiles = [SFile(tfile).copy_to(basedir) for tfile in files if os.access(tfile, os.R_OK)] # write record file to basedir record = FilesRecordFile(os.path.join(basedir, "service-asterisk.xml")) record.write(sfiles)
def restore(self, basedir): '''restore yum configuration and repositories''' # return if we cannot find require files if not os.path.isdir(basedir + "/etc/apt"): return # restore the apt config to /etc/apt for apt_conf in FileManager.get_all_files( include=[basedir + "/etc/apt"]): partial_path = apt_conf.replace(basedir + "/", "") SFile(partial_path).copy_to(self.fs_root, path_prefix=basedir)
def backup(self, basedir): # backup the configuration directory sfiles = [] files = snap.filemanager.FileManager.get_all_files( include=[Iis.CONFIG_ROOT]) for tfile in files: if os.access(tfile, os.R_OK): sfile = SFile(tfile) sfile.copy_to(basedir) sfiles.append(sfile) # write record file to basedir record = FilesRecordFile(os.path.join(basedir, "service-iis.xml")) record.write(sfiles)
def backup(self, basedir): # backup the webroot, confd sfiles = [] files = snap.filemanager.FileManager.get_all_files( include=[Httpd.DOCUMENT_ROOT, Httpd.CONF_D]) for tfile in files: if os.access(tfile, os.R_OK): sfile = SFile(tfile) sfile.copy_to(basedir) sfiles.append(sfile) # write record file to basedir record = FilesRecordFile(os.path.join(basedir, "service-http.xml")) record.write(sfiles)
def testSFileCopyToWithPrefix(self): basedir = os.path.join(os.path.dirname(__file__), "data") self.source_dir = os.path.join(basedir, "source", "subdir") self.dest_dir = os.path.join(basedir, "dest") os.makedirs(self.source_dir) f = open(os.path.join(self.source_dir, "foo"), 'w') f.write("foo") f.close() dest_file = os.path.join(self.dest_dir, "source", "subdir", "foo") sfile = SFile(path=os.path.join("source", "subdir", "foo")) sfile.copy_to(self.dest_dir, path_prefix=basedir) self.assertTrue(os.path.exists(dest_file)) shutil.rmtree(os.path.join(basedir, "source"))
def testSFileCopyLinkTo(self): basedir = os.path.join(os.path.dirname(__file__), "data") self.source = os.path.join(basedir, "sourcelink") self.dest_dir = os.path.join(basedir, "destdir") os.makedirs(self.dest_dir) if os.path.islink(self.source): os.remove(self.source) os.symlink("/foobar", self.source) sfile = SFile(self.source) sfile.copy_to(self.dest_dir) self.assertTrue(os.path.islink(self.dest_dir + self.source)) self.assertEqual("/foobar", os.path.realpath(self.dest_dir + self.source))
def backup(self, basedir, include=[], exclude=[]): '''backup the packages installed locally''' if snap.config.options.log_level_at_least('verbose'): snap.callback.snapcallback.message( "Backing up software on windows") packages = Win.get_packages() # backup program files for pkg in packages: for pkg_file in FileManager.get_all_files(include=[pkg.name]): SFile(pkg_file).copy_to( os.path.join(basedir, "windows-packages")) # TODO Backup registry? # write record file to basedir record = PackagesRecordFile(os.path.join(basedir, "packages.xml")) record.write(packages)
def testSFileCopyTo(self): basedir = os.path.join(os.path.dirname(__file__), "data") self.source_dir = os.path.join(basedir, "source", "subdir") self.dest_dir = os.path.join(basedir, "dest") os.makedirs(self.source_dir) f = open(os.path.join(self.source_dir, "foo"), 'w') f.write("foo") f.close() dest_file = os.path.join(self.dest_dir, self.source_dir, "foo") sfile = SFile(path=os.path.join(self.source_dir, "foo")) sfile.copy_to(self.dest_dir) self.assertTrue(os.path.exists(dest_file)) contents = FileManager.read_file(dest_file) self.assertEqual("foo", contents) shutil.rmtree(os.path.join(basedir, "source"))
if not additional_exclude in exclude: exclude.append(additional_exclude) # remove duplicates include = list(set(include)) exclude = list(set(exclude)) # determine which files have been modified since installation # and copy those to basedir sfiles = [] files = FileManager.get_all_files(include, exclude) for tfile in files: if snap.config.options.log_level_at_least('verbose'): snap.callback.snapcallback.message("Backing up file " + tfile) try: sfile = SFile(tfile) sfile.copy_to(basedir) sfiles.append(sfile) except: pass # write record file to basedir record = FilesRecordFile(os.path.join(basedir, "files.xml")) record.write(sfiles) def restore(self, basedir): """restore the files in the snapfile""" # if files record file isn't found, simply return if not os.path.isfile(os.path.join(basedir, "files.xml")): return
def backup(self, basedir, include=[], exclude=[]): '''backup apt configuration and repositories''' # backup the apt config in /etc/apt for apt_conf in FileManager.get_all_files(include=['/etc/apt']): SFile(apt_conf).copy_to(basedir)