def testBackupCertainFiles(self): os.mkdir(os.path.join(self.fs_root, "subdir1")) os.mkdir(os.path.join(self.fs_root, "subdir1", "subsubdir")) os.mkdir(os.path.join(self.fs_root, "subdir2")) f = open(os.path.join(self.fs_root, "subdir1", "foo"), 'w') f.write("foo") f.close() f = open(os.path.join(self.fs_root, "subdir2", "bar"), 'w') f.write("bar") f.close() f = open(os.path.join(self.fs_root, "subdir1", "subsubdir", "money"), 'w') f.write("money") f.close() backup_target = snap.backends.files.win.Win() backup_target.backup( self.basedir, include=[os.path.join(self.fs_root, "subdir1")], exclude=[os.path.join(self.fs_root, "subdir1", "subsubdir")]) self.assertTrue( os.path.exists( os.path.join( self.basedir, SFile.windows_path_escape( os.path.join(self.fs_root, "subdir1", "foo"))))) self.assertFalse( os.path.exists( os.path.join( self.basedir, SFile.windows_path_escape( os.path.join(self.fs_root, "subdir2", "foo"))))) self.assertFalse( os.path.exists( os.path.join( self.basedir, SFile.windows_path_escape( os.path.join(self.fs_root, "subdir1", "subsubdir", "money"))))) record = FilesRecordFile(os.path.join(self.basedir, "files.xml")) files = record.read() file_names = [] for sfile in files: file_names.append(sfile.path) self.assertEqual(1, len(files)) self.assertIn( SFile.windows_path_escape( os.path.join(self.fs_root, "subdir1", "foo")), file_names) self.assertNotIn( SFile.windows_path_escape( os.path.join(self.fs_root, "subdir2", "bar")), file_names) self.assertNotIn( SFile.windows_path_escape( os.path.join(self.fs_root, "subdir1", "subsubdir", "bar")), file_names)
def restore(self, basedir): dispatcher = Dispatcher.os_dispatcher() record_file = os.path.join(basedir, "service-http.xml") # if files record file isn't found, simply return if not os.path.isfile(record_file): return # stop the httpd service if already running if dispatcher.service_running(Httpd.DAEMON): dispatcher.stop_service(Httpd.DAEMON) # read files from the record file record = FilesRecordFile(record_file) sfiles = record.read() # restore those to their original locations for sfile in sfiles: sfile.copy_to(path_prefix=basedir) # ensure the various subdirs exists even if empty if OS.is_linux() and not os.path.isdir( os.path.join(Httpd.DOCUMENT_ROOT, "html")): os.mkdir(os.path.join(Httpd.DOCUMENT_ROOT, "html")) if OS.is_linux() and not os.path.isdir( os.path.join(Httpd.CONF_D, "logs")): os.mkdir(os.path.join(Httpd.CONF_D, "logs")) if OS.is_linux() and not os.path.isdir( os.path.join(Httpd.CONF_D, "run")): os.mkdir(os.path.join(Httpd.CONF_D, "run")) # start the httpd service dispatcher.start_service(Httpd.DAEMON)
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 testBackupCertainFiles(self): os.mkdir(self.fs_root + "/subdir1") os.mkdir(self.fs_root + "/subdir1/subsubdir") os.mkdir(self.fs_root + "/subdir2") f=open(self.fs_root + "/subdir1/foo" , 'w') f.write("foo") f.close() f=open(self.fs_root + "/subdir2/bar" , 'w') f.write("bar") f.close() f=open(self.fs_root + "/subdir1/subsubdir/money" , 'w') f.write("money") f.close() backup_target = snap.backends.files.syum.Syum() backup_target.backup(self.basedir, include=[self.fs_root + "/subdir1"], exclude=[self.fs_root + "/subdir1/subsubdir"]) self.assertTrue(os.path.exists(self.basedir + self.fs_root + "/subdir1/foo")) self.assertFalse(os.path.exists(self.basedir + self.fs_root + "/subdir2/foo")) self.assertFalse(os.path.exists(self.basedir + self.fs_root + "/subdir1/subsubdir/money")) record = FilesRecordFile(self.basedir + "/files.xml") files = record.read() file_names = [] for sfile in files: file_names.append(sfile.path) self.assertEqual(1, len(files)) self.assertIn(self.fs_root[1:] + "/subdir1/foo", file_names) self.assertNotIn(self.fs_root[1:] + "/subdir2/bar", file_names) self.assertNotIn(self.fs_root[1:] + "/subdir1/subsubdir/bar", file_names)
def restore(self, basedir): dispatcher = Dispatcher.os_dispatcher() record_file = os.path.join(basedir, "service-http.xml") # if files record file isn't found, simply return if not os.path.isfile(record_file): return # stop the httpd service if already running if dispatcher.service_running(Httpd.DAEMON): dispatcher.stop_service(Httpd.DAEMON) # read files from the record file record = FilesRecordFile(record_file) sfiles = record.read() # restore those to their original locations for sfile in sfiles: sfile.copy_to(path_prefix=basedir) # ensure the various subdirs exists even if empty if OS.is_linux() and not os.path.isdir(os.path.join(Httpd.DOCUMENT_ROOT, "html")): os.mkdir(os.path.join(Httpd.DOCUMENT_ROOT, "html")) if OS.is_linux() and not os.path.isdir(os.path.join(Httpd.CONF_D, "logs")): os.mkdir(os.path.join(Httpd.CONF_D, "logs")) if OS.is_linux() and not os.path.isdir(os.path.join(Httpd.CONF_D, "run")): os.mkdir(os.path.join(Httpd.CONF_D, "run")) # start the httpd service dispatcher.start_service(Httpd.DAEMON)
def testBackupCertainFiles(self): os.mkdir(self.fs_root + "/subdir1") os.mkdir(self.fs_root + "/subdir1/subsubdir") os.mkdir(self.fs_root + "/subdir2") f=open(self.fs_root + "/subdir1/foo" , 'w') f.write("foo") f.close() f=open(self.fs_root + "/subdir2/bar" , 'w') f.write("bar") f.close() f=open(self.fs_root + "/subdir1/subsubdir/money" , 'w') f.write("money") f.close() backup_target = snap.backends.files.syum.Syum() backup_target.backup(self.basedir, include=[self.fs_root + "/subdir1"], exclude=[self.fs_root + "/subdir1/subsubdir"]) self.assertTrue(os.path.exists(self.basedir + self.fs_root + "/subdir1/foo")) self.assertFalse(os.path.exists(self.basedir + self.fs_root + "/subdir2/foo")) self.assertFalse(os.path.exists(self.basedir + self.fs_root + "/subdir1/subsubdir/money")) record = FilesRecordFile(self.basedir + "/files.xml") files = record.read() file_names = [] for sfile in files: file_names.append(sfile.path) self.assertEqual(1, len(files)) self.assertIn(self.fs_root[1:] + "/subdir1/foo", file_names) self.assertNotIn(self.fs_root[1:] + "/subdir2/bar", file_names) self.assertNotIn(self.fs_root[1:] + "/subdir1/subsubdir/bar", file_names)
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 testBackupCertainFiles(self): os.mkdir(os.path.join(self.fs_root, "subdir1")) os.mkdir(os.path.join(self.fs_root, "subdir1", "subsubdir")) os.mkdir(os.path.join(self.fs_root, "subdir2")) f = open(os.path.join(self.fs_root, "subdir1", "foo"), "w") f.write("foo") f.close() f = open(os.path.join(self.fs_root, "subdir2", "bar"), "w") f.write("bar") f.close() f = open(os.path.join(self.fs_root, "subdir1", "subsubdir", "money"), "w") f.write("money") f.close() backup_target = snap.backends.files.win.Win() backup_target.backup( self.basedir, include=[os.path.join(self.fs_root, "subdir1")], exclude=[os.path.join(self.fs_root, "subdir1", "subsubdir")], ) self.assertTrue( os.path.exists( os.path.join(self.basedir, SFile.windows_path_escape(os.path.join(self.fs_root, "subdir1", "foo"))) ) ) self.assertFalse( os.path.exists( os.path.join(self.basedir, SFile.windows_path_escape(os.path.join(self.fs_root, "subdir2", "foo"))) ) ) self.assertFalse( os.path.exists( os.path.join( self.basedir, SFile.windows_path_escape(os.path.join(self.fs_root, "subdir1", "subsubdir", "money")) ) ) ) record = FilesRecordFile(os.path.join(self.basedir, "files.xml")) files = record.read() file_names = [] for sfile in files: file_names.append(sfile.path) self.assertEqual(1, len(files)) self.assertIn(SFile.windows_path_escape(os.path.join(self.fs_root, "subdir1", "foo")), file_names) self.assertNotIn(SFile.windows_path_escape(os.path.join(self.fs_root, "subdir2", "bar")), file_names) self.assertNotIn( SFile.windows_path_escape(os.path.join(self.fs_root, "subdir1", "subsubdir", "bar")), file_names )
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): # 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 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 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): # 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 testBackupFiles(self): f=open(self.fs_root + "/foo" , 'w') f.write("foo") f.close() backup_target = snap.backends.files.syum.Syum() backup_target.backup(self.basedir, include=[self.fs_root]) self.assertTrue(os.path.exists(self.basedir + self.fs_root + "/foo")) record = FilesRecordFile(self.basedir + "/files.xml") files = record.read() file_names = [] for sfile in files: file_names.append(sfile.name) self.assertIn("foo", file_names) self.assertEqual(1, len(files))
def testBackupFiles(self): f=open(self.fs_root + "/foo" , 'w') f.write("foo") f.close() backup_target = snap.backends.files.syum.Syum() backup_target.backup(self.basedir, include=[self.fs_root]) self.assertTrue(os.path.exists(self.basedir + self.fs_root + "/foo")) record = FilesRecordFile(self.basedir + "/files.xml") files = record.read() file_names = [] for sfile in files: file_names.append(sfile.name) self.assertIn("foo", file_names) self.assertEqual(1, len(files))
def restore(self, basedir): """restore the files in the snapfile""" # if files record file isn't found, simply return if not os.path.isfile(basedir + "/files.xml"): return if snap.config.options.log_level_at_least('verbose'): snap.callback.snapcallback.message("Restoring files using apt backend"); # read files from the record file record = FilesRecordFile(basedir + "/files.xml") sfiles = record.read() # restore those to their original locations for sfile in sfiles: if snap.config.options.log_level_at_least('verbose'): snap.callback.snapcallback.message("Restoring file " + sfile.path); sfile.copy_to(self.fs_root, basedir)
def testReadFilesRecordFile(self): file_path = os.path.join(os.path.dirname(__file__), "data", "recordfile.xml") files = FilesRecordFile(file_path).read() file_paths = [] for sfile in files: file_paths.append(sfile.path) self.assertIn('/tmp/file1', file_paths) self.assertIn('/tmp/subdir/file2', file_paths)
def restore(self, basedir): out = open(OSUtils.null_file(), 'w') record_file = os.path.join(basedir, "service-iis.xml") # if files record file isn't found, simply return if not os.path.isfile(record_file): return # stop the httpd service if already running if WindowsDispatcher.is_feature_enabled(Iis.WEBSERVER_FEATURE): WindowsDispatcher.disable_feature(Iis.WEBSERVER_FEATURE) # read files from the record file record = FilesRecordFile(record_file) sfiles = record.read() # restore those to their original locations for sfile in sfiles: try: sfile.copy_to(path_prefix=basedir) except IOError, e: pass # just silently ignore errors for now
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 if snap.config.options.log_level_at_least('verbose'): snap.callback.snapcallback.message("Restoring files on windows"); # read files from the record file record = FilesRecordFile(os.path.join(basedir, "files.xml")) sfiles = record.read() # restore those to their original locations for sfile in sfiles: if snap.config.options.log_level_at_least('verbose'): snap.callback.snapcallback.message("Restoring file " + sfile.path); try: sfile.copy_to(self.fs_root, basedir) except: if snap.config.options.log_level_at_least('normal'): snap.callback.snapcallback.message("Failed to restore file " + sfile.path);
def restore(self, basedir): out = open(OSUtils.null_file(), 'w') record_file = os.path.join(basedir, "service-iis.xml") # if files record file isn't found, simply return if not os.path.isfile(record_file): return # stop the httpd service if already running if WindowsDispatcher.is_feature_enabled(Iis.WEBSERVER_FEATURE): WindowsDispatcher.disable_feature(Iis.WEBSERVER_FEATURE) # read files from the record file record = FilesRecordFile(record_file) sfiles = record.read() # restore those to their original locations for sfile in sfiles: try: sfile.copy_to(path_prefix=basedir) except IOError, e: pass # just silently ignore errors for now
def restore(cls, basedir): dispatcher = Dispatcher.os_dispatcher() record_file = os.path.join(basedir, "service-asterisk.xml") # if files record file isn't found, simply return if not os.path.isfile(record_file): return # stop the service if already running if dispatcher.service_running(Asterisk.DAEMON): dispatcher.stop_service(Asterisk.DAEMON) # read files from the record file record = FilesRecordFile(record_file) sfiles = record.read() # restore those to their original locations for sfile in sfiles: sfile.copy_to(path_prefix=basedir) # start the service dispatcher.start_service(Asterisk.DAEMON)
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 if snap.config.options.log_level_at_least('verbose'): snap.callback.snapcallback.message("Restoring files on windows") # read files from the record file record = FilesRecordFile(os.path.join(basedir, "files.xml")) sfiles = record.read() # restore those to their original locations for sfile in sfiles: if snap.config.options.log_level_at_least('verbose'): snap.callback.snapcallback.message("Restoring file " + sfile.path) try: sfile.copy_to(self.fs_root, basedir) except: if snap.config.options.log_level_at_least('normal'): snap.callback.snapcallback.message( "Failed to restore file " + sfile.path)
# 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 if snap.config.options.log_level_at_least('verbose'): snap.callback.snapcallback.message("Restoring files on windows"); # read files from the record file record = FilesRecordFile(os.path.join(basedir, "files.xml")) sfiles = record.read()
# 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 if snap.config.options.log_level_at_least('verbose'): snap.callback.snapcallback.message("Restoring files on windows") # read files from the record file record = FilesRecordFile(os.path.join(basedir, "files.xml")) sfiles = record.read()