Last active
August 29, 2020 07:25
-
-
Save mshiyaf/541465b17032c20bdd70fdcb7e76653a to your computer and use it in GitHub Desktop.
Python script to convert folder to tar.gz for backing up with loading and progress bar in terminal.
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
#!/Users/mshiyaf/.pyenv/shims/python | |
import os | |
import tarfile | |
import time | |
import sys | |
def waiting(lenstr=20, zzz=0.5, dispstr='Backing up your files'): | |
dots = '.' * lenstr | |
spaces = ' ' * lenstr | |
print(dispstr.center(50, '*')) | |
while True: | |
for i in range(lenstr): | |
time.sleep(zzz) | |
outstr = dots[:i] + spaces[i:] | |
sys.stdout.write('\b' * lenstr + outstr) | |
sys.stdout.flush() | |
for i in range(lenstr, 0, -1): | |
time.sleep(zzz) | |
outstr = dots[:i] + spaces[i:] | |
sys.stdout.write('\b' * lenstr + outstr) | |
sys.stdout.flush() | |
def update_progress(progress): | |
barLength = 10 # Modify this to change the length of the progress bar | |
status = "" | |
if isinstance(progress, int): | |
progress = float(progress) | |
if not isinstance(progress, float): | |
progress = 0 | |
status = "error: progress var must be float\r\n" | |
if progress < 0: | |
progress = 0 | |
status = "Halt...\r\n" | |
if progress >= 1: | |
progress = 1 | |
status = "Done...\r\n" | |
block = int(round(barLength*progress)) | |
text = "\rPercent: [{0}] {1}% {2}".format( "#"*block + "-"*(barLength-block), progress*100, status) | |
sys.stdout.write(text) | |
sys.stdout.flush() | |
try: | |
def create_new_tar(path,tar_name): | |
log = open('/Users/mshiyaf/Code/backup.log','w') | |
progress = 0 | |
file_name_array = [] | |
for root, dirs, files in os.walk(path): | |
for file in files: | |
_node_modules = 'node_modules' | |
_vendor = 'vendor' | |
_next = '.next' | |
_git = '.git' | |
if (_node_modules not in root and _vendor not in root and _next not in root and _git not in root): | |
file_name_array.append(os.path.join(root,file)) | |
progress_length = len(file_name_array) | |
with tarfile.open(tar_name,'w:gz') as tar_handle: | |
for root, dirs, files in os.walk(path): | |
for file in files: | |
_node_modules = 'node_modules' | |
_vendor = 'vendor' | |
_next = '.next' | |
_git = '.git' | |
if (_node_modules not in root and _vendor not in root and _next not in root and _git not in root): | |
time_of_mod = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(os.path.getmtime(os.path.join(root,file)))) | |
time_now = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) | |
print(root+'------'+file+'-------'+time_now+'------'+time_of_mod,file=log) | |
path = root | |
sep = path.split('/')[3] + '/' | |
path_to_back_up = path[path.find(sep):] +'/'+file | |
if(path[path.find(sep):] == 'l' or path[path.find(sep):] == 's'): | |
path_to_back_up = sep+file | |
tar_handle.add(os.path.join(root, file),arcname=path_to_back_up) | |
progress = progress + 1 | |
update_progress(progress/progress_length) | |
print(path.split('/')[3] + ' backup successful !') | |
log.close() | |
except Exception as e: | |
error_log = open('/Users/mshiyaf/Code/error.log','w') | |
print(e,file=error_log) | |
print(e) | |
error_log.close() | |
if __name__ == '__main__': | |
import _thread as thread | |
thread.start_new_thread(waiting, (20, 0.5)) | |
create_new_tar('/Users/mshiyaf/Texol','/Users/mshiyaf/Dropbox/Back Up/Texol_BackUp_'+time.strftime('%d_%m_%Y',time.localtime(time.time()))+'.tgz') | |
create_new_tar('/Users/mshiyaf/Sites','/Users/mshiyaf/Dropbox/Back Up/Sites_Backup_'+time.strftime('%d_%m_%Y',time.localtime(time.time()))+'.tgz') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment