Skip to content

Instantly share code, notes, and snippets.

@mreschke
Created February 3, 2016 14:31
Show Gist options
  • Save mreschke/5406ab8eef05c39a141a to your computer and use it in GitHub Desktop.
Save mreschke/5406ab8eef05c39a141a to your computer and use it in GitHub Desktop.
Delete old files
#!/usr/bin/env python
# Clean up old files around the server, called weekly Sunday 8am
# mReschke 2012-05-01
import os
import sys
import subprocess
import traceback
import datetime
def clean_oldfiles():
# ImportControl Processed 30days
clean('/store/data/Production/ftp/importcontrol/DYNA-SQL/processed/', 30)
# DynaMailPDF
clean('/store/data/Production/data/DynaMail/DynaMailPDF', 720, '*.txt', 2)
clean('/store/data/Production/data/DynaMail/DynaMailPDF', 720, '*.pdf', 3)
# NCOA Archives
clean('/store/data/Production/data/NCOA', 720)
# FTP
clean('/store/data/Production/ftp/', 30, '*-deleted', 1, True) #cleaned deleted account folders
clean('/store/data/Production/ftp/rci/processed/', 7)
clean('/store/data/Production/ftp/parkwayford/', 30, '*.csv')
clean('/store/data/Production/ftp/parkwayford/', 30, '*.pdf')
clean('/store/data/Production/ftp/qmalouann/', 30, '*.*', 999, True)
clean('/store/data/Production/ftp/CarMailPrintReview/OLD-CARMAIL-FILES/', 30, '*.*', 999, True)
clean('/store/data/Production/ftp/bmwdeclines/', 60)
# Other
cmd('cat /dev/null > /var/log/proftpd/tls.log')
cmd('cat /dev/null > /var/mail/mail')
################################################################################
################################################################################
def cmd(run, capture_output=False):
# Run the cmd
if capture_output:
proc = subprocess.Popen(run, universal_newlines=True, executable='bash', shell=True, stdout=subprocess.PIPE).stdout
return proc.read().strip()
else:
run = "bash -c '" + run + "'"
os.system(run)
#print run
def clean(path, older_than_days, file_filter='*', max_depth=999, include_folders=False):
del_cmd = 'rm -rfv'
#del_cmd = 'echo'
typestr = '-type f'
if include_folders: typestr = ''
cmd('find ' + path + ' -maxdepth ' + str(max_depth) + ' -iname "' + file_filter + '" ' + typestr + ' -mtime +' + str(older_than_days) + ' -exec ' + del_cmd + ' {} \; | tee -a ' + log)
def finish():
cmd('touch /tmp/clean_oldfiles.end.alive')
exit(0)
################################################################################
try:
# Globals
now = datetime.datetime.now()
log='/store/data/Production/log/Xenstore/CleanOldFiles/' + now.strftime("%Y-%m-%d") + '_CleanOldFiles.log'
cmd('touch /tmp/clean_oldfiles.begin.alive')
clean_oldfiles()
finish()
except Exception as e:
# On any error return 0 which means success.
# If this script returns 1 then postfix thinks mail delivery failed so it will
# Bounce back to the sender which would contain the error details like script line number, script
# On any errors, just return success so no mail will ever be returned
print str(e) + traceback.format_exc()
finish()
################################################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment