Exemplo n.º 1
0
def autoclean(assume_yes=False):
    """
    Remove a series of backup files based on the following rules:
    * Keeps all the backups from the last 7 days
    * Keeps the most recent backup from each week of the last month
    * Keeps the most recent backup from each month of the last year
    * Keeps the most recent backup from each year of the remaining years
    """

    # check if there are backups
    backup = Backup()
    backup.files = tuple(backup.target.get_files())
    if not backup.files:
        click.echo("==> No backups found.")
        return None

    # get black and white list
    cleaning = BackupAutoClean(backup.get_timestamps())
    white_list = cleaning.white_list
    black_list = cleaning.black_list
    if not black_list:
        click.echo("==> No backup to be deleted.")
        return None

    # print the list of files to be kept
    click.echo(f"\n==> {len(white_list)} backups will be kept:")
    for date_id in white_list:
        date_formated = backup.target.parse_timestamp(date_id)
        click.echo(f"\n    ID: {date_id} (from {date_formated})")
        for f in backup.by_timestamp(date_id):
            click.echo(f"    {backup.target.path}{f}")

    # print the list of files to be deleted
    delete_list = list()
    click.echo(f"\n==> {len(black_list)} backups will be deleted:")
    for date_id in black_list:
        date_formated = backup.target.parse_timestamp(date_id)
        click.echo(f"\n    ID: {date_id} (from {date_formated})")
        for f in backup.by_timestamp(date_id):
            click.echo(f"    {backup.target.path}{f}")
            delete_list.append(f)

    # delete
    confirm = Confirm(assume_yes)
    if confirm.ask():
        for name in delete_list:
            backup.target.delete_file(name)
            click.echo(f"    {name} deleted.")
    backup.close_ftp()
Exemplo n.º 2
0
def autoclean(assume_yes=False):
    """
    Remove a series of backup files based on the following rules:
    * Keeps all the backups from the last 7 days
    * Keeps the most recent backup from each week of the last month
    * Keeps the most recent backup from each month of the last year
    * Keeps the most recent backup from each year of the remaining years
    """

    # check if there are backups
    backup = Backup()
    backup.files = tuple(backup.target.get_files())
    if not backup.files:
        print('==> No backups found.')
        return None

    # get black and white list
    cleaning = BackupAutoClean(backup.get_timestamps())
    white_list = cleaning.white_list
    black_list = cleaning.black_list
    if not black_list:
        print('==> No backup to be deleted.')
        return None

    # print the list of files to be kept
    print('\n==> {} backups will be kept:'.format(len(white_list)))
    for date_id in white_list:
        date_formated = backup.target.parse_timestamp(date_id)
        print('\n    ID: {} (from {})'.format(date_id, date_formated))
        for f in backup.by_timestamp(date_id):
            print('    {}{}'.format(backup.target.path, f))

    # print the list of files to be deleted
    delete_list = list()
    print('\n==> {} backups will be deleted:'.format(len(black_list)))
    for date_id in black_list:
        date_formated = backup.target.parse_timestamp(date_id)
        print('\n    ID: {} (from {})'.format(date_id, date_formated))
        for f in backup.by_timestamp(date_id):
            print('    {}{}'.format(backup.target.path, f))
            delete_list.append(f)

    # delete
    confirm = Confirm(assume_yes)
    if confirm.ask():
        for name in delete_list:
            backup.target.delete_file(name)
            print('    {} deleted.'.format(name))
    backup.close_ftp()
Exemplo n.º 3
0
class TestBackup(TestCase):

    FILES = (
        "BRA-19940704123000-USA.gz",
        "BRA-19940709163000-NED.gz",
        "BRA-19940713123000-SWE.gz",
        "BRA-19940717123000-ITA.gz",
    )

    @patch("flask_alchemydumps.backup.decouple.config")
    def setUp(self, mock_config):
        self.tmp = TemporaryDirectory()

        # Respectively: FTP server, FTP user, FTP password, FTP path, local
        # directory for backups and file prefix
        mock_config.side_effect = (None, None, None, None, self.tmp.name,
                                   "BRA")

        # main objects
        self.backup = Backup()
        self.backup.files = tuple(self.files)

    def tearDown(self):
        self.tmp.cleanup()

    @property
    def files(self):
        for name in self.FILES:
            yield Path(self.tmp.name) / name

    def test_get_timestamps(self):
        self.assertEqual(
            sorted(("19940704123000", "19940709163000", "19940713123000",
                    "19940717123000")),
            sorted(self.backup.get_timestamps()),
        )

    def test_by_timestamp(self):
        self.assertEqual(
            (Path(self.tmp.name) / "BRA-19940717123000-ITA.gz", ),
            tuple(self.backup.by_timestamp("19940717123000")),
        )

    def test_valid(self):
        self.assertTrue(self.backup.valid("19940704123000"))
        self.assertFalse(self.backup.valid("19980712210000"))

    def test_get_name(self):
        self.assertEqual(f"BRA-{self.backup.target.TIMESTAMP}-GER.gz",
                         self.backup.get_name("GER"))
