1: <?php
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: class Mage_Adminhtml_System_BackupController extends Mage_Adminhtml_Controller_Action
35: {
36: 37: 38:
39: public function indexAction()
40: {
41: $this->_title($this->__('System'))->_title($this->__('Tools'))->_title($this->__('Backups'));
42:
43: if($this->getRequest()->getParam('ajax')) {
44: $this->_forward('grid');
45: return;
46: }
47:
48: $this->loadLayout();
49: $this->_setActiveMenu('system');
50: $this->_addBreadcrumb(Mage::helper('adminhtml')->__('System'), Mage::helper('adminhtml')->__('System'));
51: $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Tools'), Mage::helper('adminhtml')->__('Tools'));
52: $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Backups'), Mage::helper('adminhtml')->__('Backup'));
53:
54: $this->_addContent($this->getLayout()->createBlock('adminhtml/backup', 'backup'));
55:
56: $this->renderLayout();
57: }
58:
59: 60: 61:
62: public function gridAction()
63: {
64: $this->getResponse()->setBody($this->getLayout()->createBlock('adminhtml/backup_grid')->toHtml());
65: }
66:
67: 68: 69: 70: 71:
72: public function createAction()
73: {
74: if (!$this->getRequest()->isAjax()) {
75: return $this->getUrl('*/*/index');
76: }
77:
78: $response = new Varien_Object();
79:
80: 81: 82:
83: $helper = Mage::helper('backup');
84:
85: try {
86: $type = $this->getRequest()->getParam('type');
87:
88: if ($type == Mage_Backup_Helper_Data::TYPE_SYSTEM_SNAPSHOT
89: && $this->getRequest()->getParam('exclude_media')
90: ) {
91: $type = Mage_Backup_Helper_Data::TYPE_SNAPSHOT_WITHOUT_MEDIA;
92: }
93:
94: $backupManager = Mage_Backup::getBackupInstance($type)
95: ->setBackupExtension($helper->getExtensionByType($type))
96: ->setTime(time())
97: ->setBackupsDir($helper->getBackupsDir());
98:
99: $backupManager->setName($this->getRequest()->getParam('backup_name'));
100:
101: Mage::register('backup_manager', $backupManager);
102:
103: if ($this->getRequest()->getParam('maintenance_mode')) {
104: $turnedOn = $helper->turnOnMaintenanceMode();
105:
106: if (!$turnedOn) {
107: $response->setError(
108: Mage::helper('backup')->__('You do not have sufficient permissions to enable Maintenance Mode during this operation.')
109: . ' ' . Mage::helper('backup')->__('Please either unselect the "Put store on the maintenance mode" checkbox or update your permissions to proceed with the backup."')
110: );
111: $backupManager->setErrorMessage(Mage::helper('backup')->__("System couldn't put store on the maintenance mode"));
112: return $this->getResponse()->setBody($response->toJson());
113: }
114: }
115:
116: if ($type != Mage_Backup_Helper_Data::TYPE_DB) {
117: $backupManager->setRootDir(Mage::getBaseDir())
118: ->addIgnorePaths($helper->getBackupIgnorePaths());
119: }
120:
121: $successMessage = $helper->getCreateSuccessMessageByType($type);
122:
123: $backupManager->create();
124:
125: $this->_getSession()->addSuccess($successMessage);
126:
127: $response->setRedirectUrl($this->getUrl('*/*/index'));
128: } catch (Mage_Backup_Exception_NotEnoughFreeSpace $e) {
129: $errorMessage = Mage::helper('backup')->__('Not enough free space to create backup.');
130: } catch (Mage_Backup_Exception_NotEnoughPermissions $e) {
131: Mage::log($e->getMessage());
132: $errorMessage = Mage::helper('backup')->__('Not enough permissions to create backup.');
133: } catch (Exception $e) {
134: Mage::log($e->getMessage());
135: $errorMessage = Mage::helper('backup')->__('An error occurred while creating the backup.');
136: }
137:
138: if (!empty($errorMessage)) {
139: $response->setError($errorMessage);
140: $backupManager->setErrorMessage($errorMessage);
141: }
142:
143: if ($this->getRequest()->getParam('maintenance_mode')) {
144: $helper->turnOffMaintenanceMode();
145: }
146:
147: $this->getResponse()->setBody($response->toJson());
148: }
149:
150: 151: 152: 153: 154:
155: public function downloadAction()
156: {
157:
158: $backup = Mage::getModel('backup/backup')->loadByTimeAndType(
159: $this->getRequest()->getParam('time'),
160: $this->getRequest()->getParam('type')
161: );
162:
163: if (!$backup->getTime() || !$backup->exists()) {
164: return $this->_redirect('*/*');
165: }
166:
167: $fileName = Mage::helper('backup')->generateBackupDownloadName($backup);
168:
169: $this->_prepareDownloadResponse($fileName, null, 'application/octet-stream', $backup->getSize());
170:
171: $this->getResponse()->sendHeaders();
172:
173: $backup->output();
174: exit();
175: }
176:
177: 178: 179: 180: 181:
182: public function rollbackAction()
183: {
184: if (!Mage::helper('backup')->isRollbackAllowed()){
185: return $this->_forward('denied');
186: }
187:
188: if (!$this->getRequest()->isAjax()) {
189: return $this->getUrl('*/*/index');
190: }
191:
192: $helper = Mage::helper('backup');
193: $response = new Varien_Object();
194:
195: try {
196:
197: $backup = Mage::getModel('backup/backup')->loadByTimeAndType(
198: $this->getRequest()->getParam('time'),
199: $this->getRequest()->getParam('type')
200: );
201:
202: if (!$backup->getTime() || !$backup->exists()) {
203: return $this->_redirect('*/*');
204: }
205:
206: if (!$backup->getTime()) {
207: throw new Mage_Backup_Exception_CantLoadSnapshot();
208: }
209:
210: $type = $backup->getType();
211:
212: $backupManager = Mage_Backup::getBackupInstance($type)
213: ->setBackupExtension($helper->getExtensionByType($type))
214: ->setTime($backup->getTime())
215: ->setBackupsDir($helper->getBackupsDir())
216: ->setName($backup->getName(), false)
217: ->setResourceModel(Mage::getResourceModel('backup/db'));
218:
219: Mage::register('backup_manager', $backupManager);
220:
221: $passwordValid = Mage::getModel('backup/backup')->validateUserPassword(
222: $this->getRequest()->getParam('password')
223: );
224:
225: if (!$passwordValid) {
226: $response->setError(Mage::helper('backup')->__('Invalid Password.'));
227: $backupManager->setErrorMessage(Mage::helper('backup')->__('Invalid Password.'));
228: return $this->getResponse()->setBody($response->toJson());
229: }
230:
231: if ($this->getRequest()->getParam('maintenance_mode')) {
232: $turnedOn = $helper->turnOnMaintenanceMode();
233:
234: if (!$turnedOn) {
235: $response->setError(
236: Mage::helper('backup')->__('You do not have sufficient permissions to enable Maintenance Mode during this operation.')
237: . ' ' . Mage::helper('backup')->__('Please either unselect the "Put store on the maintenance mode" checkbox or update your permissions to proceed with the rollback."')
238: );
239: $backupManager->setErrorMessage(Mage::helper('backup')->__("System couldn't put store on the maintenance mode"));
240: return $this->getResponse()->setBody($response->toJson());
241: }
242: }
243:
244: if ($type != Mage_Backup_Helper_Data::TYPE_DB) {
245:
246: $backupManager->setRootDir(Mage::getBaseDir())
247: ->addIgnorePaths($helper->getRollbackIgnorePaths());
248:
249: if ($this->getRequest()->getParam('use_ftp', false)) {
250: $backupManager->setUseFtp(
251: $this->getRequest()->getParam('ftp_host', ''),
252: $this->getRequest()->getParam('ftp_user', ''),
253: $this->getRequest()->getParam('ftp_pass', ''),
254: $this->getRequest()->getParam('ftp_path', '')
255: );
256: }
257: }
258:
259: $backupManager->rollback();
260:
261: $helper->invalidateCache()->invalidateIndexer();
262:
263: $adminSession = $this->_getSession();
264: $adminSession->unsetAll();
265: $adminSession->getCookie()->delete($adminSession->getSessionName());
266:
267: $response->setRedirectUrl($this->getUrl('*'));
268: } catch (Mage_Backup_Exception_CantLoadSnapshot $e) {
269: $errorMsg = Mage::helper('backup')->__('Backup file not found');
270: } catch (Mage_Backup_Exception_FtpConnectionFailed $e) {
271: $errorMsg = Mage::helper('backup')->__('Failed to connect to FTP');
272: } catch (Mage_Backup_Exception_FtpValidationFailed $e) {
273: $errorMsg = Mage::helper('backup')->__('Failed to validate FTP');
274: } catch (Mage_Backup_Exception_NotEnoughPermissions $e) {
275: Mage::log($e->getMessage());
276: $errorMsg = Mage::helper('backup')->__('Not enough permissions to perform rollback');
277: } catch (Exception $e) {
278: Mage::log($e->getMessage());
279: $errorMsg = Mage::helper('backup')->__('Failed to rollback');
280: }
281:
282: if (!empty($errorMsg)) {
283: $response->setError($errorMsg);
284: $backupManager->setErrorMessage($errorMsg);
285: }
286:
287: if ($this->getRequest()->getParam('maintenance_mode')) {
288: $helper->turnOffMaintenanceMode();
289: }
290:
291: $this->getResponse()->setBody($response->toJson());
292: }
293:
294: 295: 296: 297: 298:
299: public function massDeleteAction()
300: {
301: $backupIds = $this->getRequest()->getParam('ids', array());
302:
303: if (!is_array($backupIds) || !count($backupIds)) {
304: return $this->_redirect('*/*/index');
305: }
306:
307:
308: $backupModel = Mage::getModel('backup/backup');
309: $resultData = new Varien_Object();
310: $resultData->setIsSuccess(false);
311: $resultData->setDeleteResult(array());
312: Mage::register('backup_manager', $resultData);
313:
314: $deleteFailMessage = Mage::helper('backup')->__('Failed to delete one or several backups.');
315:
316: try {
317: $allBackupsDeleted = true;
318:
319: foreach ($backupIds as $id) {
320: list($time, $type) = explode('_', $id);
321: $backupModel
322: ->loadByTimeAndType($time, $type)
323: ->deleteFile();
324:
325: if ($backupModel->exists()) {
326: $allBackupsDeleted = false;
327: $result = Mage::helper('adminhtml')->__('failed');
328: } else {
329: $result = Mage::helper('adminhtml')->__('successful');
330: }
331:
332: $resultData->setDeleteResult(
333: array_merge($resultData->getDeleteResult(), array($backupModel->getFileName() . ' ' . $result))
334: );
335: }
336:
337: $resultData->setIsSuccess(true);
338: if ($allBackupsDeleted) {
339: $this->_getSession()->addSuccess(
340: Mage::helper('backup')->__('The selected backup(s) has been deleted.')
341: );
342: }
343: else {
344: throw new Exception($deleteFailMessage);
345: }
346: } catch (Exception $e) {
347: $resultData->setIsSuccess(false);
348: $this->_getSession()->addError($deleteFailMessage);
349: }
350:
351: return $this->_redirect('*/*/index');
352: }
353:
354: 355: 356: 357: 358:
359: protected function _isAllowed()
360: {
361: return Mage::getSingleton('admin/session')->isAllowed('system/tools/backup' );
362: }
363:
364: 365: 366: 367: 368:
369: protected function _getSession()
370: {
371: return Mage::getSingleton('adminhtml/session');
372: }
373: }
374: