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: class Mage_Backup_Helper_Data extends Mage_Core_Helper_Abstract
31: {
32: 33: 34:
35: const TYPE_DB = 'db';
36:
37: 38: 39:
40: const TYPE_FILESYSTEM = 'filesystem';
41:
42: 43: 44:
45: const TYPE_SYSTEM_SNAPSHOT = 'snapshot';
46:
47: 48: 49:
50: const TYPE_MEDIA = 'media';
51:
52: 53: 54:
55: const TYPE_SNAPSHOT_WITHOUT_MEDIA = 'nomedia';
56:
57: 58: 59: 60: 61:
62: public function getBackupTypes()
63: {
64: return array(
65: self::TYPE_DB => self::__('Database'),
66: self::TYPE_MEDIA => self::__('Database and Media'),
67: self::TYPE_SYSTEM_SNAPSHOT => self::__('System'),
68: self::TYPE_SNAPSHOT_WITHOUT_MEDIA => self::__('System (excluding Media)')
69: );
70: }
71:
72: 73: 74: 75: 76:
77: public function getBackupTypesList()
78: {
79: return array(
80: self::TYPE_DB,
81: self::TYPE_SYSTEM_SNAPSHOT,
82: self::TYPE_SNAPSHOT_WITHOUT_MEDIA,
83: self::TYPE_MEDIA
84: );
85: }
86:
87: 88: 89: 90: 91:
92: public function getDefaultBackupType()
93: {
94: return self::TYPE_DB;
95: }
96:
97: 98: 99: 100: 101:
102: public function getBackupsDir()
103: {
104: return Mage::getBaseDir('var') . DS . 'backups';
105: }
106:
107: 108: 109: 110: 111: 112:
113: public function getExtensionByType($type)
114: {
115: $extensions = $this->getExtensions();
116: return isset($extensions[$type]) ? $extensions[$type] : '';
117: }
118:
119: 120: 121: 122: 123:
124: public function getExtensions()
125: {
126: return array(
127: self::TYPE_SYSTEM_SNAPSHOT => 'tgz',
128: self::TYPE_SNAPSHOT_WITHOUT_MEDIA => 'tgz',
129: self::TYPE_MEDIA => 'tgz',
130: self::TYPE_DB => 'gz'
131: );
132: }
133:
134: 135: 136: 137: 138: 139:
140: public function generateBackupDownloadName(Mage_Backup_Model_Backup $backup)
141: {
142: $additionalExtension = $backup->getType() == self::TYPE_DB ? '.sql' : '';
143: return $backup->getType() . '-' . date('YmdHis', $backup->getTime()) . $additionalExtension . '.'
144: . $this->getExtensionByType($backup->getType());
145: }
146:
147: 148: 149: 150: 151:
152: public function isRollbackAllowed(){
153: return Mage::getSingleton('admin/session')->isAllowed('system/tools/backup/rollback' );
154: }
155:
156: 157: 158: 159: 160:
161: public function getBackupIgnorePaths()
162: {
163: return array(
164: '.svn',
165: 'maintenance.flag',
166: Mage::getBaseDir('var') . DS . 'session',
167: Mage::getBaseDir('var') . DS . 'cache',
168: Mage::getBaseDir('var') . DS . 'full_page_cache',
169: Mage::getBaseDir('var') . DS . 'locks',
170: Mage::getBaseDir('var') . DS . 'log',
171: Mage::getBaseDir('var') . DS . 'report'
172: );
173: }
174:
175: 176: 177: 178: 179:
180: public function getRollbackIgnorePaths()
181: {
182: return array(
183: '.svn',
184: 'maintenance.flag',
185: Mage::getBaseDir('var') . DS . 'session',
186: Mage::getBaseDir('var') . DS . 'locks',
187: Mage::getBaseDir('var') . DS . 'log',
188: Mage::getBaseDir('var') . DS . 'report',
189: Mage::getBaseDir('app') . DS . 'Mage.php',
190: Mage::getBaseDir() . DS . 'errors',
191: Mage::getBaseDir() . DS . 'index.php'
192: );
193: }
194:
195: 196: 197: 198: 199:
200: public function turnOnMaintenanceMode()
201: {
202: $maintenanceFlagFile = $this->getMaintenanceFlagFilePath();
203: $result = file_put_contents($maintenanceFlagFile, 'maintenance');
204:
205: return $result !== false;
206: }
207:
208: 209: 210:
211: public function turnOffMaintenanceMode()
212: {
213: $maintenanceFlagFile = $this->getMaintenanceFlagFilePath();
214: @unlink($maintenanceFlagFile);
215: }
216:
217: 218: 219: 220: 221: 222:
223: public function getCreateSuccessMessageByType($type)
224: {
225: $messagesMap = array(
226: self::TYPE_SYSTEM_SNAPSHOT => $this->__('The system backup has been created.'),
227: self::TYPE_SNAPSHOT_WITHOUT_MEDIA => $this->__('The system (excluding Media) backup has been created.'),
228: self::TYPE_MEDIA => $this->__('The database and media backup has been created.'),
229: self::TYPE_DB => $this->__('The database backup has been created.')
230: );
231:
232: if (!isset($messagesMap[$type])) {
233: return;
234: }
235:
236: return $messagesMap[$type];
237: }
238:
239: 240: 241: 242: 243:
244: protected function getMaintenanceFlagFilePath()
245: {
246: return Mage::getBaseDir() . DS . 'maintenance.flag';
247: }
248:
249: 250: 251: 252:
253: public function invalidateCache()
254: {
255: if ($cacheTypesNode = Mage::getConfig()->getNode(Mage_Core_Model_Cache::XML_PATH_TYPES)) {
256: $cacheTypesList = array_keys($cacheTypesNode->asArray());
257: Mage::app()->getCacheInstance()->invalidateType($cacheTypesList);
258: }
259: return $this;
260: }
261:
262: 263: 264: 265: 266:
267: public function invalidateIndexer()
268: {
269: foreach (Mage::getResourceModel('index/process_collection') as $process){
270: $process->changeStatus(Mage_Index_Model_Process::STATUS_REQUIRE_REINDEX);
271: }
272: return $this;
273: }
274:
275: 276: 277: 278: 279: 280:
281: public function nameToDisplayName($name)
282: {
283: return str_replace('_', ' ', $name);
284: }
285:
286: 287: 288: 289: 290: 291:
292: public function ($filename)
293: {
294: $extensions = $this->getExtensions();
295:
296: $filenameWithoutExtension = $filename;
297:
298: foreach ($extensions as $extension) {
299: $filenameWithoutExtension = preg_replace('/' . preg_quote($extension, '/') . '$/', '',
300: $filenameWithoutExtension
301: );
302: }
303:
304: $filenameWithoutExtension = substr($filenameWithoutExtension, 0, strrpos($filenameWithoutExtension, "."));
305:
306: list($time, $type) = explode("_", $filenameWithoutExtension);
307:
308: $name = str_replace($time . '_' . $type, '', $filenameWithoutExtension);
309:
310: if (!empty($name)) {
311: $name = substr($name, 1);
312: }
313:
314: $result = new Varien_Object();
315: $result->addData(array(
316: 'name' => $name,
317: 'type' => $type,
318: 'time' => $time
319: ));
320:
321: return $result;
322: }
323: }
324: