Exemple #1
0
    def test_getOlderFiles_zero_file(self):
        #In this case, the number of file < keep
        file_list = []
        expected_list = []
        now = datetime.datetime.now()

        result = get_older_files(file_list, days=0, keep=10)
        self.assertEqual(result, expected_list)
Exemple #2
0
 def test_getOlderFiles_no_older_keep0(self):
     file_list = []
     expected_list = []
     now = datetime.datetime.now()
     for day in range(1,9):
         date = now - datetime.timedelta(days=day)
         filename = date.strftime("%Y-%m-%d_%Hh%Mm%Ss")
         file_list.append(filename)
     result = get_older_files(file_list, days=10, keep=0)
     self.assertEqual(result, expected_list)
Exemple #3
0
    def _delete_old_files(self, days=10, keep=10):
        """
        Delete old archives in the destination

        :param days: delete files older than this value
        :type days: int
        :param keep: keep at least this amount of archives
        :type keep: int
        """
        #TODO : review logs

        path = os.path.join(self.destination.path, self.name)

        self.destination.check_availability()
        if self.destination.is_local():
            filenames = os.listdir(path)
        elif self.destination.is_ssh():
                command = ['ssh', '-t', self.destination.login, 'ls', '-1', path]
                self.logger.debug('SSH ls command: ' + str(command))
                process = subprocess.Popen(command, bufsize=4096, stdout=subprocess.PIPE)
                stdout, stderr = process.communicate()
                filenames = stdout.decode()
                filenames = filenames.split('\n')
                filenames = [x.strip('\r') for x in filenames if x != '']
        else:
            return

        to_delete = utils.get_older_files(filenames, days, keep)
        self.logger.debug("Backups available %s ", filenames)
        self.logger.debug("Backups to delete %s ", to_delete)

        self.destination.check_availability()
        if self.destination.is_local():
            for element in to_delete:
                self.logger.debug("Remove backup %s", element)
                try:
                    shutil.rmtree(os.path.join(path, element))
                except OSError:
                    self.logger.debug("Could not delete %s, try to chmod 644",
                                      os.path.join(path, element))
                    utils.r_chmod(os.path.join(path, element), 0o664)
                    try:
                        # try again
                        shutil.rmtree(os.path.join(path, element))
                    except OSError:
                        self.logger.error("Impossible to delete %s (symlink?)",
                                          os.path.join(path, element))
        elif self.destination.is_ssh():
            filepaths = [os.path.join(path, element) for element in to_delete]
            if filepaths != []:
                command = ['ssh', '-t', self.destination.login, 'rm', '-rf']
                command.extend(filepaths)
                self.logger.debug('SSH rm command: ' + str(command))
                process = subprocess.Popen(command, bufsize=4096, stdout=subprocess.PIPE)
                stdout, stderr = process.communicate()
Exemple #4
0
 def test_getOlderFiles_plenty_older_keep0_unexpected(self):
     file_list = ['foo']
     expected_list = []
     now = datetime.datetime.now()
     for day in range(1,20):
         date = now - datetime.timedelta(days=day)
         filename = date.strftime("%Y-%m-%d_%Hh%Mm%Ss")
         file_list.append(filename)
         if day >= 10:
             expected_list.append(filename)
     expected_list.sort()
     result = get_older_files(file_list, days=10, keep=0)
     self.assertEqual(result, expected_list)
Exemple #5
0
 def test_wrong_day_value(self):
     with self.assertRaises(ValueError):
         get_older_files([], days=-1, keep=0)