Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion rotate_backups/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,12 @@ def timestamp(self):
@property
def week(self):
"""The ISO week number of :attr:`timestamp` (a number)."""
return self.timestamp.isocalendar()[1]
# for some days close to January 1, isocalendar()[1] may return the week number as 52 or 53
# eg: date(2022, 1, 1).isocalendar() returns (2021, 52, 6)
if self.year == self.timestamp.isocalendar()[0] + 1:
return 0
else:
return self.timestamp.isocalendar()[1]

def __getattr__(self, name):
"""Defer attribute access to :attr:`timestamp`."""
Expand Down
17 changes: 17 additions & 0 deletions rotate_backups/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,23 @@ def test_minutely_rotation(self):
assert os.path.exists(os.path.join(root, 'backup-2016-01-10_21-30-00'))
assert os.path.exists(os.path.join(root, 'backup-2016-01-10_21-45-00'))

def test_weekly_rotation(self):
"""Test weekly rotation."""
with TemporaryDirectory(prefix='rotate-backups-', suffix='-test-suite') as root:
os.mkdir(os.path.join(root, 'galera_backup_db4.sl.example.lab_2020-12-26_10-00'))
os.mkdir(os.path.join(root, 'galera_backup_db4.sl.example.lab_2021-01-02_10-00'))
os.mkdir(os.path.join(root, 'galera_backup_db4.sl.example.lab_2021-01-09_10-00'))
os.mkdir(os.path.join(root, 'galera_backup_db4.sl.example.lab_2021-01-16_10-00'))
os.mkdir(os.path.join(root, 'galera_backup_db4.sl.example.lab_2021-01-23_10-00'))
os.mkdir(os.path.join(root, 'galera_backup_db4.sl.example.lab_2021-01-30_10-00'))
run_cli(main, '--weekly=4', root)
assert os.path.exists(os.path.join(root, 'galera_backup_db4.sl.example.lab_2020-12-26_10-00')) is False
assert os.path.exists(os.path.join(root, 'galera_backup_db4.sl.example.lab_2021-01-02_10-00')) is False
assert os.path.exists(os.path.join(root, 'galera_backup_db4.sl.example.lab_2021-01-09_10-00'))
assert os.path.exists(os.path.join(root, 'galera_backup_db4.sl.example.lab_2021-01-16_10-00'))
assert os.path.exists(os.path.join(root, 'galera_backup_db4.sl.example.lab_2021-01-23_10-00'))
assert os.path.exists(os.path.join(root, 'galera_backup_db4.sl.example.lab_2021-01-30_10-00'))

def test_removal_command(self):
"""Test that the removal command can be customized."""
with TemporaryDirectory(prefix='rotate-backups-', suffix='-test-suite') as root:
Expand Down