Skip to content

Instantly share code, notes, and snippets.

@jetchirag
Last active January 15, 2018 21:11
Show Gist options
  • Save jetchirag/bce4fd24e7aa040764def604380d6cc3 to your computer and use it in GitHub Desktop.
Save jetchirag/bce4fd24e7aa040764def604380d6cc3 to your computer and use it in GitHub Desktop.
Rotate backups files and folder older than
import os
import time
import shutil
# Declare paths below
# "{dir}": {OPTIONS}
# TYPES:
# 1: Files only
# 2: Folders only
# 3: Both
# NOTE: Make sure to add / after dir
dirList = {
"/home/acc/backups/": {"type": 1, "olderThan": 14},
}
# Declare paths above
curTime = time.time()
for dir in dirList:
for f in os.listdir(dir):
item = dir + f
itemType = dirList[dir]["type"]
itemTime = os.stat(item).st_mtime
lastUpdate = int(curTime - itemTime) / 86400
if itemType == 1:
if os.path.isfile(item):
if lastUpdate > dirList[dir]["olderThan"]:
os.remove(item)
elif itemType == 2:
if os.path.isdir(item):
shutil.rmtree(item)
elif itemType == 3:
if os.path.isfile(item):
os.remove(item)
elif os.path.isdir(item):
shutil.rmtree(item)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment