Easy backup cleanups

Posted: December 4th, 2009 | Author: | Filed under: Nerdery | Tags: , | No Comments »

I often create little cron jobs that create backups of various things on linux systems. Typically there’s a baks dir that has a tarball for each day with some clever name like ‘bak-2009-12-04.tar.gz’. This works well, but it can be a little difficult to clean the directory up without moving to a much more complicated system. I wrote a little python script to help me sort out the ones I wanted to save and the ones I wanted to trash. It took me a while to find it, so I’m posting here for posterity.

The basic idea is it loops through all the files in the current directory and moves ones older than a certain dateĀ  that don’t meet certain criteria to a trash folder. I have it configured to save every day for the last month, every week for the last 6 months, and the beginning of every month forever. You can then delete the trash manually, but it makes the script fairly non-destructive.

#!/usr/bin/env python
import os, time
 
def categorize(zeta):
	for file, date in zeta:	
		if date.tm_mday == 1: #save the first bak for each month, no matter what
			tosave.append(file)
		elif time.localtime(time.time() - 15778463) > date: #if the file is over 6 months old delete it
			tomove.append(file)
		elif time.localtime(time.time() - 15778463) < date and date.tm_wday == 0: #if the file is less than 6 months old and made on a sunday than delete it 
			tosave.append(file)
		elif time.localtime(time.time() - 2592000) < date: #if the file is less than a month old, save it
			tosave.append(file)
		else:
			tomove.append(file)
 
def makeEasier(files):
	zeta = []
	for file in files:
		if "tar" in file:
			if file.startswith("trac"):
				date = file[5:13]
			elif file.startswith("svn"):
				date = file[4:12]
			else:
				continue		
			date = time.strptime(date, "%Y%m%d")
			zeta.append([file,date])
	return zeta
 
def moveEm():
	for file in tomove:
		if file not in tosave:
			try:
				os.rename(path+'/'+file,path+"/trash/"+file)
			except Exception, detail:
				print "Error with", file, ":", detail
			else:
				print "Moved", file, "to trash"
 
if __name__ == "__main__":
	path = os.getcwd()
	tomove = []
	tosave = []
 
	files = os.listdir(path)
	zeta = makeEasier(files)
	categorize(zeta)
	moveEm()
No Comments »