Skip to content

Instantly share code, notes, and snippets.

@atvKumar
Last active December 12, 2015 13:02
Show Gist options
  • Save atvKumar/c90e790ad60156cc0627 to your computer and use it in GitHub Desktop.
Save atvKumar/c90e790ad60156cc0627 to your computer and use it in GitHub Desktop.
Backup Zip
import os
import copy
import subprocess as sp
zip_cmd = ['zip', '-r', '-9', '-T']
lwza_cmd = ['7za', 'a', '-t7z', '-mx9', '-v2g']
def loadExcludedListFromPath(path):
excludedList = list()
for afile in os.listdir(path):
if not os.path.isdir(afile) and not afile.startswith(".") and ("zip" in afile or "7z" in afile):
baseFileName = None
if afile.count(".") == 2:
baseFileName = os.path.splitext(os.path.splitext(afile)[0])[0]
elif afile.count(".") == 1:
baseFileName = os.path.splitext(afile)[0]
if baseFileName is not None:
excludedList.append(baseFileName)
return excludedList
def backupzip(path, destination=None, zip_type=zip_cmd):
exclude_list = loadExcludedListFromPath(destination)
if '~' in path:
path = os.path.expanduser(path)
if destination is None:
destination = path
for directory in os.listdir(path):
full_path = os.path.join(path, directory)
if os.path.isdir(full_path) and not directory.startswith('.') and directory not in exclude_list:
cmd = copy.deepcopy(zip_type)
if zip_type == lwza_cmd:
cmd.append(os.path.join(destination, directory+'.7z'))
else:
cmd.append(os.path.join(destination, directory+'.zip'))
cmd.append(full_path)
sp.call(cmd)
print ' '.join(cmd)
del cmd
if __name__ == '__main__':
import sys
backupzip(sys.argv[1], sys.argv[2], eval(sys.argv[3]))
#Example:
# python zipbackup /Volumes/Source /Volumes/Destination zip_cmd
# python zipbackup /Volumes/Source /Volumes/Destination lwza_cmd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment