| #!/usr/bin/env python |
| # File: backitup.py |
| # Author: shadytyrant@gmail.com |
| # Date: 2009-11-19 |
| # Notes: A gui version of my linux backup script written with wxPython |
| #-------------------------------------------- |
| |
| import wx, os, time |
| |
| class MyFrame(wx.Frame): |
| def __init__(self, *args, **kwds): |
| |
| # Create widgets, layout, and propertys. Bind events to handlers |
| |
| kwds["style"] = wx.DEFAULT_FRAME_STYLE |
| wx.Frame.__init__(self, *args, **kwds) |
| |
| self.label_1 = wx.StaticText(self, -1, "File One ") |
| self.txtFilePathOne = wx.TextCtrl(self, -1, "") |
| self.btnAddFileOne = wx.Button(self, -1, "Add File") |
| |
| self.label_2 = wx.StaticText(self, -1, "File Two ") |
| self.txtFilePathTwo = wx.TextCtrl(self, -1, "") |
| self.btnAddFileTwo = wx.Button(self, -1, "Add File") |
| |
| self.label_3 = wx.StaticText(self, -1, "File Three ") |
| self.txtFilePathThree = wx.TextCtrl(self, -1, "") |
| self.btnAddFileThree = wx.Button(self, -1, "Add File") |
| |
| self.label_4 = wx.StaticText(self, -1, "File Four ") |
| self.txtFilePathFour = wx.TextCtrl(self, -1, "") |
| self.btnAddFileFour = wx.Button(self, -1, "Add File") |
| |
| self.label_5 = wx.StaticText(self, -1, "Backup Folder") |
| self.txtBackupFilePath = wx.TextCtrl(self, -1, "") |
| self.btnSelectBackupFolder = wx.Button(self, -1, "Select Folder") |
| |
| self.btnRunBackup = wx.Button(self, -1, "Run Backup") |
| |
| self.__set_properties() |
| self.__do_layout() |
| |
| self.Bind(wx.EVT_BUTTON, self.OnAddFileOneClick, self.btnAddFileOne) |
| self.Bind(wx.EVT_BUTTON, self.OnAddFileTwoClick, self.btnAddFileTwo) |
| self.Bind(wx.EVT_BUTTON, self.OnAddFileThreeClick, self.btnAddFileThree) |
| self.Bind(wx.EVT_BUTTON, self.OnAddFileFourClick, self.btnAddFileFour) |
| self.Bind(wx.EVT_BUTTON, self.OnSelectFolderClick, self.btnSelectBackupFolder) |
| self.Bind(wx.EVT_BUTTON, self.OnRunBackupClick, self.btnRunBackup) |
| |
| def __set_properties(self): |
| # Create properties |
| self.SetTitle("Back It Up") |
| |
| |
| def __do_layout(self): |
| # Create layout |
| sizer_1 = wx.BoxSizer(wx.VERTICAL) |
| sizer_7 = wx.BoxSizer(wx.HORIZONTAL) |
| sizer_4 = wx.BoxSizer(wx.HORIZONTAL) |
| sizer_3 = wx.BoxSizer(wx.HORIZONTAL) |
| sizer_2 = wx.BoxSizer(wx.HORIZONTAL) |
| sizer_6 = wx.BoxSizer(wx.HORIZONTAL) |
| |
| sizer_6.Add(self.label_1, 0, wx.ALL, 3) |
| sizer_6.Add((30, 30), 0, wx.ADJUST_MINSIZE, 0) |
| sizer_6.Add(self.txtFilePathOne, 0, wx.ALL, 3) |
| sizer_6.Add((30, 30), 0, wx.ADJUST_MINSIZE, 0) |
| sizer_6.Add(self.btnAddFileOne, 0, wx.ALL, 3) |
| |
| sizer_1.Add(sizer_6, 1, wx.EXPAND, 0) |
| |
| sizer_2.Add(self.label_2, 0, wx.ALL, 3) |
| sizer_2.Add((30, 30), 0, wx.ADJUST_MINSIZE, 0) |
| sizer_2.Add(self.txtFilePathTwo, 0, wx.ALL, 3) |
| sizer_2.Add((30, 30), 0, wx.ADJUST_MINSIZE, 0) |
| sizer_2.Add(self.btnAddFileTwo, 0, wx.ALL, 3) |
| |
| sizer_1.Add(sizer_2, 1, wx.EXPAND, 0) |
| |
| sizer_3.Add(self.label_3, 0, wx.ALL, 3) |
| sizer_3.Add((30, 30), 0, wx.ADJUST_MINSIZE, 0) |
| sizer_3.Add(self.txtFilePathThree, 0, wx.ALL, 3) |
| sizer_3.Add((30, 30), 0, wx.ADJUST_MINSIZE, 0) |
| sizer_3.Add(self.btnAddFileThree, 0, wx.ALL, 3) |
| |
| sizer_1.Add(sizer_3, 1, wx.EXPAND, 0) |
| |
| sizer_4.Add(self.label_4, 0, wx.ALL, 3) |
| sizer_4.Add((30, 30), 0, wx.ADJUST_MINSIZE, 0) |
| sizer_4.Add(self.txtFilePathFour, 0, wx.ALL, 3) |
| sizer_4.Add((30, 30), 0, wx.ADJUST_MINSIZE, 0) |
| sizer_4.Add(self.btnAddFileFour, 0, wx.ALL, 3) |
| |
| sizer_1.Add(sizer_4, 1, wx.EXPAND, 0) |
| |
| sizer_7.Add(self.label_5, 0, wx.ALL, 3) |
| sizer_7.Add((30, 30), 0, wx.ADJUST_MINSIZE, 0) |
| sizer_7.Add(self.txtBackupFilePath, 0, wx.ALL, 3) |
| sizer_7.Add((30, 30), 0, wx.ADJUST_MINSIZE, 0) |
| sizer_7.Add(self.btnSelectBackupFolder, 0, wx.ALL, 3) |
| |
| sizer_1.Add(sizer_7, 1, wx.EXPAND, 0) |
| sizer_1.Add(self.btnRunBackup, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, 3) |
| |
| self.SetSizer(sizer_1) |
| sizer_1.Fit(self) |
| |
| self.Layout() |
| self.Centre() |
| |
| #------------------------------------------------------------------------- |
| # Event Handlers |
| |
| def OnAddFileOneClick(self, event): |
| # Open file dialog and set path string in text box |
| dlg = wx.FileDialog(self, message="Open a file...", defaultDir=os.getcwd(), defaultFile="", style=wx.OPEN) |
| wildcard = "All files (*.*)|*.*" |
| dlg.SetWildcard(wildcard) |
| |
| if dlg.ShowModal() == wx.ID_OK: |
| filename = dlg.GetPath() |
| self.txtFilePathOne.SetValue(filename) |
| dlg.Destroy() |
| |
| def OnAddFileTwoClick(self, event): |
| # Open file dialog and set path string in text box |
| dlg = wx.FileDialog(self, message="Open a file...", defaultDir=os.getcwd(), defaultFile="", style=wx.OPEN) |
| wildcard = "All files (*.*)|*.*" |
| dlg.SetWildcard(wildcard) |
| |
| if dlg.ShowModal() == wx.ID_OK: |
| filename = dlg.GetPath() |
| self.txtFilePathTwo.SetValue(filename) |
| dlg.Destroy() |
| |
| def OnAddFileThreeClick(self, event): |
| # Open file dialog and set path string in text box |
| dlg = wx.FileDialog(self, message="Open a file...", defaultDir=os.getcwd(), defaultFile="", style=wx.OPEN) |
| wildcard = "All files (*.*)|*.*" |
| dlg.SetWildcard(wildcard) |
| |
| if dlg.ShowModal() == wx.ID_OK: |
| filename = dlg.GetPath() |
| self.txtFilePathThree.SetValue(filename) |
| dlg.Destroy() |
| |
| def OnAddFileFourClick(self, event): |
| # Open file dialog and set path string in text box |
| dlg = wx.FileDialog(self, message="Open a file...", defaultDir=os.getcwd(), defaultFile="", style=wx.OPEN) |
| wildcard = "All files (*.*)|*.*" |
| dlg.SetWildcard(wildcard) |
| |
| if dlg.ShowModal() == wx.ID_OK: |
| filename = dlg.GetPath() |
| self.txtFilePathFour.SetValue(filename) |
| dlg.Destroy() |
| |
| def OnSelectFolderClick(self, event): |
| # Open file dialog and set path string in text box |
| dlg = wx.FileDialog(self, message="Open a file...", defaultDir=os.getcwd(), defaultFile="", style=wx.OPEN) |
| wildcard = "All files (*.*)|*.*" |
| dlg.SetWildcard(wildcard) |
| |
| if dlg.ShowModal() == wx.ID_OK: |
| filename = dlg.GetPath() |
| self.txtBackupFilePath.SetValue(filename) |
| dlg.Destroy() |
| |
| def OnRunBackupClick(self, event): |
| # Check for user errors and run backup |
| source = [self.txtFilePathOne.GetValue(), self.txtFilePathTwo.GetValue(), |
| self.txtFilePathThree.GetValue(), self.txtFilePathFour.GetValue()] |
| |
| target_dir = self.txtBackupFilePath.GetValue() |
| |
| if len(target_dir) == 0: |
| messageBox = wx.MessageBox('Please Enter Backup File Path', 'Error', wx.OK | wx.ICON_ERROR) |
| app.MainLoop() |
| |
| target = target_dir + time.strftime('%Y_%m_%d') + '.tar.gz' |
| |
| for i in source: |
| backup = 'tar -cvzf %s %s' % (target, ' '.join(source[:-1])) |
| |
| if os.system(backup) == 0: |
| messageBox = wx.MessageBox('Backup Was Successful', 'Error', wx.OK | wx.ICON_EXCLAMATION) |
| else: |
| messageBox = wx.MessageBox('ERROR: Backup Failed', 'Error', wx.OK | wx.ICON_ERROR) |
| |
| if __name__ == "__main__": |
| # Create app and frame objects and send into main loop |
| app = wx.PySimpleApp(0) |
| wx.InitAllImageHandlers() |
| MainFrame = MyFrame(None, -1, "") |
| app.SetTopWindow(MainFrame) |
| MainFrame.Show() |
| app.MainLoop() |