pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

pastebin private pastebin - collaborative debugging tool What's a private pastebin?

Posted by frymaster on Sun 11th Dec 16:43
download | new post

  1. #!/usr/bin/python
  2.  
  3. # check free space on backup drive
  4. # if less than free_space_plimsoll gb:
  5. #       don't delete backups more recent than keep_all days
  6. #       for backups between keep_all and keep_week days, only leave Monday morning's backup and the first of the month
  7. #       for backups older than keep_week days, only leave the first of the month
  8. #       KEEP_WEEKS IN IS DAYS, NOT WEEKS
  9.  
  10. import os,re,datetime,shutil
  11. from os.path import join
  12.  
  13. keep_all = 14
  14. keep_week = 52
  15. backup_location = "/backup"
  16. free_space_plimsoll = 200       # On fruitbat's insistance
  17. dont_del_me_bro = re.compile("^(?P<year>[0-9]{2,4})-(?P<month>[0-9]{2})-(?P<day>[0-9]{2})--(?P<minutes>[0-9]{4})$")
  18.  
  19. theFilesystem = os.statvfs(backup_location)
  20. freeSpace = theFilesystem.f_bavail * theFilesystem.f_bsize / 1024 / 1024 / 1024.0
  21.  
  22. if freeSpace < free_space_plimsoll:
  23.         print "Free space below trigger level - deleting old backups"
  24.         theFiles = os.listdir(backup_location)
  25.         now = datetime.date.today()
  26.         for item in theFiles:
  27.                 if os.path.isdir(join(backup_location,item)):
  28.                         result = dont_del_me_bro.match(item)
  29.                         if result:
  30.                                 year = int(result.group("year"))
  31.                                 if year < 100: year = year + 2000
  32.                                 then = datetime.date(year,int(result.group("month")),int(result.group("day")))
  33.                                 if (now - then).days > keep_all:
  34.                                         if (now - then).days > keep_week:
  35.                                                 #Delete all but first of month
  36.                                                 if not (then.day == 1):
  37.                                                         print "Delete:",join(backup_location,item)
  38.                                                         shutil.rmtree(join(backup_location,item))
  39.                                         else:
  40.                                                 #Delete all but first of month and first of week
  41.                                                 if not (then.day == 1 or then.isoweekday() == 7):
  42.                                                         print "Delete:",join(backup_location,item)
  43.                                                         shutil.rmtree(join(backup_location,item))
  44.  
  45. else:
  46.         print "Free space above trigger level - doing nothing"

Submit a correction or amendment below. (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Use syntax highlighting

To highlight particular lines, prefix each line with @@


Remember my settings