#!/usr/bin/python #Filename: back.py '''Backup uitility (Windows version)''' import os import time # switch of debuging DEBUG = True OS = "WINDOWS" # definitions of global if OS == "WINDOWS": rarPath = r'C:/Program Files/WinRAR' bklist_file = r'C:/Documents and Settings/%s/My Documents/bklist.txt'%os.getlogin() #main backup directory target_dir = r'c:/python32/backup'; #-------------------------------------------------------------------------- # definitions of function def EndSlash(strs): if strs.endswith(os.sep) == False: strs += os.sep return strs def PRINT(*arg): if DEBUG == True: print(*arg) else: pass #------------------------------------------------------------------------ # 1. The files and directories to be backed up are specified in a bklist.txt PRINT(bklist_file) source = [] f = open("%s"%bklist_file, 'r') for line in f: source.append(line.replace('/n','')) PRINT(source) f.close() #source = [r'c:/python32/back1', r'c:/python32/back2']; # 2. The backup must be stored in a main back directory # .. path string format target_dir = EndSlash(target_dir) i = 1; for i in range(0, len(source)): if (source[i].find('.')) == -1: source[i] = "/"" + EndSlash(source[i]) + "/"" rarPath = EndSlash(rarPath) # 3. The files are backed up into a rar file. # 4. The name of the rar archive is the current data and time target_dir = target_dir + time.strftime("%Y%m%d") if os.path.exists(target_dir) == False: os.mkdir(target_dir) PRINT(target_dir) target = target_dir + os.sep + time.strftime("%H%M%S") PRINT(target) # 5. We use the rar command (in Windows) to put files in a rar archive rar_command = "/"%srar/" a /"%s/" %s" %(rarPath, target, ' '.join(source)) PRINT(rar_command) # Run the backup if os.system("/"%s/""%rar_command) == 0: PRINT ('Successful backup to', target) else: PRINT ('Backup FAILED')