add ConfigurationBackup
[enigma2.git] / lib / python / Plugins / SystemPlugins / ConfigurationBackup / plugin.py
1 from enigma import *
2 from Screens.Screen import Screen
3 from Screens.MessageBox import MessageBox
4 from Screens.Console import Console
5 from Components.ActionMap import ActionMap, NumberActionMap
6 from Components.Pixmap import *
7 from Components.Pixmap import Pixmap
8 from Components.Label import Label
9 from Components.MenuList import MenuList
10 from Components.config import config, configSelection, configSelection, getConfigListEntry, configElement, ConfigSubsection, currentConfigSelectionElement
11 from Components.ConfigList import ConfigList
12 from Plugins.Plugin import PluginDescriptor
13
14 from Tools.NumericalTextInput import *
15 from Tools.Directories import *
16 import os
17 import string
18 import time
19 import datetime
20
21 plugin_path = ""
22
23 BackupPath = {
24                 "hdd" : "/media/hdd/backup",
25                 "usb" : "/media/usb/backup",
26                 "cf" : "/media/cf/backup"
27         }
28
29 class BackupSetup(Screen):
30         skin = """
31                 <screen position="100,100" size="400,400" title="Backup and Restore" >
32                         <widget name="config" position="5,10" size="380,350" />
33                         <widget name="ok" position="10,365" size="53,30" pixmap="~/green.png" />
34                         <widget name="oktext" position="10,365" size="53,30" valign="center" halign="center" zPosition="2" font="Regular;20" transparent="1"  foregroundColor="black" />
35                         <widget name="cancel" position="73,365" size="100,30" pixmap="~/red.png" />
36                         <widget name="canceltext" position="73,365" size="100,30" valign="center" halign="center" zPosition="2" font="Regular;20" transparent="1" foregroundColor="black" />
37                         <widget name="restore" position="183,365" size="100,30" pixmap="~/yellow.png" />
38                         <widget name="restoretext" position="183,365" size="100,30" valign="center" halign="center" zPosition="2" font="Regular;20" transparent="1"  foregroundColor="black" />
39                         <widget name="backup" position="293,365" size="100,30" pixmap="~/blue.png" />
40                         <widget name="backuptext" position="293,365" size="100,30" valign="center" halign="center" zPosition="2" font="Regular;20" transparent="1"  foregroundColor="black" />
41                 </screen>"""
42                 
43         def keyLeft(self):
44                 self["config"].handleKey(config.key["prevElement"])
45
46         def keyRight(self):
47                 self["config"].handleKey(config.key["nextElement"])
48
49         def keyNumberGlobal(self, number):
50                 print "You pressed number " + str(number)
51                 if (self["config"].getCurrent()[1].parent.enabled == True):
52                         self["config"].handleKey(config.key[str(number)])
53
54         def keyCancel(self):
55                 for x in self["config"].list:
56                         x[1].cancel()
57                 self.close()
58                 
59         def keySave(self):
60                 for x in self["config"].list:
61                         x[1].save()
62                 self.close()
63         
64         def __init__(self, session, args = None):
65                 Screen.__init__(self, session)
66                 self.skin_path = plugin_path
67                 
68                 self["oktext"] = Label(_("OK"))
69                 self["canceltext"] = Label(_("Cancel"))
70                 self["backuptext"] = Label(_("Backup"))
71                 self["restoretext"] = Label(_("Restore"))
72                 self["restore"] = Pixmap()
73                 self["backup"] = Pixmap()
74                 self["ok"] = Pixmap()
75                 self["cancel"] = Pixmap()
76
77                 self.path = ""
78                 self.list = []
79                 self["config"] = ConfigList(self.list)
80                 self.createSetup()
81
82                 self["actions"] = NumberActionMap(["SetupActions"],
83                 {
84                         "ok": self.keySave,
85                         "cancel": self.keyCancel,
86                         "left": self.keyLeft,
87                         "right": self.keyRight
88                 }, -1)
89                 
90                 self["shortcuts"] = ActionMap(["ShortcutActions"],
91                 {
92                         "red": self.keyCancel,
93                         "green": self.keySave,
94                         "blue": self.Backup,
95                         "yellow": self.Restore,
96                 })
97                 
98
99         def createSetup(self):
100                 print "Creating BackupSetup"
101                 self.list = [ ]
102                 self["config"] = ConfigList(self.list)
103                 config.backup = ConfigSubsection()
104                 config.backup.type = configElement("config.backup.type", configSelection, 0, (("full", _("full /etc directory")), ("settings", _("only /etc/enigma2 directory")), ("var", _("/var directory"))))
105                 config.backup.location = configElement("config.backup.location", configSelection, 0, (("usb", _("USB Stick")), ("cf", _("CF Drive")), ("hdd", _("Harddisk"))))
106                 self.list.append(getConfigListEntry(_("Backup Mode"), config.backup.type))
107                 self.list.append(getConfigListEntry(_("Backup Location"), config.backup.location))
108                 
109
110         def createBackupfolders(self):
111                 self.path = BackupPath[str(currentConfigSelectionElement(config.backup.location))]
112                 print "BackupPath: ", str(self.path)
113                 print "Creating Backup Folder if not already there..."
114                 cmd = "[ ! -e " + self.path + " ] && mkdir " + self.path
115                 print "Commandstring: " + str(cmd)
116                 os.system(cmd)
117
118         def Backup(self):
119                 print "this will start the backup now!"
120                 self.session.openWithCallback(self.runBackup, MessageBox, _("Do you want to backup now?\nAfter pressing OK, please wait!"))     
121
122         def Restore(self):
123                 print "this will start the restore now!"
124                 self.session.open(RestoreMenu)
125
126         def runBackup(self, result):
127                 if result:
128                         self.createBackupfolders()
129                         d = time.localtime()
130                         dt = datetime.date(d.tm_year, d.tm_mon, d.tm_mday)
131                         print "Backup Time: " + str(dt)
132                         self.path = BackupPath[str(currentConfigSelectionElement(config.backup.location))]
133                         print "BackupPath: ", str(self.path)
134                         if currentConfigSelectionElement(config.backup.type) == "full":
135                                 print "Backup Mode: Full"
136                                 self.session.open(Console, ["Backup running", "tar -czvf " + self.path + "/" + str(dt) + "_full_backup.tar.gz /etc/"])
137                         if currentConfigSelectionElement(config.backup.type) == "settings":
138                                 print "Backup Mode: Settings"
139                                 self.session.open(Console, ["Backup running", "tar -czvf " + self.path + "/" + str(dt) + "_settings_backup.tar.gz /etc/enigma2/"])
140                         if currentConfigSelectionElement(config.backup.type) == "var":
141                                 print "Backup Mode: var"
142                                 self.session.open(Console, ["Backup running", "tar -czvf " + self.path + "/" + str(dt) + "_var_backup.tar.gz /var/"])
143
144
145 class RestoreMenu(Screen):
146         skin = """
147                 <screen position="100,100" size="400,400" title="Restore Backups" >
148                 <widget name="filelist" position="5,10" size="380,350" scrollbarMode="showOnDemand" />
149                 <widget name="cancel" position="73,365" size="100,30" pixmap="~/red.png" />
150                 <widget name="canceltext" position="73,365" size="100,30" valign="center" halign="center" zPosition="2" font="Regular;20" transparent="1" foregroundColor="black" />
151                 <widget name="restore" position="183,365" size="100,30" pixmap="~/yellow.png" />
152                 <widget name="restoretext" position="183,365" size="100,30" valign="center" halign="center" zPosition="2" font="Regular;20" transparent="1"  foregroundColor="black" />
153                 <widget name="text0" position="0,270" size="350,20" font="Regular;20" />
154                 </screen>"""
155
156         def __init__(self, session, args = None):
157                 Screen.__init__(self, session)
158                 self.skin_path = plugin_path
159                 
160                 self['text0'] = Label("")
161                 self["canceltext"] = Label(_("Cancel"))
162                 self["restoretext"] = Label(_("Restore"))
163                 self["restore"] = Pixmap()
164                 self["cancel"] = Pixmap()
165                 
166                 self.sel = []
167                 self.val = []
168                 self.entry = False
169                 self.exe = False
170                 
171                 self.path = ""
172
173                 self["actions"] = NumberActionMap(["SetupActions"],
174                 {
175                         "ok": self.KeyOk,
176                         "cancel": self.keyCancel
177                 }, -1)
178                 
179                 self["shortcuts"] = ActionMap(["ShortcutActions"],
180                 {
181                         "red": self.keyCancel,
182                         "yellow": self.KeyOk,
183                 })
184                 self.flist = []
185                 self["filelist"] = MenuList(self.flist)
186                 self.fill_list()
187
188         def fill_list(self):
189                 self.flist = []
190                 self.path = BackupPath[str(currentConfigSelectionElement(config.backup.location))]
191                 print "BackupPath: ", str(self.path)
192                 dir = os.listdir(self.path)
193                 for x in dir:
194                         bla = string.find(x, ".tar.gz")
195                         if (bla > 1):
196                                 self.flist.append((x))
197                                 self.entry = True
198                                 self["filelist"].l.setList(self.flist)
199
200
201
202         def KeyOk(self):
203             if (self.exe == False) and (self.entry == True):
204                 self.sel = self["filelist"].getCurrent()
205                 self.val = self.path + self.sel
206                 self.session.openWithCallback(self.startRestore, MessageBox, _("are you sure you want to restore\nfollowing backup:\n\n") + self.sel)
207
208         def keyCancel(self):
209                 self.close()
210
211         def startRestore(self, ret = False):
212                 if (ret == True):
213                         self.exe = True
214                         self['text0'].setText(_('One moment please...'))
215                         print "Restore Location: " + str(self.path)
216                         self.session.open(Console, ["Restore running", "tar -xzvf " + self.path + "/" + self.sel + " -C /"])
217
218         def Exit(self):
219                 self.close()
220
221 def BackupMain(session, **kwargs):
222         session.open(BackupSetup)
223
224 def Plugins(path, **kwargs):
225         global plugin_path
226         plugin_path = path
227         return PluginDescriptor(name="Backup/Restore", description="Backup and Restore your Settings", icon="backup.png", where = PluginDescriptor.WHERE_PLUGINMENU, fnc=BackupMain)