Skip to content

Instantly share code, notes, and snippets.

@RecNes
Last active June 20, 2018 08:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RecNes/ed41b6d98f187ea0a88eb116e0ab5d46 to your computer and use it in GitHub Desktop.
Save RecNes/ed41b6d98f187ea0a88eb116e0ab5d46 to your computer and use it in GitHub Desktop.
Remove old hourly backups
#!/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