Guest User

backup

a guest
Nov 4th, 2011
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.65 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # Backup files - As published in Python Cookbook
  4. # by O'Reilly with some bug-fixes.
  5.  
  6. # Credit: Anand Pillai, Tiago Henriques, Mario Ruggier
  7. import sys,os, shutil, filecmp
  8.  
  9. MAXVERSIONS=100
  10. BAKFOLDER = '.bak'
  11.  
  12. def backup_files(tree_top, bakdir_name=BAKFOLDER):
  13.     """ Directory back up function. Takes the top-level
  14.    directory and an optional backup folder name as arguments.
  15.    By default the backup folder is a folder named '.bak' created
  16.    inside the folder which is backed up. If another directory
  17.    path is passed as value of this argument, the backup versions
  18.    are created inside that directory instead. Maximum of
  19.    'MAXVERSIONS' simultaneous versions can be maintained
  20.  
  21.    Example usage
  22.    -------------
  23.    
  24.    The command
  25.    $ python backup.py ~/programs
  26.    
  27.    will create backups of every file inside ~/programs
  28.    inside sub-directories named '.bak' inside each folder.
  29.    For example, the backups of files inside ~/programs will
  30.    be found in ~/programs/.bak, the backup of files inside
  31.    ~/programs/python in ~/programs/python/.bak etc.
  32.  
  33.    The command
  34.    $ python backup.py ~/programs ~/backups
  35.  
  36.    will create backups of every file inside ~/backups/programs
  37.    folder. No .bak folder is created. Instead backups of
  38.    files in ~/programs will be inside ~/backups/programs,
  39.    backups of files in ~/programs/python will be inside
  40.    ~/backups/programs/python etc.
  41.    
  42.    """
  43.    
  44.     top_dir = os.path.basename(tree_top)
  45.     tree_top += os.sep
  46.    
  47.     for dir, subdirs, files in os.walk(tree_top):
  48.  
  49.         if os.path.isabs(bakdir_name):
  50.             relpath = dir.replace(tree_top,'')
  51.             backup_dir = os.path.join(bakdir_name, top_dir, relpath)
  52.         else:
  53.             backup_dir = os.path.join(dir, bakdir_name)
  54.  
  55.         if not os.path.exists(backup_dir):
  56.             os.makedirs(backup_dir)
  57.  
  58.         # To avoid recursing into sub-directories
  59.         subdirs[:] = [d for d in subdirs if d != bakdir_name]
  60.         for f in files:
  61.             filepath = os.path.join(dir, f)
  62.             destpath = os.path.join(backup_dir, f)
  63.             # Check existence of previous versions
  64.             for index in xrange(MAXVERSIONS):
  65.                 backup = '%s.%2.2d' % (destpath, index)
  66.                 abspath = os.path.abspath(filepath)
  67.                
  68.                 if index > 0:
  69.                     # No need to backup if file and last version
  70.                     # are identical
  71.                     old_backup = '%s.%2.2d' % (destpath, index-1)
  72.                     if not os.path.exists(old_backup): break
  73.                     abspath = os.path.abspath(old_backup)
  74.                    
  75.                     try:
  76.                         if os.path.isfile(abspath) and filecmp.cmp(abspath, filepath, shallow=False):
  77.                             continue
  78.                     except OSError:
  79.                         pass
  80.                
  81.                 try:
  82.                     if not os.path.exists(backup):
  83.                         print 'Copying %s to %s...' % (filepath, backup)
  84.                         shutil.copy(filepath, backup)
  85.                 except (OSError, IOError), e:
  86.                     pass
  87.  
  88. if __name__=="__main__":
  89.     if len(sys.argv)<2:
  90.         sys.exit("Usage: %s [directory] [backup directory]" % sys.argv[0])
  91.        
  92.     tree_top = os.path.abspath(os.path.expanduser(os.path.expandvars(sys.argv[1])))
  93.    
  94.     if len(sys.argv)>=3:
  95.         bakfolder = os.path.abspath(os.path.expanduser(os.path.expandvars(sys.argv[2])))
  96.     else:
  97.         bakfolder = BAKFOLDER
  98.        
  99.     if os.path.isdir(tree_top):
  100.         backup_files(tree_top, bakfolder)
  101.  
Advertisement
Add Comment
Please, Sign In to add comment