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 17:04
download | new post

  1. #!/usr/bin/python
  2.  
  3. import os,re,datetime,shutil
  4. from os.path import join
  5. import sys
  6.  
  7. keep_at_least = 5 # will never go below this many backups
  8. keep_no_more_than = 10 # will never keep more than this many backups
  9. free_space_plimsoll = 2000 # subject to the above, will keep deleting until free space on the disk is above this many gigabytes
  10. backup_location = "/home/localhost/frymaster"
  11.  
  12. if keep_at_least > keep_no_more_than: keep_no_more_than = keep_at_least
  13.  
  14. 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})$")
  15.  
  16. def freeSpace():
  17.         theFilesystem = os.statvfs(backup_location)
  18.         freeSpace = theFilesystem.f_bavail * theFilesystem.f_bsize / 1024 / 1024 / 1024.0
  19.  
  20. theFiles = sorted(os.listdir(backup_location))
  21. theFiles = [x for x in theFiles if dont_del_me_bro.match(x)]
  22.  
  23. while len(theFiles) > keep_no_more_than:
  24.         item = theFiles[0]
  25.         print "Removing",item,"because we have too many backups"
  26.         shutil.rmtree(join(backup_location,item))
  27.         theFiles.remove(item)
  28.  
  29. while freeSpace() < free_space_plimsoll:
  30.         if len(theFiles) <= keep_at_least:
  31.                 print "Need to remove more files to reach space requirement, but being forced to keep some backups"
  32.                 break
  33.         item = theFiles[0]
  34.         print "Removing",item,"to reach space requirements"
  35.         shutil.rmtree(join(backup_location,item))
  36.         theFiles.remove(item)

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