Advertisement
Guest User

backup.py

a guest
Mar 24th, 2011
3,264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.05 KB | None | 0 0
  1. import sys
  2. import os
  3. import hashlib
  4. import cPickle
  5. import zipfile
  6. dir_backup="Directory"
  7. dict={}
  8.  
  9. for (path,dirs,files) in os.walk(dir_backup):
  10.   if ((dirs == [] ) & (files == [])):
  11.     os.rmdir(path)
  12.  
  13. try:
  14.   checksum=open(os.path.join(dir_backup,"..","chksm.txt"),'r')
  15.   dict=cPickle.load(checksum)
  16.   checksum.close()
  17. except IOError:
  18.  
  19.   checksum=open(os.path.join(dir_backup,"..","chksm.txt"),'w')
  20.  
  21.   for (path,dirs,files) in os.walk(dir_backup):
  22.     for filename in files:
  23.       f_temp=open(os.path.join(path,filename),'r')
  24.       content=f_temp.read()
  25.       m=hashlib.md5()
  26.       m.update(content)
  27.       dict[os.path.join(path,filename)]=m.hexdigest()
  28.       f_temp.close()
  29.  
  30.   cPickle.dump(dict,checksum)
  31.   checksum.close()
  32.   delta=zipfile.ZipFile(os.path.join(dir_backup,"..","delta.zip"),'w')
  33.  
  34.   for (path,dirs,files) in os.walk(dir_backup):
  35.     for filename in files:
  36.       delta.write(os.path.join(path,filename))
  37.   delta.close()
  38.  
  39.   os.system("7za -mx=9 a delta.7z delta.zip")
  40.   os.remove("delta.zip")
  41.  
  42.   remove=open(os.path.join(dir_backup,"..","remove.txt"),'w')
  43.   remove.close()
  44.   exit()
  45.  
  46. remove=open(os.path.join(dir_backup,"..","remove.txt"),'w')
  47. for key in dict.keys():
  48.   if(os.path.exists(key) != 1):
  49.     remove.write(key)
  50.     remove.write('\n')
  51.     del dict[key]
  52. remove.close()
  53.  
  54. delta=zipfile.ZipFile(os.path.join(dir_backup,"..","delta.zip"),'w')
  55. for (path,dirs,files) in os.walk(dir_backup):
  56.   for filename in files:
  57.     f_temp=open(os.path.join(path,filename),'r')
  58.     content=f_temp.read()
  59.     m=hashlib.md5()
  60.     m.update(content)
  61.     f_temp.close()
  62.     try:
  63.       if(dict[os.path.join(path,filename)] != m.hexdigest()):
  64.     delta.write(os.path.join(path,filename))
  65.     dict[os.path.join(path,filename)] = m.hexdigest()
  66.     except KeyError:
  67.       delta.write(os.path.join(path,filename))
  68.       dict[os.path.join(path,filename)] = m.hexdigest()
  69. f=open(os.path.join(dir_backup,"..","chksm.txt"),'w')
  70. cPickle.dump(dict,f)
  71. f.close()
  72. delta.close()
  73. os.system("7za -mx=9 a delta.7z delta.zip")
  74. os.remove("delta.zip")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement