#!/usr/bin/env python import tarfile import os import re import sys import time pattern = r'(.*)\.tar\.gz' suffix = '.tar.gz' ls = os.sep def 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 break while 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 break while True: backupname = raw_input('3. Take a name for the backup: ').strip() if len(backupname) == 0: print 'Input nothing, try again' continue elif '/' in backupname: print 'Please enter a name without "/".' continue break if 'tar.gz' in backupname: m = re.search(pattern,backupname) prefix = m.group(1).replace('.', '_').replace('-', '_') backupname = prefix + suffix else: prefix = backupname.replace('.', '_').replace('-', '_') backupname = prefix + suffix while True: if_continue = 0 prompt = ''' 4. Enter directorys or files which you want to exclude,if there is no directorys or files need to be excluded, press enter directly! (Note: absolute path is not needed) For example:dir1,dir2 or dir1 dir2 Enter:''' 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 = 1 break if if_continue: continue else: break backabs = 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 start...' start_time = time.time() tar_dir(dest_path,backupname,backupdir,excludes) print 'backup complete' end_time = time.time() print '#' * 60 print 'the backup took ' + 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 start...' start_time = time.time() tar_dir(dest_path,backupname,backupdir,excludes) print 'backup complete' end_time = time.time() print '#' * 60 print 'the backup took ' + str(round(end_time-start_time,1)) + 's.' print 'you can find the backup at: ' + backabs print '#' * 60