From e8375cfc65bf6fe0f6233a6c0e463a8a23d27087 Mon Sep 17 00:00:00 2001 From: gabe venberg Date: Mon, 1 Mar 2021 16:48:37 -0600 Subject: [PATCH] zipfile program --- backupToZip.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100755 backupToZip.py diff --git a/backupToZip.py b/backupToZip.py new file mode 100755 index 0000000..7862571 --- /dev/null +++ b/backupToZip.py @@ -0,0 +1,36 @@ +#! /usr/bin/env python3 +usage = "backupToZip: usage backupToZip [directory] backup given directory to a zip file, incrementing filename if name already exists" + +import zipfile, os, sys + +if len(sys.argv)<1: + print(usage) + sys.exit() + +dir = os.path.realpath(sys.argv[1]) + +#figure out filename based on what already exists. +number = 1 +while True: + zipFilename = os.path.basename(dir)+'-'+str(number)+'.zip' + if not os.path.exists(zipFilename): + break + number += 1 + +print(F'Creating {zipFilename}') +backupZip = zipfile.ZipFile(zipFilename, 'w') + +for dirName, subDirList, fileList in os.walk(dir): + #print(f'Adding files in {dirName}') + backupZip.write(dirName) + + for fileName in fileList: + #lets not backup other backup files. + newBase = os.path.basename(dir)+'-' + if fileName.startswith(newBase) and fileName.endswith('.zip'): + print(f'skipping {fileName}, is backup file.') + continue + print(f'adding {fileName}') + backupZip.write(os.path.join(dirName, fileName)) +backupZip.close() +print('done')