#!/usr/bin/env python '''This script is used for backup directorys. you can backup a directory at a time,and also you can backup multiple directorys at a time. ''' import tarfile import os import sys # # the following lines is used to process the directorys # to be backed up # dirs_src = raw_input('Enter directorys that which you want to backup: ') if dirs_src == '': sys.exit('you should enter at least one directory') dirs_src = dirs_src.replace(',', ' ').split(' ') for judge in dirs_src: if os.path.isfile(judge): sys.exit('%s is not a directory' % judge) dirs = [] for d in dirs_src: dirs.append(d.rstrip('/')) # # the following lines is used to process the directory # to be placed # dest = raw_input('Enter storage path: ') if dest == '': sys.exit('you should input a storage path') if not os.path.exists(dest): os.makedirs(dest) if dest[-1] == '/': pass else: dest = dest+'/' # # backup section # for dir in dirs: if os.path.exists(dir): tar_name = dir.lstrip('/').replace('/', '-') os.chdir(dir) print dir+' backup is beging...' files = [n for n in os.listdir(dir)] tar = tarfile.open('%s%s.tar.gz' % (dest,tar_name),'w:gz') for name in files: tar.add(name) tar.close() print dir+' backup finished' else: print 'There is no directory called '+ dir