-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathabs.py
97 lines (83 loc) · 3.38 KB
/
abs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
try:
from colorama import Fore,Back
except:
print('Please download requirements file , command : pip3 install -r requirements.txt')
exit()
import time
import os
from distutils.dir_util import copy_tree
import shutil
import pathlib
import argparse
try:
args = argparse.ArgumentParser(description="ABS Project minimal backup service")
args.add_argument('-p','--path',required=False,default=".",help="Name of the folder to be backed up")
args.add_argument('-o','--output',required=False,default=f'../backup',help="Path of output directory")
args.add_argument('-l','--log-file',required=False,default="backup.log",help="Name of log files")
args.add_argument('-t','--time',required=False,default=30,type=int,help="Backup time interval")
arguments = args.parse_args()
def folder_check(val):
val = str(val)
if not val.endswith('/'):
return val+'/'
def get_file_size(file_path):
size = os.path.getsize(file_path)
return str(round(size / 1024, 3)) +" kb"
def file_or_folder(mainPath):
files = []
folders = []
for path in pathlib.Path(mainPath).iterdir():
if path.is_file():
if str(path).startswith("."):continue
files.append(path)
else:
if str(path).startswith("."):continue
folders.append(path)
return {"files":files,"folders":folders}
def write_log(contents):
context = "+"+"-"*50+"+"
title1,title2 = "Files And Folders","Size"
context += f"\n|{title1}"+" "*(35-len(title1))+"|"+title2+" "*(14-len(title2))+"|"
context += "\n+"+"-"*50+"+"
for text in contents:
context += f"\n|{text[0]}"+" "*(35-len(text[0]))+"|"+text[1]+" "*(14-len(text[1]))+"|"
context += "\n+"+"-"*50+"+"
with open(arguments.log_file,'a') as f:
f.write('All files copied !!!')
f.write(context+"\n\n")
f.close()
def log(contents):
context = "+"+"-"*50+"+"
title1,title2 = "Files And Folders","Size"
context += f"\n|{Fore.RED+title1+Fore.RESET}"+" "*(35-len(title1))+"|"+Fore.RED+title2+Fore.RESET+" "*(14-len(title2))+"|"
context += "\n+"+"-"*50+"+"
for text in contents:
context += f"\n|{Fore.GREEN+text[0]+Fore.RESET}"+" "*(35-len(text[0]))+"|"+Fore.YELLOW+text[1]+Fore.RESET+" "*(14-len(text[1]))+"|"
context += "\n+"+"-"*50+"+"
return context
def backup():
files = file_or_folder(arguments.path)['files']
logInfos = []
folders = file_or_folder(arguments.path)['folders']
for folder in folders:
shutil.copytree(folder,folder_check(arguments.output)+str(folder),copy_function=shutil.copy2)
for file in files:
shutil.copy(file,folder_check(arguments.output))
for i in files:
kb = get_file_size(i)
logInfos.append((str(i),kb))
for i in folders:
kb = get_file_size(i)
folder = str(i)+" /"
logInfos.append((folder,kb))
print(Fore.BLUE+'\nAll files copied !!!'+Fore.RESET)
print(log(logInfos))
write_log(logInfos)
while 1:
try:shutil.rmtree(arguments.output)
except:pass
backup()
time.sleep(arguments.time)
except KeyboardInterrupt :
print("\nBackup Service Closing")
exit()