Esempio n. 1
0
    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)
Esempio n. 2
0
 def testSFileWindowsPathEscape(self):
     orig_path = "C:\\Program Files\\myfile"
     
     path = SFile.windows_path_escape(orig_path)
     self.assertEqual("C___Program Files\\myfile", path)
     
     path = SFile.windows_path_escape(path)
     self.assertEqual(orig_path, path)
Esempio n. 3
0
    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)
Esempio n. 4
0
    def testSFileWindowsPathEscape(self):
        orig_path = "C:\\Program Files\\myfile"

        path = SFile.windows_path_escape(orig_path)
        self.assertEqual("C___Program Files\\myfile", path)

        path = SFile.windows_path_escape(path)
        self.assertEqual(orig_path, path)
Esempio n. 5
0
    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)
Esempio n. 6
0
    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)
Esempio n. 7
0
    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)
Esempio n. 8
0
    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)
Esempio n. 9
0
    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)
Esempio n. 10
0
    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)
Esempio n. 11
0
    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))
Esempio n. 12
0
    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)
Esempio n. 13
0
    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"))
Esempio n. 14
0
    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))
Esempio n. 15
0
    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"))
Esempio n. 16
0
    def testIisService(self):
        # backup the iis conf directory and document roots
        test_backup_dir = os.path.join(self.basedir, "snap-iis-test")
        if os.path.isdir(test_backup_dir):
            shutil.rmtree(test_backup_dir)
        shutil.copytree(snap.backends.services.adapters.iis.Iis.CONFIG_ROOT, os.path.join(test_backup_dir, "conf"))

        # run the backup
        test_base_dir = os.path.join(self.basedir, "snap-iis-basedir")
        backend = snap.backends.services.adapters.iis.Iis()
        backend.backup(test_base_dir)

        # ensure conf.d and document root were backed up
        bfiles = []
        for root, dirs, files in os.walk(snap.backends.services.adapters.iis.Iis.CONFIG_ROOT):
            for hfile in files:
                rfile = os.path.join(root, hfile)
                ffile = os.path.join(test_base_dir, rfile)
                self.assertTrue(os.path.isfile(ffile))
                bfiles.append(os.path.join(root, hfile))
        self.assertTrue(os.path.isfile(os.path.join(test_base_dir, "service-iis.xml")))

        # run the restore
        shutil.rmtree(snap.backends.services.adapters.iis.Iis.CONFIG_ROOT, ignore_errors=True)
        backend.restore(test_base_dir)

        # ensure the files backed up were restored
        for hfile in bfiles:
            self.assertTrue(os.path.isfile(hfile))

        # ensure the features are enabled
        self.assertTrue(
            snap.backends.services.windowsdispatcher.WindowsDispatcher.is_feature_enabled(
                snap.backends.services.adapters.iis.Iis.WEBSERVER_FEATURE
            )
        )

        # restore backup
        shutil.rmtree(snap.backends.services.adapters.iis.Iis.CONFIG_ROOT, ignore_errors=True)
        for root, dirs, files in os.walk(os.path.join(test_backup_dir, "conf")):
            for idir in dirs:
                fdir = os.path.join(snap.backends.services.adapters.iis.Iis.CONFIG_ROOT, idir)
                if not os.path.isdir(fdir):
                    os.makedirs(fdir)
            for ifile in files:
                sfile = os.path.join(root, ifile)

                ffile = os.path.join(snap.backends.services.adapters.iis.Iis.CONFIG_ROOT, ifile)
                if not os.path.isfile(ffile):
                    shutil.copy(os.path.join(root, ifile), ffile)
                os.chmod(sfile, stat.S_IWRITE)
                os.remove(sfile)
        for bfile in bfiles:
            fbfile = os.path.join(test_base_dir, SFile.windows_path_escape(bfile))
            os.chmod(fbfile, stat.S_IWRITE)
            os.remove(fbfile)
        shutil.rmtree(test_backup_dir)
Esempio n. 17
0
    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)
Esempio n. 18
0
    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)
Esempio n. 19
0
    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"))
Esempio n. 20
0
    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"))
Esempio n. 21
0
    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)
Esempio n. 22
0
    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
        )
Esempio n. 23
0
    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)
Esempio n. 24
0
 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)
Esempio n. 25
0
            drives = c.split()[1:]
       
            # loop through each drive and determine which are available
            for drive in drives:
                include_drive = True
                try:
                    os.listdir(drive)
                except WindowsError, e:
                    include_drive = False
                if include_drive:
                    include.append(drive)

        # else apply path manipulation to specified includes
        else:
            for i in range(len(include)):
                include[i] = SFile.windows_path_escape(include[i])

        for additional_exclude in Win.EXCLUDE_DIRS:
            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'):
Esempio n. 26
0
    def testIisService(self):
        # backup the iis conf directory and document roots
        test_backup_dir = os.path.join(self.basedir, "snap-iis-test")
        if os.path.isdir(test_backup_dir):
            shutil.rmtree(test_backup_dir)
        shutil.copytree(snap.backends.services.adapters.iis.Iis.CONFIG_ROOT,
                        os.path.join(test_backup_dir, "conf"))

        # run the backup
        test_base_dir = os.path.join(self.basedir, "snap-iis-basedir")
        backend = snap.backends.services.adapters.iis.Iis()
        backend.backup(test_base_dir)

        # ensure conf.d and document root were backed up
        bfiles = []
        for root, dirs, files in os.walk(
                snap.backends.services.adapters.iis.Iis.CONFIG_ROOT):
            for hfile in files:
                rfile = os.path.join(root, hfile)
                ffile = os.path.join(test_base_dir, rfile)
                self.assertTrue(os.path.isfile(ffile))
                bfiles.append(os.path.join(root, hfile))
        self.assertTrue(
            os.path.isfile(os.path.join(test_base_dir, "service-iis.xml")))

        # run the restore
        shutil.rmtree(snap.backends.services.adapters.iis.Iis.CONFIG_ROOT,
                      ignore_errors=True)
        backend.restore(test_base_dir)

        # ensure the files backed up were restored
        for hfile in bfiles:
            self.assertTrue(os.path.isfile(hfile))

        # ensure the features are enabled
        self.assertTrue(
            snap.backends.services.windowsdispatcher.WindowsDispatcher.
            is_feature_enabled(
                snap.backends.services.adapters.iis.Iis.WEBSERVER_FEATURE))

        # restore backup
        shutil.rmtree(snap.backends.services.adapters.iis.Iis.CONFIG_ROOT,
                      ignore_errors=True)
        for root, dirs, files in os.walk(os.path.join(test_backup_dir,
                                                      "conf")):
            for idir in dirs:
                fdir = os.path.join(
                    snap.backends.services.adapters.iis.Iis.CONFIG_ROOT, idir)
                if not os.path.isdir(fdir):
                    os.makedirs(fdir)
            for ifile in files:
                sfile = os.path.join(root, ifile)

                ffile = os.path.join(
                    snap.backends.services.adapters.iis.Iis.CONFIG_ROOT, ifile)
                if not os.path.isfile(ffile):
                    shutil.copy(os.path.join(root, ifile), ffile)
                os.chmod(sfile, stat.S_IWRITE)
                os.remove(sfile)
        for bfile in bfiles:
            fbfile = os.path.join(test_base_dir,
                                  SFile.windows_path_escape(bfile))
            os.chmod(fbfile, stat.S_IWRITE)
            os.remove(fbfile)
        shutil.rmtree(test_backup_dir)
Esempio n. 27
0
File: win.py Progetto: russellb/snap
            drives = c.split()[1:]

            # loop through each drive and determine which are available
            for drive in drives:
                include_drive = True
                try:
                    os.listdir(drive)
                except WindowsError, e:
                    include_drive = False
                if include_drive:
                    include.append(drive)

        # else apply path manipulation to specified includes
        else:
            for i in range(len(include)):
                include[i] = SFile.windows_path_escape(include[i])

        for additional_exclude in Win.EXCLUDE_DIRS:
            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'):