Exemplo n.º 4
0
class TestBackup(TestCase):

    FILES = (
        'BRA-19940704123000-USA.gz',
        'BRA-19940709163000-NED.gz',
        'BRA-19940713123000-SWE.gz',
        'BRA-19940717123000-ITA.gz',
    )

    @patch.object(LocalTools, 'normalize_path')
    @patch('flask_alchemydumps.backup.decouple.config')
    def setUp(self, mock_config, mock_path):
        # (respectively: FTP server, FTP # user, FTP password, FTP path, local
        # directory for backups and file prefix)
        mock_config.side_effect = (None, None, None, None, 'foobar', 'BRA')
        self.backup = Backup()
        self.backup.files = self.FILES

    @patch.object(LocalTools, 'normalize_path')
    def test_get_timestamps(self, mock_path):
        expected = [
            '19940704123000',
            '19940709163000',
            '19940713123000',
            '19940717123000'
        ]
        self.assertEqual(expected, self.backup.get_timestamps())

    @patch.object(LocalTools, 'normalize_path')
    def test_by_timestamp(self, mock_path):
        expected = ['BRA-19940717123000-ITA.gz']
        self.assertEqual(expected, list(self.backup.by_timestamp('19940717123000')))

    @patch.object(LocalTools, 'normalize_path')
    def test_valid(self, mock_path):
        self.assertTrue(self.backup.valid('19940704123000'))
        self.assertFalse(self.backup.valid('19980712210000'))

    @patch.object(LocalTools, 'normalize_path')
    def test_get_name(self, mock_path):
        expected = 'BRA-{}-GER.gz'.format(self.backup.target.TIMESTAMP)
        self.assertEqual(expected, self.backup.get_name('GER'))
Exemplo n.º 5
0
def remove(date_id, assume_yes=False):
    """Remove a series of backup files based on the date part of the files"""

    # check if date/id is valid
    backup = Backup()
    if backup.valid(date_id):

        # List files to be deleted
        delete_list = tuple(backup.by_timestamp(date_id))
        print('==> Do you want to delete the following files?')
        for name in delete_list:
            print('    {}{}'.format(backup.target.path, name))

        # delete
        confirm = Confirm(assume_yes)
        if confirm.ask():
            for name in delete_list:
                backup.target.delete_file(name)
                print('    {} deleted.'.format(name))
    backup.close_ftp()
Exemplo n.º 6
0
def remove(date_id, assume_yes=False):
    """Remove a series of backup files based on the date part of the files"""

    # check if date/id is valid
    backup = Backup()
    if backup.valid(date_id):

        # List files to be deleted
        delete_list = tuple(backup.by_timestamp(date_id))
        click.echo("==> Do you want to delete the following files?")
        for name in delete_list:
            click.echo(f"    {backup.target.path}{name}")

        # delete
        confirm = Confirm(assume_yes)
        if confirm.ask():
            for name in delete_list:
                backup.target.delete_file(name)
                click.echo(f"    {name} deleted.")
    backup.close_ftp()
Exemplo n.º 7
0
class TestBackup(TestCase):

    FILES = (
        'BRA-19940704123000-USA.gz',
        'BRA-19940709163000-NED.gz',
        'BRA-19940713123000-SWE.gz',
        'BRA-19940717123000-ITA.gz',
    )

    @patch.object(LocalTools, 'normalize_path')
    @patch('flask_alchemydumps.backup.decouple.config')
    def setUp(self, mock_config, mock_path):
        # (respectively: FTP server, FTP # user, FTP password, FTP path, local
        # directory for backups and file prefix)
        mock_config.side_effect = (None, None, None, None, 'foobar', 'BRA')
        self.backup = Backup()
        self.backup.files = self.FILES

    @patch.object(LocalTools, 'normalize_path')
    def test_get_timestamps(self, mock_path):
        expected = [
            '19940704123000', '19940709163000', '19940713123000',
            '19940717123000'
        ]
        self.assertEqual(expected, self.backup.get_timestamps())

    @patch.object(LocalTools, 'normalize_path')
    def test_by_timestamp(self, mock_path):
        expected = ['BRA-19940717123000-ITA.gz']
        self.assertEqual(expected,
                         list(self.backup.by_timestamp('19940717123000')))

    @patch.object(LocalTools, 'normalize_path')
    def test_valid(self, mock_path):
        self.assertTrue(self.backup.valid('19940704123000'))
        self.assertFalse(self.backup.valid('19980712210000'))

    @patch.object(LocalTools, 'normalize_path')
    def test_get_name(self, mock_path):
        expected = 'BRA-{}-GER.gz'.format(self.backup.target.TIMESTAMP)
        self.assertEqual(expected, self.backup.get_name('GER'))
Exemplo n.º 8
0
def history():
    """List existing backups"""

    backup = Backup()
    backup.files = tuple(backup.target.get_files())

    # if no files
    if not backup.files:
        print('==> No backups found at {}.'.format(backup.target.path))
        return None

    # create output
    timestamps = backup.get_timestamps()
    groups = [{'id': i, 'files': backup.by_timestamp(i)} for i in timestamps]
    for output in groups:
        if output['files']:
            date_formated = backup.target.parse_timestamp(output['id'])
            print('\n==> ID: {} (from {})'.format(output['id'], date_formated))
            for file_name in output['files']:
                print('    {}{}'.format(backup.target.path, file_name))
    print('')
    backup.close_ftp()
Exemplo n.º 9
0
def history():
    """List existing backups"""

    backup = Backup()
    backup.files = tuple(backup.target.get_files())

    # if no files
    if not backup.files:
        click.echo(f"==> No backups found at {backup.target.path}.")
        return None

    # create output
    timestamps = backup.get_timestamps()
    groups = [{"id": i, "files": backup.by_timestamp(i)} for i in timestamps]
    for output in groups:
        if output["files"]:
            date_formated = backup.target.parse_timestamp(output["id"])
            click.echo(f"\n==> ID: {output['id']} (from {date_formated})")
            for file_name in output["files"]:
                click.echo(f"    {backup.target.path}{file_name}")
    click.echo("")
    backup.close_ftp()