-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
231 lines (195 loc) · 7.43 KB
/
main.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import os
import time
import tkinter
from datetime import datetime
from tkinter import *
from crontab import CronTab
import keyring
class backup_info:
def __init__(self):
self.computer_name=None
self.backup_location=None
self.partition=None
self.always_on=False
self.email = None
self.user = None
def setup_cron(self):
print("creating Cron Job")
cron = CronTab('root')
# cron = CronTab(user = 'root')
directory = os.getcwd()
command_name = 'python3 '+directory+'/main.py'
##check if cron is set
exist_cron = cron.find_command(command_name)
for cr in cron.crons:
if(cr.command == command_name):
print("cron exist already exiting program")
exit(0)
job = cron.new(command=command_name, comment='auto disk image creator job')
job.hour.every(2)
job.enable()
cron.write()
def loadConfig(self):
if (os.path.exists('config.txt')):
f = open('config.txt', 'r')
ls = f.readlines()
self.computer_name = ls[0].split("computer_name: ")[1].replace("\n", '')
self.backup_location = ls[1].split("backup_location: ")[1].replace("\n", '')
always_on_txt = ls[2].split("always_on: ")[1].replace("\n", '')
if(always_on_txt.lower() == 'y'):
self.always_on = True
else:
self.always_on = False
self.partition = ls[3].split("partition: ")[1].replace("\n", '')
self.email = ls[4].split("email: ")[1].replace("\n", '')
self.user = ls[5].split("user: ")[1].replace("\n", '')
else:
self.setConfigFile()
def setConfigFile(self):
computer_name = input("What is this computer's name? \n")
backup_location = input("Where do you wan't to store your backups? \n ex: /mnt/data/ \n")
while (os.path.exists(backup_location) == False):
print("back up location was invalid.")
backup_location = input("Where do you wan't to store your backups? \n ex: /mnt/data/ \n")
always_on_txt = input("Always on mode (y/N):")
if (always_on_txt.lower() == 'y' or always_on_txt.lower() == 'n'):
con_while = False
else:
con_while = True
while(con_while):
print("incorrect choice. Please try again entering 'y' or 'n' or press enter for 'n'")
always_on_txt = input("Always on mode (y/N):")
if(always_on_txt.lower() == 'y' or always_on_txt.lower() == 'n'):
con_while=False
else:
con_while = True
print("Warning! partition needs to be exact. If you are not sure quit here and check before continuing \n")
print("Please write the partion or disk you wish to copy. \n")
partition = input("ex: /dev/sda \n")
print("Please enter your gmail password:\n")
print("Note passwords are stored on the system keychain and not by this program.\n")
pw = input("enter password: ")
keyring.set_password('gmail_dd_email_pw',os.getlogin(), pw)
email =input("Enter gmail: \n")
user =input("Enter user: \n")
f = open("config.txt", "w")
f.write('computer_name: ' + computer_name)
f.write('\n')
f.write('backup_location: ' + backup_location)
f.write('\n')
f.write('always_on: ' + always_on_txt)
f.write('\n')
f.write('partition: ' + partition)
f.write('\n')
f.write('email: ' + email)
f.write('\n')
f.write('user: ' + user)
f.write('\n')
f.close()
self.setup_cron()
gui_continue = False
lvtext = None
def continue_gui():
global gui_continue
global lvtext
gui_continue = True
timething = True
count = 0
while (timething):
count += 10
# if(gui_continue):
# break
lvtext.set("Backup Started: " + str(count))
time.sleep(1)
def gui_warning():
global lvtext
base_label = "Start backup warning do not disconnect"
window = Tk()
btn = Button(window, text=base_label, fg='blue',command=continue_gui)
btn.place(x=80, y=100)
lvtext = tkinter.StringVar()
lvtext.set("Start Backup")
lbl = Label(window, textvariable=lvtext, fg='red', font=("Helvetica", 16))
lbl.place(x=60, y=50)
lbl.pack()
btn.pack()
window.title('Backup warning')
window.geometry("300x200+10+10")
window.mainloop()
def send_email(bi: backup_info):
import smtplib, ssl
port = 465
pw = keyring.get_password('gmail_dd_email_pw',bi.user)
context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
server.login(bi.email, pw)
sender_email = bi.email
receiver_email = bi.email
message = " Subject: backup completed \nnotification that dd image was created for computer '"+bi.computer_name+"' \nThis message is sent from Python."
server.sendmail(sender_email, receiver_email, message)
def start_backup(bi: backup_info):
backup_file_base= 'backup_image'
backup_base = bi.backup_location + bi.computer_name + "/"
backup_file_init = backup_base + backup_file_base+'.img.gz'
backup_file_monthly= backup_base + backup_file_base+'_monthly.img.gz'
backup_file_command = backup_file_init
if(os.path.exists(backup_file_init)):
backup_file_command = backup_file_monthly
if(os.path.exists(backup_file_monthly)):
os.remove(backup_file_monthly)
backup_command = 'sudo dd if='+bi.partition+' conv=sync,noerror bs=64K status=progress | gzip -c > ' + backup_file_command
x_thread= None
print("starting Backup do not shut off or disconnect the internet")
if(bi.always_on==False):
import threading
x_thread = threading.Thread(target=gui_warning, daemon=True)
x_thread.start()
if(bi.always_on ==False):
while(gui_continue == False):
time.sleep(1)
os.system(backup_command)
else:
os.system(backup_command)
f = open(backup_base + "backup_info.txt", "w")
now = datetime.now()
year = now.strftime("%Y")
f.write("year: " + year)
f.write('\n')
month = now.strftime("%m")
f.write("month: " + month)
f.write('\n')
day = now.strftime("%d")
f.write("day: " + day)
f.write('\n')
f.close()
print("Backup finished")
send_email(bi)
def compare_dates(bi: backup_info):
backup_base = bi.backup_location + bi.computer_name + "/"
f = open(backup_base+'backup_info.txt', 'r')
ls = f.readlines()
yr = ls[0].split("year: ")[1].replace("\n", '')
month = ls[1].split("month: ")[1].replace("\n", '')
day = ls[2].split("day: ")[1].replace("\n", '')
datetime_object = datetime.strptime(month+'/'+day+'/'+yr, '%m/%d/%Y')
timediff = datetime_object - datetime.now()
if(abs(timediff.days) >30):
start_backup(bi)
def check_for_backup(bi: backup_info):
backup_base = bi.backup_location+bi.computer_name+"/"
if(os.path.exists(backup_base)):
compare_dates(bi)
else:
os.mkdir(backup_base)
start_backup(bi)
def get_info():
bi = backup_info()
bi.loadConfig()
if(os.path.exists(os.getcwd()+'/force.txt')):
os.remove(os.getcwd() + '/force.txt')
start_backup(bi)
else:
check_for_backup(bi)
# send_email(bi)
if __name__ == "__main__":
get_info()