Skip to content

Instantly share code, notes, and snippets.

@janoamaral
Last active April 30, 2020 13:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janoamaral/b0377c5f3e07bc73036b774f437555e1 to your computer and use it in GitHub Desktop.
Save janoamaral/b0377c5f3e07bc73036b774f437555e1 to your computer and use it in GitHub Desktop.
Limpieza de backups viejos de Windows
#!/usr/bin/env python3
"""
Module Docstring
"""
__author__ = "Alejandro Amaral"
__author__ = "Emmanuel Arcumano"
__version__ = "0.0.1"
__license__ = "MIT"
"""
USO:
limpieza_backup.py /path/al/directorio/raiz
Ejemplo:
./limpieza_backup.py /var/shares
"""
import os, sys
from datetime import datetime, timedelta
from shutil import rmtree
def main(baseDir):
""" Main entry point of the app """
with os.scandir(baseDir) as entries:
for i, entry in enumerate(entries):
if not entry.name.startswith('.') and entry.is_dir():
with os.scandir(entry.path) as backupDir:
for i, bd in enumerate(backupDir):
if not bd.name.startswith('.') and bd.is_dir():
with os.scandir(bd.path) as winback:
for i, wb in enumerate(winback):
if not wb.name.startswith('.') and wb.is_dir() and wb.name.startswith('Backup Set'):
fecha=(datetime.fromtimestamp(wb.stat(follow_symlinks=False).st_mtime)) - datetime.today()
# Si el backup tiene más de 90 días limpiarlo.
if (fecha.days < -90):
print('Eliminando ' + wb.path)
# Para depurar es necesario apretar enter para eliminar. Con Ctrl+C se detiene el script
#input("Press Enter to continue...")
rmtree(wb.path)
if __name__ == "__main__":
""" This is executed when run from the command line """
main(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment