#!/usr/bin/env pythonimport tarfileimport osimport reimport sysimport Timepattern = r ' (. *) \.tar\.gz ' suffix = '. tar.gz ' Ls = os.sepdef tar_dir ( Stpath,back_name,back_dir,excludedir): tar = tarfile.open ('%s%s ' % (stpath,back_name), ' W:gz ') for name in os.listdir ('%s ' % back_dir): os.chdir ('%s ' % back_dir) if name not in excludedir: tar.add (name) tar.close () while true: backupdir = raw_input (' 1. enter a directroy which you want to back up: '). Strip () if len ( Backupdir) == 0: print ' Input nothing, try again ' continue elif os.path.isfile (backupdir): print backupdir + ' is not a directory,try again ' continue elif not os.path.isabs (backupdir): print ' Please enter an absolute path, try again ' continue elif not os.path.exists (Backupdir): print ' there is no directory called ' + backupdir continue elif backupdir[-1] != '/': backupdir = backupdir + ls breakwhile true: dest _path = raw_input (' 2. enter a storage path: '). Strip () if len (dest_ Path) == 0: print ' Input nothing, try again ' continue if not os.path.exists (Dest_path) : os.makedirs (Dest_path) if not os.path.isabs (dest_ Path): print ' please enter an absolute path, try Again ' continue if dest_path[-1] != '/': dest_path = dest_path + ls breakwhile true: backupname = raw_input (' 3. take a name for the backup: '). Strip () if len (backupname) == 0: print ' Input&nbsP;nothing, try again ' continue elif '/' in backupname: print ' please enter a name without '/'. ' continue breakif ' tar.gz ' in backupname: m = re.search (Pattern,backupname) prefix = M.group (1). replace ('. ', ' _ '). Replace ('-', ' _ ') backupname = prefix + suffixelse: prefix = backupname.replace ('. ', ' _ '). Replace (' -', ' _ ') backupname = prefix + suffixwhile True: if_continue = 0 prompt = ' 4. enter directorys or files which you want to exclude,if thereis no Directorys or files need to be excluded, press enter directly! (note: absolute path is not needed) For example:dir1,dir2 or dir1 dir2enter: " excludes = Raw_input (Prompt). Strip () if '/' in excludes: print ' \nwarning: absolute path is not needed\n ' continue excludes = excludes.replace (' ', ', '). Split (', ') for i in excludes: if not os.path.exists (backupdir+i): print ' \nwarning: there is no file or directory called ' + ( backupdir + i) + ' \ n ' if_continue&nbsP;= 1 break if if_continue: continue else: Breakbackabs = dest_path + backupname if os.path.exists ( Backabs): while true: anwser = raw_input (' The backup %s is already exists, do you want to overwrite it (yes/no)? ' % backabs) if anwser == ' yes ': print ' backup is begining ' start_time = time.time () tar_dir (dest_path,backupname,backupdir,excludes) print ' backup has been finished ' end_time = time.time () print ' # ' * 60 print ' this backup used ' + str (Round (end_time-start_time,1)) + ' s. ' print ' you can find the backup at: ' + backabs print ' # ' * 60 break elif anwser == ' no ': print ' the backup for %s has Been caceled ' % backupdir break Sys.exit () else: print ' invalid option, try again ' else: print ' backup is begining ... ' start_time = time.time () tar_dir (dest_path,backupname , backupdir,excludes) print ' backup has been finished ' end_time = time.time () print ' # ' * 60 print ' this backup used ' + str (Round (end_time-start_time,1)) + ' s. ' print ' you can find the backup at: ' + backabs print ' # ' * 60
This article is from the "Never Stop" blog, please be sure to keep this source http://wjcaiyf.blog.51cto.com/7105309/1695756
Backup Script Two