Last active
June 20, 2018 08:35
-
-
Save RecNes/ed41b6d98f187ea0a88eb116e0ab5d46 to your computer and use it in GitHub Desktop.
Remove old hourly backups
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import shutil | |
import os | |
from datetime import datetime | |
print(" ".join(("-"*20, "Removing Hourly Backups Older Than Today", "-"*20))) | |
path_to_backups = '/var/backups/sqlbackups' | |
today = datetime.today().date() | |
for folder_name in os.listdir(path_to_backups): | |
folder_date = datetime.strptime(folder_name, '%Y_%m_%d_%H').date() | |
year_diff = today.year - folder_date.year | |
if today != folder_date and not folder_name.endswith('_00'): | |
# Remove hourly backups older than today without touching last backup of that day. | |
shutil.rmtree(os.path.join(path_to_backups, folder_name)) | |
if year_diff > 0: | |
# Remove previous years daily last backups | |
shutil.rmtree(os.path.join(path_to_backups, folder_name)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment