-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaxSync.py
executable file
·115 lines (90 loc) · 4.81 KB
/
MaxSync.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
#!/usr/bin/python
# -*- coding: iso-8859-1 -*-
SourceDirPath = "/data/data1"
BackupDirPath = "/media"
# Don't need to change anything below this line
import Tkinter, tkFileDialog, tkMessageBox
import subprocess
import os
class simpleapp_tk(Tkinter.Tk):
def __init__(self,parent):
Tkinter.Tk.__init__(self,parent)
self.parent = parent
self.initialize()
def initialize(self):
self.grid()
# We only need the Source and Destination directory
self.varSource = Tkinter.StringVar()
self.varSource.set("SourceDir")
self.varDestination = Tkinter.StringVar()
self.varDestination.set("DestinationDir")
# Setup buttons on the left for doing the actions
button = Tkinter.Button(self, text=u"1: Select Source Directory!",
command=self.askdirectorySource, width=25)
button.grid(column=0, row=0, sticky='EW')
button = Tkinter.Button(self, text=u"2: Select Destination Directory!",
command=self.askdirectoryDestination, width=25)
button.grid(column=0, row=1, sticky='EW')
button = Tkinter.Button(self, text=u"3: Backup your data!",
command=self.backupdata, width=25)
button.grid(column=0, row=2, sticky='EW')
# Labels with the names of the directories
SourceDir = Tkinter.Label(self,textvariable=self.varSource,
anchor="w")
SourceDir.grid(column=1,row=0,sticky='EW')
DestinationDir = Tkinter.Label(self,textvariable=self.varDestination,
anchor="w")
DestinationDir.grid(column=1,row=1,sticky='EW')
# trying to also make a text box with output
self.var_textbox = Tkinter.Text(self, height=10)
self.var_textbox.grid(column=0, row=3, columnspan=2)
def askdirectorySource(self):
"""Returns a selected directory name."""
if self.varSource.get() == "SourceDir" :
self.varSource.set(SourceDirPath)
self.chosen_dir = tkFileDialog.askdirectory(initialdir=self.varSource.get())
self.varSource.set(self.chosen_dir)
self.var_textbox.insert(Tkinter.END, "\n"+self.chosen_dir+" selected as source directory\n")
def askdirectoryDestination(self):
"""Returns a selected directory name."""
if self.varDestination.get() == "DestinationDir" :
self.varDestination.set(BackupDirPath)
self.chosen_dir = tkFileDialog.askdirectory(initialdir=self.varDestination.get())
self.varDestination.set(self.chosen_dir+"/")
self.var_textbox.insert(Tkinter.END, "\n"+self.chosen_dir+" selected as backup directory\n")
def backupdata(self):
print "You requested the backup routine"
self.option_add('*font', 'Helvetica -14')
self.option_add("*Dialog.msg.wrapLength", "10i")
# Check to see if the directories were set for something else
if self.varSource.get() == "SourceDir" :
tkMessageBox.showwarning("Warning!", "First set the source directory!")
elif self.varDestination.get() == "DestinationDir" :
tkMessageBox.showwarning("Warning!", "Also remember to set the destination directory!")
# OK good to go
else:
result = tkMessageBox.askyesno("Ready to backup!!",
"You will now synchronize your folder\n\n%s\n\ninto your backup folder:\n\n%s"
%(os.path.basename(self.varSource.get()),self.varDestination.get()))
if result :
print "You have accepted to backup"
self.DiskUsage = Tkinter.StringVar()
DiskUsage = os.popen('du -hs "%s"'%(self.varSource.get())).read()
tkMessageBox.showinfo("First notification",
"Your job size is :\n"
+DiskUsage+
"\n this could take a while, please be patient.")
var_rsync_command = subprocess.Popen(["rsync", "-ahv", self.varSource.get(), self.varDestination.get()], stdout=subprocess.PIPE)
# Trying to make the textbox update all the time
for line in iter(var_rsync_command.stdout.readline,''):
self.var_textbox.insert(Tkinter.END, line)
self.var_textbox.update()
self.var_textbox.yview(Tkinter.END)
#
tkMessageBox.showinfo("Backup complete","The rsync job has finished")
else:
print "You declined to backup your data"
if __name__ == "__main__":
app = simpleapp_tk(None)
app.title('MaxSync - Backup your data')
app.mainloop()