Skip to content

Instantly share code, notes, and snippets.

@faisalraja
Created October 1, 2014 21:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save faisalraja/bf648f9a2446eb4967f2 to your computer and use it in GitHub Desktop.
Save faisalraja/bf648f9a2446eb4967f2 to your computer and use it in GitHub Desktop.
Python Differential Backup Script for Immutable Files
import os
import shutil
__author__ = 'faisal'
backup_folders = [
# folders to backup, backup location, ignore top level directories
('D:\\', 'H:\\Drive_D\\', ['_temp', 'Downloads', '$RECYCLE.BIN']),
('E:\\', 'H:\\Drive_E\\', ['$RECYCLE.BIN'])
]
cached_list = {}
def get_files(path, ignores=[]):
if path in cached_list:
return cached_list[path]
f = []
for (dirpath, dirnames, filenames) in os.walk(path):
prefix = dirpath.replace(path, '')
f.extend([os.path.join(prefix, filename) for filename in filenames if prefix.split(os.sep).pop(0) not in ignores])
cached_list[path] = f
return f
for folder in backup_folders:
print 'Getting source files...'
src = get_files(folder[0], folder[2])
print 'Getting destination files...'
dest = get_files(folder[1])
print 'Finding diff files...'
diff = list(set(src) - set(dest))
diff_rem = list(set(dest) - set(src))
print 'Found {} files to copy and {} files to remove'.format(len(diff), len(diff_rem))
# change current dir to source for copying
os.chdir(folder[0])
for f in diff:
print 'Copying {}...'.format(f)
df = os.path.join(folder[1], f)
dd = os.path.dirname(df)
if not os.path.exists(dd):
os.makedirs(dd)
shutil.copy2(f, df)
# change current dir to destination for deletions
os.chdir(folder[1])
for f in diff_rem:
print 'Deleting {}...'.format(f)
os.unlink(f)
print 'Completed'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment