Created
September 30, 2019 12:41
-
-
Save Tran-Antoine/946483d44ceb2198855ad26b859832c6 to your computer and use it in GitHub Desktop.
Automates backups by converting folders to zip archives repeatedly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import zipfile | |
import os, sys | |
import time, threading | |
class BackUpLoop(threading.Thread): | |
def __init__(self, dirName, repeatTime): | |
threading.Thread.__init__(self) | |
self.dirName = dirName | |
self.repeatTime = repeatTime | |
self._last_turn = False | |
def write(zip, root, name): | |
zip.write(os.path.join(root, name), compress_type=zipfile.ZIP_DEFLATED) | |
def run(self): | |
while not self._last_turn: | |
backup = zipfile.ZipFile(f'{self.dirName}.zip', 'w') | |
for root, dirs, files in os.walk(self.dirName + '/'): | |
for file in files: | |
BackUpLoop.write(backup, root, file) | |
for dir in dirs: | |
BackUpLoop.write(backup, root, dir) | |
backup.close() | |
print(f"Successfully backed {self.dirName} up") | |
try: | |
time.sleep(self.repeatTime) | |
except ValueError: | |
break | |
print("Back up loop successfully stopped") | |
def stop_loop(self): | |
self._last_turn = True | |
folder = sys.argv[1] | |
repeatTime = -1 | |
try: | |
repeatTime = float(sys.argv[2]) | |
except IndexError: | |
pass | |
loop = BackUpLoop(folder, repeatTime) | |
loop.start() | |
print("Backup loop started (press enter to stop)") | |
input() | |
print("Stopping program...") | |
loop.stop_loop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment