#!/usr/bin/python import sys, os, time ################################ define function called to check directories and files ############################### def backupfunc(currentfile): "Traverse a directory tree, looking for files to back up." # document string for the function global base, in_file, out_file, bu2_py_mtime # defined outside the function tempdir = base + "/" + currentfile # tempdir = absolute address of current directory if os.path.isdir(tempdir): # if it's a directory... os.chdir(tempdir) # go there backuplist = os.listdir(tempdir) # get a list of contents of the directory for entry in backuplist: # loop until all enries in the list are checked if os.path.islink(entry): # skip if this is a symbolic link pass # else: # otherwise, nextdir = currentfile + "/" + entry # create relative path to next lower directory if os.path.isdir(entry): # if the current directory is a directory backupfunc(nextdir) # now go check that directory os.chdir(tempdir) # get back to current directory else: # otherwise.. if os.path.getmtime(entry) > bu2_py_mtime: # if file is newer than script out_file.write(nextdir + "\n") # write the file name to the output file else: # otherwise it's a file, so... if os.path.getmtime(tempdir) > bu2_py_mtime: # if file newer than script out_file.write(currentfile + "\n") # write the file name to the output file ################################### # end of function definition ############################################ if os.environ[HOMEPATH] == "/home/Peter": # I back up from home directory so that the tar will os.chdir("/home/Peter") # hold paths relative to home directory base = os.getcwd() # defined as current working directory on entry infile = "bin/backuplist" # default name of full backup file/directory list outfile = "bin/incbackuplist" # default name of incremental backup file list tarfile = "incbackup" # default name of the incremental backup file # get arguments from command line. Watch out for the primitive error checking. if len(sys.argv) = 2: # one argument -- assign it to infile infile = sys.argv[1] if len(sys.argv) = 3: # two arguments -- assign next to outfile outfile = sys.argv[2] if len(sys.argv) = 4: # three arguments -- assign third to backup file name tarfile = sys.argv[3] if infile[0] != "/": # provide complete path to input and output files inf = base + "/" + infile # by appending current working directory, which means if outfile[0] != "/": # don't use "../" as part of the path. outf = base + "/" + outfile out_file = open(outf, "w") # open output file (e.g. incbackuplist) for writing. in_file = open(inf, "r") # open input file (e.g. backuplist) for reading. bu2_py_mtime = os.path.getmtime(sys.argv[0]) # get mtime for bu2_py print "The last backup was done ",(time.time() - bu2_py_mtime)/86400, "days ago.\n" a = in_file.readlines() # this will read the file into an array filelist = [b.rstrip() for b in a] # this will remove newlines. for x in range(0, len(filelist)): # main loop to check specified directories & files backupfunc(filelist[x]) # call function pointing to current entry in backup list out_file.close() # close the files in_file.close() # and report. print "Review",outfile,"to see which files will be backed up.\n" print "Execute at the system prompt: 'backmeup",tarfile,outfile,"inc' to complete the backup."