37 lines
1.1 KiB
Python
Executable file
37 lines
1.1 KiB
Python
Executable file
#! /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')
|