backup_migrate.module 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072
  1. <?php
  2. /**
  3. * @file
  4. * Backup and restore databases for Drupal.
  5. */
  6. define('BACKUP_MIGRATE_VERSION', '7.x-3.x');
  7. define('BACKUP_MIGRATE_MENU_PATH', 'admin/config/system/backup_migrate');
  8. define('BACKUP_MIGRATE_MENU_DEPTH', 4);
  9. /**
  10. * Implements hook_help().
  11. */
  12. function backup_migrate_help($section, $arg) {
  13. $help = array(
  14. array(
  15. 'body' => t('Backup and Migrate makes the task of backing up your Drupal database and migrating data from one Drupal install to another easier. It provides a function to backup the entire database to file or download, and to restore from a previous backup. You can also schedule the backup operation. Compression of backup files is also supported. The database backup files created with this module can be imported into this or any other Drupal installation with the !restorelink, or you can use a database tool such as <a href="!phpmyadminurl">phpMyAdmin</a> or the mysql command line command.', array(
  16. '!restorelink' => user_access('restore from backup') ? l(t('restore feature'), BACKUP_MIGRATE_MENU_PATH . '/restore') : t('restore feature'),
  17. '!phpmyadminurl' => 'http://www.phpmyadmin.net',
  18. )),
  19. ),
  20. BACKUP_MIGRATE_MENU_PATH => array(
  21. 'title' => t('Quick Backup Tab'),
  22. 'body' => t('Use this form to run simple manual backups of your database and files.'),
  23. 'access arguments' => array('perform backup'),
  24. ),
  25. BACKUP_MIGRATE_MENU_PATH . '/export/advanced' => array(
  26. 'title' => t('Advanced Backup Tab'),
  27. 'body' => t('Use this form to run manual backups of your database and files with more advanced options. If you have any !profilelink saved you can load those settings. You can save any of the changes you make to these settings as a new settings profile.', array(
  28. "!profilelink" => user_access('administer backup and migrate') ? l(t('settings profiles'), BACKUP_MIGRATE_MENU_PATH . '/profile') : t('settings profiles'),
  29. '!restorelink' => user_access('restore from backup') ? l(t('restore feature'), BACKUP_MIGRATE_MENU_PATH . '/restore') : t('restore feature'),
  30. '!phpmyadminurl' => 'http://www.phpmyadmin.net',
  31. )),
  32. 'access arguments' => array('perform backup'),
  33. ),
  34. BACKUP_MIGRATE_MENU_PATH . '/restore' => array(
  35. 'title' => t('Restore Tab'),
  36. 'body' => t('Upload a backup and migrate backup file. The restore function will not work with database dumps from other sources such as phpMyAdmin.'),
  37. 'access arguments' => array('restore from backup'),
  38. ),
  39. BACKUP_MIGRATE_MENU_PATH . '/settings/destination' => array(
  40. 'title' => t('Destinations'),
  41. 'body' => t('Destinations are the places you can save your backup files to or them load from.'),
  42. 'more' => t('Files can be saved to a directory on your web server, downloaded to your desktop or emailed to a specified email account. From the Destinations tab you can create, delete and edit destinations or list the files which have already been backed up to the available destinations.'),
  43. 'access arguments' => array('administer backup and migrate'),
  44. ),
  45. BACKUP_MIGRATE_MENU_PATH . '/settings/source' => array(
  46. 'title' => t('Sources'),
  47. 'body' => t('Sources are the things that can be backed up such as database or file directories.'),
  48. 'access arguments' => array('administer backup and migrate'),
  49. ),
  50. BACKUP_MIGRATE_MENU_PATH . '/settings/profile' => array(
  51. 'title' => t('Profiles'),
  52. 'body' => t('Profiles are saved backup settings. Profiles store your table exclusion settings as well as your backup file name, compression and timestamp settings. You can use profiles in !schedulelink and for !manuallink.', array(
  53. '!schedulelink' => user_access('administer backup and migrate') ? l(t('schedules'), BACKUP_MIGRATE_MENU_PATH . '/schedule') : t('settings profiles'),
  54. '!manuallink' => user_access('perform backups') ? l(t('manual backups'), BACKUP_MIGRATE_MENU_PATH) : t('manual backups'),
  55. )),
  56. 'more' => t('You can create new profiles using the add profiles tab or by checking the "Save these settings" button on the advanced backup page.'),
  57. 'access arguments' => array('administer backup and migrate'),
  58. ),
  59. BACKUP_MIGRATE_MENU_PATH . '/schedule' => array(
  60. 'title' => t('Scheduling'),
  61. 'body' => t('Automatically backup up your database and files on a regular schedule using <a href="!cronurl">cron</a>.', array(
  62. '!cronurl' => 'http://drupal.org/cron',
  63. )),
  64. 'more' => t("Each schedule will run a maximum of once per cron run, so they will not run more frequently than your cron is configured to run. If you specify a number of backups to keep for a schedule, old backups will be deleted as new ones created. <strong>If specifiy a number of files to keep other backup files in that schedule's destination will get deleted</strong>."),
  65. 'access arguments' => array('administer backup and migrate'),
  66. ),
  67. BACKUP_MIGRATE_MENU_PATH . '/settings/import' => array(
  68. 'title' => t('Importing Settings'),
  69. 'body' => t('Import a settings profile, backup schedule, source or destination by pasting the export code into the textarea.', array(
  70. '!cronurl' => 'http://drupal.org/cron',
  71. )),
  72. ),
  73. );
  74. if (module_exists('help')) {
  75. $help[BACKUP_MIGRATE_MENU_PATH]['body'] .= ' ' . t('Visit the !helppage for more help using this module.', array('!helppage' => l(t('help page'), 'admin/help/backup_migrate')));
  76. }
  77. if (isset($help[$section])) {
  78. return $help[$section]['body'];
  79. }
  80. if ($section == 'admin/help#backup_migrate') {
  81. $out = '';
  82. foreach ($help as $key => $section) {
  83. if (isset($section['access arguments'])) {
  84. foreach ($section['access arguments'] as $access) {
  85. if (!user_access($access)) {
  86. continue 2;
  87. }
  88. }
  89. }
  90. if (@$section['title']) {
  91. if (!is_numeric($key)) {
  92. $section['title'] = l($section['title'], $key);
  93. }
  94. $out .= "<h3>" . $section['title'] . "</h3>";
  95. }
  96. $out .= "<p>" . $section['body'] . "</p>";
  97. if (!empty($section['more'])) {
  98. $out .= "<p>" . $section['more'] . "</p>";
  99. }
  100. }
  101. return $out;
  102. }
  103. }
  104. /**
  105. * Implements hook_menu().
  106. */
  107. function backup_migrate_menu() {
  108. $items = array();
  109. $items[BACKUP_MIGRATE_MENU_PATH] = array(
  110. 'title' => 'Backup and Migrate',
  111. 'description' => 'Backup/restore your database and files or migrate data to or from another Drupal site.',
  112. 'page callback' => 'backup_migrate_menu_callback',
  113. 'page arguments' => array('', 'backup_migrate_ui_manual_backup_quick', TRUE),
  114. 'access arguments' => array('access backup and migrate'),
  115. 'type' => MENU_NORMAL_ITEM,
  116. );
  117. $items[BACKUP_MIGRATE_MENU_PATH . '/export'] = array(
  118. 'title' => 'Backup',
  119. 'description' => 'Backup the database.',
  120. 'page callback' => 'backup_migrate_menu_callback',
  121. 'page arguments' => array('', 'backup_migrate_ui_manual_backup_quick', TRUE),
  122. 'access arguments' => array('access backup and migrate'),
  123. 'weight' => 0,
  124. 'type' => MENU_DEFAULT_LOCAL_TASK,
  125. );
  126. $items[BACKUP_MIGRATE_MENU_PATH . '/export/quick'] = array(
  127. 'title' => 'Quick Backup',
  128. 'description' => 'Backup the database.',
  129. 'page callback' => 'backup_migrate_menu_callback',
  130. 'page arguments' => array('', 'backup_migrate_ui_manual_backup_quick', TRUE),
  131. 'access arguments' => array('perform backup'),
  132. 'weight' => 0,
  133. 'type' => MENU_DEFAULT_LOCAL_TASK,
  134. );
  135. $items[BACKUP_MIGRATE_MENU_PATH . '/export/advanced'] = array(
  136. 'title' => 'Advanced Backup',
  137. 'description' => 'Backup the database.',
  138. 'page callback' => 'backup_migrate_menu_callback',
  139. 'page arguments' => array(
  140. '',
  141. 'backup_migrate_ui_manual_backup_advanced',
  142. TRUE,
  143. ),
  144. 'access arguments' => array('perform backup'),
  145. 'weight' => 1,
  146. 'type' => MENU_LOCAL_TASK,
  147. );
  148. $items[BACKUP_MIGRATE_MENU_PATH . '/restore'] = array(
  149. 'title' => 'Restore',
  150. 'description' => 'Restore the database from a previous backup',
  151. 'page callback' => 'backup_migrate_menu_callback',
  152. 'page arguments' => array('', 'backup_migrate_ui_manual_restore', TRUE),
  153. 'access arguments' => array('restore from backup'),
  154. 'weight' => 1,
  155. 'type' => MENU_LOCAL_TASK,
  156. );
  157. $items[BACKUP_MIGRATE_MENU_PATH . '/backups'] = array(
  158. 'title' => 'Saved Backups',
  159. 'description' => 'Previously created backups',
  160. 'page callback' => 'backup_migrate_menu_callback',
  161. 'page arguments' => array('', 'backup_migrate_ui_saved_backups', TRUE),
  162. 'access arguments' => array('access backup files'),
  163. 'weight' => 2,
  164. 'type' => MENU_LOCAL_TASK,
  165. );
  166. // @TODO: This was removed because it was getting far too complicated for an
  167. // abstraction. Exposing the actual schedules is more direct.
  168. // @code
  169. // $items[BACKUP_MIGRATE_MENU_PATH . '/schedule'] = array(
  170. // 'title' => 'Schedule',
  171. // 'description' => 'Schedule automatic backups',
  172. // 'page callback' => 'backup_migrate_menu_callback',
  173. // 'page arguments' => array('', 'backup_migrate_ui_schedule', TRUE),
  174. // 'access arguments' => array('administer backup and migrate'),
  175. // 'weight' => 3,
  176. // 'type' => MENU_LOCAL_TASK,
  177. // );
  178. // @endcode
  179. $items[BACKUP_MIGRATE_MENU_PATH . '/settings'] = array(
  180. 'title' => 'Settings',
  181. 'description' => 'Module settings.',
  182. 'page callback' => 'backup_migrate_menu_callback',
  183. 'page arguments' => array('crud', 'backup_migrate_crud_ui_list_all', TRUE),
  184. 'access arguments' => array('administer backup and migrate'),
  185. 'weight' => 4,
  186. 'type' => MENU_LOCAL_TASK,
  187. );
  188. $items[BACKUP_MIGRATE_MENU_PATH . '/settings/import'] = array(
  189. 'title' => 'Import',
  190. 'description' => 'Import backup and migrate settings.',
  191. 'page callback' => 'backup_migrate_menu_callback',
  192. 'page arguments' => array('crud', 'backup_migrate_crud_ui_import', TRUE),
  193. 'access arguments' => array('administer backup and migrate'),
  194. 'weight' => 10,
  195. 'type' => MENU_LOCAL_TASK,
  196. );
  197. backup_migrate_include('crud');
  198. $items += backup_migrate_crud_menu();
  199. return $items;
  200. }
  201. /**
  202. * Implements hook_element_info().
  203. */
  204. function backup_migrate_element_info() {
  205. $types['backup_migrate_dependent'] = array(
  206. '#dependencies' => array(),
  207. '#theme' => 'backup_migrate_dependent',
  208. );
  209. $types['backup_migrate_file_list'] = array(
  210. '#theme' => 'backup_migrate_file_list',
  211. '#process' => array('form_process_backup_migrate_file_list'),
  212. '#multiple' => FALSE,
  213. '#files' => array(),
  214. '#display_options' => array(),
  215. '#default_value' => 0,
  216. );
  217. return $types;
  218. }
  219. /**
  220. * Callback function for backup_migrate_file_list '#process' callback.
  221. */
  222. function form_process_backup_migrate_file_list($element) {
  223. if (count($element['#files']) > 0) {
  224. $weight = 0;
  225. $options = array();
  226. $max = $count = $element['#display_options']['limit'];
  227. $files = $element['#files'];
  228. foreach ($files as $id => $file) {
  229. $options[$id] = $file->info('filename');
  230. }
  231. $element['files'] = array(
  232. '#type' => 'radios',
  233. '#options' => $options,
  234. '#default_value' => $element['#default_value'],
  235. '#parents' => $element['#parents'],
  236. );
  237. }
  238. return $element;
  239. }
  240. /**
  241. * Implements hook_form_update_script_selection_form_alter().
  242. */
  243. function backup_migrate_form_update_script_selection_form_alter(&$form, $form_state) {
  244. $msg = t('Before you run any database updates, <strong>make sure you have a backup of your database.</strong> Use !backup_migrate to create that backup now.', array('!backup_migrate' => l(t('Backup and Migrate'), BACKUP_MIGRATE_MENU_PATH)));
  245. drupal_set_message($msg, 'warning');
  246. }
  247. /**
  248. * Implements hook_cron().
  249. */
  250. function backup_migrate_cron() {
  251. // Optionally disable hook_cron().
  252. if (variable_get('backup_migrate_disable_cron', FALSE)) {
  253. if (variable_get('backup_migrate_verbose')) {
  254. watchdog('backup_migrate', 'Cron disabled.');
  255. }
  256. return;
  257. }
  258. if (variable_get('backup_migrate_verbose')) {
  259. watchdog('backup_migrate', 'Cron starting.');
  260. }
  261. // Takes care of scheduled backups and deletes abandoned temp files.
  262. // Set the message mode to logging.
  263. _backup_migrate_message_callback('_backup_migrate_message_log');
  264. // Clean up any previous abandoned tmp files before we attempt to back up.
  265. backup_migrate_include('files');
  266. // Clean up any temporary files that were already present.
  267. if (variable_get('backup_migrate_verbose')) {
  268. watchdog('backup_migrate', 'Deleting temp files.');
  269. }
  270. _backup_migrate_temp_files_delete();
  271. if (variable_get('backup_migrate_verbose')) {
  272. watchdog('backup_migrate', 'Processing schedule(s) backups.');
  273. }
  274. backup_migrate_include('schedules');
  275. backup_migrate_schedules_cron();
  276. // Clean up any temporary files from this run.
  277. if (variable_get('backup_migrate_verbose')) {
  278. watchdog('backup_migrate', 'Deleting temp files.');
  279. }
  280. _backup_migrate_temp_files_delete();
  281. if (variable_get('backup_migrate_verbose')) {
  282. watchdog('backup_migrate', 'Cron finished.');
  283. }
  284. }
  285. /**
  286. * Implements hook_cronapi().
  287. */
  288. function backup_migrate_cronapi($op, $job = NULL) {
  289. // Optionally disable hook_cron().
  290. if (variable_get('backup_migrate_disable_cron', FALSE)) {
  291. if (variable_get('backup_migrate_verbose')) {
  292. watchdog('backup_migrate', 'Cron disabled.');
  293. }
  294. return;
  295. }
  296. $items = array();
  297. backup_migrate_include('schedules');
  298. foreach (backup_migrate_get_schedules() as $schedule) {
  299. if ($schedule->get('cron') == BACKUP_MIGRATE_CRON_ELYSIA) {
  300. $id = $schedule->get('id');
  301. $items['backup_migrate_' . $id] = array(
  302. 'description' => t("Run the Backup and Migrate '!name' schedule", array('!name' => $schedule->get('name'))),
  303. 'rule' => $schedule->get('cron_schedule'),
  304. 'callback' => 'backup_migrate_schedule_run',
  305. 'arguments' => array($id),
  306. 'file' => 'includes/schedules.inc',
  307. );
  308. }
  309. }
  310. return $items;
  311. }
  312. /**
  313. * Implements hook_permission().
  314. */
  315. function backup_migrate_permission() {
  316. return array(
  317. 'access backup and migrate' => array(
  318. 'title' => t('Access Backup and Migrate'),
  319. 'description' => t('Access the Backup and Migrate admin section.'),
  320. 'restrict access' => TRUE,
  321. ),
  322. 'perform backup' => array(
  323. 'title' => t('Perform a backup'),
  324. 'description' => t('Back up any of the available databases.'),
  325. 'restrict access' => TRUE,
  326. ),
  327. 'access backup files' => array(
  328. 'title' => t('Access backup files'),
  329. 'description' => t('Access and download the previously created backup files.'),
  330. 'restrict access' => TRUE,
  331. ),
  332. 'delete backup files' => array(
  333. 'title' => t('Delete backup files'),
  334. 'description' => t('Delete the previously created backup files.'),
  335. 'restrict access' => TRUE,
  336. ),
  337. 'restore from backup' => array(
  338. 'title' => t('Restore the site'),
  339. 'description' => t("Restore the site's database from a backup file."),
  340. 'restrict access' => TRUE,
  341. ),
  342. 'administer backup and migrate' => array(
  343. 'title' => t('Administer Backup and Migrate'),
  344. 'description' => t('Edit Backup and Migrate profiles, schedules and destinations.'),
  345. 'restrict access' => TRUE,
  346. ),
  347. );
  348. }
  349. /**
  350. * Implements hook_simpletest().
  351. */
  352. function backup_migrate_simpletest() {
  353. $dir = drupal_get_path('module', 'backup_migrate') . '/tests';
  354. $tests = file_scan_directory($dir, '\.test$');
  355. return array_keys($tests);
  356. }
  357. /**
  358. * Implements hook_theme().
  359. */
  360. function backup_migrate_theme() {
  361. $themes = array(
  362. 'backup_migrate_ui_manual_quick_backup_form_inline' => array(
  363. 'arguments' => array('form'),
  364. 'render element' => 'form',
  365. ),
  366. 'backup_migrate_ui_quick_schedule_form_inline' => array(
  367. 'arguments' => array('form'),
  368. 'render element' => 'form',
  369. ),
  370. 'backup_migrate_dependent' => array(
  371. 'arguments' => array('element'),
  372. 'render element' => 'element',
  373. ),
  374. 'backup_migrate_file_list' => array(
  375. 'arguments' => array('element'),
  376. 'render element' => 'element',
  377. ),
  378. 'backup_migrate_group' => array(
  379. 'variables' => array('title' => '', 'body' => '', 'description' => ''),
  380. ),
  381. );
  382. return $themes;
  383. }
  384. /**
  385. * Implements hook_block_info().
  386. */
  387. function backup_migrate_block_info() {
  388. $blocks = array();
  389. $blocks['quick_backup'] = array(
  390. 'info' => t('Backup and Migrate Quick Backup'),
  391. 'admin' => TRUE,
  392. );
  393. return $blocks;
  394. }
  395. /**
  396. * Implements hook_block_view().
  397. */
  398. function backup_migrate_block_view($delta) {
  399. $function = '_backup_migrate_block_view_' . $delta;
  400. if (function_exists('_backup_migrate_block_view_' . $delta)) {
  401. return $function();
  402. }
  403. }
  404. /**
  405. * Quick Backup block.
  406. */
  407. function _backup_migrate_block_view_quick_backup() {
  408. if (user_access('access backup and migrate') && user_access('perform backup') && $_GET['q'] != BACKUP_MIGRATE_MENU_PATH) {
  409. drupal_add_css(drupal_get_path('module', 'backup_migrate') . '/backup_migrate.css');
  410. // Messages should be sent to the browser.
  411. _backup_migrate_message_callback('_backup_migrate_message_browser');
  412. return array(
  413. 'subject' => t('Quick Backup'),
  414. 'content' => drupal_get_form('backup_migrate_ui_manual_quick_backup_form', FALSE),
  415. );
  416. }
  417. return array();
  418. }
  419. /**
  420. * Implements hook_backup_migrate_destinations().
  421. */
  422. function backup_migrate_backup_migrate_schedules() {
  423. backup_migrate_include('sources');
  424. $schedules = array();
  425. // Declare a default backup for each source that can be enabled.
  426. // @TODO: This makes setting up scheduled easier, but only when combined with
  427. // the now disabled quick-schedule feature.
  428. // @code
  429. // $sources = backup_migrate_get_sources();
  430. // foreach ($sources as $id => $source) {
  431. // $schedule = array(
  432. // 'schedule_id' => $id,
  433. // 'name' => $source->get('name'),
  434. // 'source_id' => $id,
  435. // 'destination_id' => 'scheduled',
  436. // 'profile_id' => 'default',
  437. // 'period' => variable_get('backup_migrate_default_schedule', 60*60),
  438. // 'keep' => BACKUP_MIGRATE_SMART_DELETE,
  439. // 'enabled' => FALSE,
  440. // );
  441. // $schedules[$id] = backup_migrate_crud_create_item('schedule', $schedule);
  442. // }
  443. // @endcode
  444. if (variable_get('nodesquirrel_secret_key', FALSE) != FALSE) {
  445. $schedule = array(
  446. 'machine_name' => 'nodesquirrel',
  447. 'name' => 'NodeSquirrel',
  448. 'source_id' => variable_get('nodesquirrel_schedule_source_id', 'db'),
  449. 'destination_id' => 'nodesquirrel',
  450. 'profile_id' => 'default',
  451. 'period' => variable_get('nodesquirrel_schedule', 60 * 60 * 24),
  452. 'enabled' => variable_get('nodesquirrel_schedule_enabled', TRUE) && variable_get('nodesquirrel_secret_key', FALSE) != FALSE,
  453. 'keep' => variable_get('nodesquirrel_schedule_keep', BACKUP_MIGRATE_SMART_DELETE),
  454. );
  455. $schedules['nodesquirrel'] = backup_migrate_crud_create_item('schedule', $schedule);
  456. }
  457. return $schedules;
  458. }
  459. /**
  460. * Implements hook_ctools_plugin_api().
  461. */
  462. function backup_migrate_ctools_plugin_api($owner, $api) {
  463. // Tell CTools that we support the default_mymodule_presets API.
  464. if ($owner == 'backup_migrate') {
  465. return array('version' => 1);
  466. }
  467. }
  468. /**
  469. * A menu callback helper. Handles file includes and interactivity setting.
  470. */
  471. function backup_migrate_menu_callback($include, $function, $interactive = TRUE) {
  472. if ($include) {
  473. backup_migrate_include($include);
  474. }
  475. // Set the message handler based on interactivity setting.
  476. _backup_migrate_message_callback($interactive ? '_backup_migrate_message_browser' : '_backup_migrate_message_log');
  477. // Get the arguments with the first 3 removed.
  478. $args = array_slice(func_get_args(), 3);
  479. return call_user_func_array($function, $args);
  480. }
  481. /**
  482. * Include views .inc files as necessary.
  483. */
  484. function backup_migrate_include() {
  485. static $used = array();
  486. foreach (func_get_args() as $file) {
  487. if (!isset($used[$file])) {
  488. require_once backup_migrate_include_path($file);
  489. }
  490. $used[$file] = TRUE;
  491. }
  492. }
  493. /**
  494. * Include views .inc files as necessary.
  495. */
  496. function backup_migrate_include_path($file) {
  497. return DRUPAL_ROOT . '/' . drupal_get_path('module', 'backup_migrate') . "/includes/$file.inc";
  498. }
  499. /**
  500. * The menu callback for easy manual backups.
  501. */
  502. function backup_migrate_ui_manual_backup_quick() {
  503. $out = array();
  504. if (user_access('perform backup')) {
  505. return drupal_get_form('backup_migrate_ui_manual_quick_backup_form');
  506. }
  507. else {
  508. return t('You do not have permission to back up this site.');
  509. }
  510. return $out;
  511. }
  512. /**
  513. * The menu callback for advanced manual backups.
  514. */
  515. function backup_migrate_ui_manual_backup_advanced() {
  516. backup_migrate_include('profiles');
  517. $out = array();
  518. $profile_id = arg(BACKUP_MIGRATE_MENU_DEPTH + 2);
  519. $profile = _backup_migrate_profile_saved_default_profile($profile_id);
  520. $out[] = drupal_get_form('backup_migrate_ui_manual_backup_load_profile_form', $profile);
  521. $out[] = drupal_get_form('backup_migrate_ui_manual_backup_form', $profile);
  522. return $out;
  523. }
  524. /**
  525. * The backup/export load profile form.
  526. */
  527. function backup_migrate_ui_manual_backup_load_profile_form($form, &$form_state, $profile = NULL) {
  528. $form = array();
  529. $profile_options = _backup_migrate_get_profile_form_item_options();
  530. if (count($profile_options) > 0) {
  531. $profile_options = array(0 => t('-- Select a Settings Profile --')) + $profile_options;
  532. $form['profile'] = array(
  533. "#title" => t("Settings Profile"),
  534. "#collapsible" => TRUE,
  535. "#collapsed" => FALSE,
  536. "#prefix" => '<div class="container-inline">',
  537. "#suffix" => '</div>',
  538. "#tree" => FALSE,
  539. "#description" => t("You can load a profile. Any changes you made below will be lost."),
  540. );
  541. $form['profile']['profile_id'] = array(
  542. "#type" => "select",
  543. "#title" => t("Load Settings"),
  544. '#default_value' => is_object($profile) ? $profile->get_id() : 0,
  545. "#options" => $profile_options,
  546. );
  547. $form['profile']['load_profile'] = array(
  548. '#type' => 'submit',
  549. '#value' => t('Load Profile'),
  550. );
  551. }
  552. return $form;
  553. }
  554. /**
  555. * Submit the profile load form.
  556. */
  557. function backup_migrate_ui_manual_backup_load_profile_form_submit($form, &$form_state) {
  558. if ($profile = backup_migrate_get_profile($form_state['values']['profile_id'])) {
  559. variable_set("backup_migrate_profile_id", $profile->get_id());
  560. $form_state['redirect'] = BACKUP_MIGRATE_MENU_PATH . '/export/advanced';
  561. }
  562. else {
  563. variable_set("backup_migrate_profile_id", NULL);
  564. }
  565. }
  566. /**
  567. * The menu callback for quick schedules.
  568. */
  569. function backup_migrate_ui_schedule() {
  570. backup_migrate_include('sources', 'schedules', 'profiles');
  571. $out = '';
  572. $sources = backup_migrate_get_sources();
  573. $schedules = backup_migrate_get_schedules();
  574. $out = drupal_get_form('backup_migrate_ui_schedule_form', $sources, $schedules);
  575. return $out;
  576. }
  577. /**
  578. * The quick schedule form.
  579. */
  580. function backup_migrate_ui_schedule_form($form, &$form_state, $sources, $schedules) {
  581. $form['quickschedule'] = array('#tree' => TRUE);
  582. // Add a quick schedule item for each source.
  583. foreach ($sources as $id => $source) {
  584. if (isset($schedules[$id])) {
  585. $schedule = $schedules[$id];
  586. $key = preg_replace('/[^A-Za-z0-9\-_]/', '-', $id);
  587. $form['quickschedule'][$key] = array();
  588. $form['quickschedule'][$key]['#theme'] = 'backup_migrate_ui_quick_schedule_form_inline';
  589. $form['quickschedule'][$key]['id'] = array(
  590. '#type' => 'value',
  591. '#value' => $id,
  592. );
  593. $form['quickschedule'][$key]['enabled'] = array(
  594. '#type' => 'checkbox',
  595. '#title' => t('Automatically backup my @source', array('@source' => $source->get('name'))),
  596. '#default_value' => $schedule->get('enabled'),
  597. );
  598. // $form['quickschedule'][$id]['source_id'] = array(
  599. // '#type' => 'item',
  600. // '#title' => t('Backup Source'),
  601. // '#value' => $source->get('name'),
  602. // );
  603. $form['quickschedule'][$key]['settings'] = array(
  604. '#type' => 'backup_migrate_dependent',
  605. '#dependencies' => array(
  606. 'quickschedule[' . $key . '][enabled]' => TRUE,
  607. ),
  608. );
  609. $options = array(
  610. (60 * 60) => t('Once an hour'),
  611. (60 * 60 * 24) => t('Once a day'),
  612. (60 * 60 * 24 * 7) => t('Once a week'),
  613. );
  614. $period = $schedule->get('period');
  615. if (!isset($options[$period])) {
  616. $options[$period] = $schedule->get('frequency_description');
  617. }
  618. $form['quickschedule'][$key]['settings']['period'] = array(
  619. '#type' => 'select',
  620. '#title' => t('Schedule Frequency'),
  621. '#options' => $options,
  622. '#default_value' => $period,
  623. );
  624. $form['quickschedule'][$key]['settings']['destination'] = _backup_migrate_get_destination_pulldown('scheduled backup', $schedule->get('destination_id'), $schedule->get('copy_destination_id'));
  625. $form['quickschedule'][$key]['settings']['destination']['destination_id']['#parents'] = array(
  626. 'quickschedule',
  627. $key,
  628. 'destination_id',
  629. );
  630. $form['quickschedule'][$key]['settings']['destination']['copy']['#parents'] = array(
  631. 'quickschedule',
  632. $key,
  633. 'copy',
  634. );
  635. $form['quickschedule'][$key]['settings']['destination']['copy_destination']['copy_destination_id']['#parents'] = array(
  636. 'quickschedule',
  637. $key,
  638. 'copy_destination_id',
  639. );
  640. array(
  641. '#type' => 'select',
  642. '#title' => t('Backup Destination'),
  643. '#options' => _backup_migrate_get_destination_form_item_options('scheduled backup'),
  644. '#default_value' => $schedule->get('period'),
  645. );
  646. $form['quickschedule'][$key]['settings']['profile_id'] = array(
  647. '#type' => 'select',
  648. '#title' => t('Settings Profile'),
  649. '#default_value' => $schedule->get('profile_id'),
  650. '#options' => _backup_migrate_get_profile_form_item_options(),
  651. );
  652. $keep = $schedule->get('keep');
  653. $option_keys = array_unique(array(
  654. BACKUP_MIGRATE_SMART_DELETE,
  655. 30,
  656. 100,
  657. 500,
  658. BACKUP_MIGRATE_KEEP_ALL,
  659. $keep,
  660. ));
  661. $options = array();
  662. foreach ($option_keys as $i) {
  663. $options[$i] = $schedule->generate_keep_description($i);
  664. }
  665. $form['quickschedule'][$key]['settings']['keep'] = array(
  666. '#type' => 'select',
  667. '#title' => t('Backup retention'),
  668. '#options' => $options,
  669. '#default_value' => $keep,
  670. );
  671. $form['quickschedule'][$key]['settings']['advanced'] = array(
  672. '#type' => 'markup',
  673. '#markup' => l(t('Advanced Settings.'), BACKUP_MIGRATE_MENU_PATH . '/settings/schedule/edit/' . $id, array('query' => drupal_get_destination())),
  674. );
  675. // Set the parent for the setttings up a level.
  676. foreach (element_children($form['quickschedule'][$key]['settings']) as $child) {
  677. $form['quickschedule'][$key]['settings'][$child]['#parents'] = array(
  678. 'quickschedule',
  679. $key,
  680. $child,
  681. );
  682. }
  683. }
  684. }
  685. if (element_children($form['quickschedule'])) {
  686. $form['submit'] = array(
  687. '#type' => 'submit',
  688. '#value' => t('Save Schedules'),
  689. );
  690. }
  691. $form['advanced'] = array(
  692. '#type' => 'markup',
  693. '#markup' => '<div class="clearblock backup-migrate-footer-message">' . t('For more scheduling options and to add additional schedules, try the <a href="!advancedurl">advanced schedule page</a>.', array('!advancedurl' => url(BACKUP_MIGRATE_MENU_PATH . '/settings/schedule'))) . '</div>',
  694. );
  695. return $form;
  696. }
  697. /**
  698. * Submit the quick schedule form.
  699. */
  700. function backup_migrate_ui_schedule_form_submit($form, &$form_state) {
  701. backup_migrate_include('schedules');
  702. if (user_access('adminsiter backup and migrate')) {
  703. // Override the backups.
  704. foreach ($form_state['values']['quickschedule'] as $key => $values) {
  705. $id = $values['id'];
  706. if ($schedule = backup_migrate_get_schedule($id)) {
  707. $schedule->from_array($values);
  708. $schedule->save();
  709. }
  710. }
  711. drupal_set_message(t('Your schedules have been saved'));
  712. }
  713. }
  714. /**
  715. * The quick backup form.
  716. */
  717. function backup_migrate_ui_manual_quick_backup_form($form, &$form_state, $inline = TRUE) {
  718. backup_migrate_include('profiles', 'destinations', 'sources');
  719. drupal_add_js(drupal_get_path('module', 'backup_migrate') . '/backup_migrate.js');
  720. $form = array();
  721. // Theme the form if we want it inline.
  722. if ($inline) {
  723. $form['#theme'] = 'backup_migrate_ui_manual_quick_backup_form_inline';
  724. }
  725. $form['quickbackup'] = array(
  726. '#type' => 'fieldset',
  727. "#title" => t("Quick Backup"),
  728. "#collapsible" => FALSE,
  729. "#collapsed" => FALSE,
  730. "#tree" => FALSE,
  731. );
  732. $form['quickbackup']['source_id'] = _backup_migrate_get_source_pulldown(variable_get('backup_migrate_source_id', NULL));
  733. $form['quickbackup']['destination'] = _backup_migrate_get_destination_pulldown('manual backup', variable_get('backup_migrate_destination_id', 'download'), variable_get('backup_migrate_copy_destination_id', ''));
  734. $profile_options = _backup_migrate_get_profile_form_item_options();
  735. $form['quickbackup']['profile_id'] = array(
  736. "#type" => "select",
  737. "#title" => t("Settings Profile"),
  738. '#default_value' => variable_get('backup_migrate_profile_id', NULL),
  739. "#options" => $profile_options,
  740. );
  741. $form['quickbackup']['description_enabled'] = array(
  742. '#type' => 'checkbox',
  743. "#title" => t("Add a note to the backup"),
  744. );
  745. $form['quickbackup']['description'] = array(
  746. '#type' => 'backup_migrate_dependent',
  747. '#dependencies' => array(
  748. 'description_enabled' => TRUE,
  749. ),
  750. );
  751. $form['quickbackup']['description']['description'] = array(
  752. '#type' => 'textarea',
  753. "#title" => t("Note"),
  754. '#description' => t('This note will be saved with the backup file and shown on the listing page.'),
  755. );
  756. $form['advanced'] = array(
  757. '#type' => 'markup',
  758. '#markup' => '<div class="clearblock backup-migrate-footer-message">' . t('For more backup options, try the <a href="!advancedurl">advanced backup page</a>.', array('!advancedurl' => url(BACKUP_MIGRATE_MENU_PATH . '/export/advanced'))) . '</div>',
  759. );
  760. $form['quickbackup']['submit'] = array(
  761. '#type' => 'submit',
  762. '#value' => t('Backup now'),
  763. '#weight' => 1,
  764. );
  765. $form['#validate'] = array('backup_migrate_ui_manual_quick_backup_form_validate');
  766. $form['#submit'] = array('backup_migrate_ui_manual_quick_backup_form_submit');
  767. return _backup_migrate_ui_action_form($form, $form_state, 'backup');
  768. }
  769. /**
  770. * Validate the quick backup form.
  771. */
  772. function backup_migrate_ui_manual_quick_backup_form_validate($form, &$form_state) {
  773. backup_migrate_include('profiles', 'destinations');
  774. if ($form_state['values']['source_id'] == $form_state['values']['destination_id']) {
  775. form_set_error('destination_id', t('A source cannot be backed up to itself. Please pick a different destination for this backup.'));
  776. }
  777. // Wrap profile_id for prevent Undefined index notice.
  778. $profile_id = isset($form_state['values']['profile_id']) ? $form_state['values']['profile_id'] : NULL;
  779. // For a quick backup use the default settings.
  780. $settings = _backup_migrate_profile_saved_default_profile($profile_id);
  781. // Set the destination to the one chosen in the pulldown.
  782. $settings->destination_id = array($form_state['values']['destination_id']);
  783. $settings->source_id = $form_state['values']['source_id'];
  784. // Add the second destination.
  785. if (!empty($form_state['values']['copy_destination_id'])) {
  786. $settings->destination_id[] = $form_state['values']['copy_destination_id'];
  787. }
  788. // Add the note.
  789. if (!empty($form_state['values']['description_enabled'])) {
  790. $settings->filters['utils_description'] = $form_state['values']['description'];
  791. }
  792. $form_state['values']['settings'] = $settings;
  793. }
  794. /**
  795. * Submit the quick backup form.
  796. */
  797. function backup_migrate_ui_manual_quick_backup_form_submit($form, &$form_state) {
  798. backup_migrate_include('profiles', 'destinations');
  799. if (user_access('perform backup') && !empty($form_state['values']['settings'])) {
  800. // Save the settings for next time.
  801. variable_set("backup_migrate_source_id", $form_state['values']['source_id']);
  802. variable_set("backup_migrate_destination_id", $form_state['values']['destination_id']);
  803. variable_set("backup_migrate_copy_destination_id", $form_state['values']['copy_destination_id']);
  804. variable_set("backup_migrate_profile_id", $form_state['values']['profile_id']);
  805. // Do the backup.
  806. backup_migrate_ui_manual_backup_perform($form_state['values']['settings']);
  807. }
  808. $form_state['redirect'] = BACKUP_MIGRATE_MENU_PATH;
  809. }
  810. /**
  811. * Theme the quick backup form.
  812. */
  813. function theme_backup_migrate_ui_manual_quick_backup_form_inline($form) {
  814. drupal_add_css(drupal_get_path('module', 'backup_migrate') . '/backup_migrate.css');
  815. $form = $form['form'];
  816. // Remove the titles so that the pulldowns can be displayed inline.
  817. unset($form['quickbackup']['source_id']['#title']);
  818. unset($form['quickbackup']['destination']['destination_id']['#title']);
  819. unset($form['quickbackup']['destination']['destination_id']['#description']);
  820. unset($form['quickbackup']['profile_id']['#title']);
  821. unset($form['quickbackup']['destination']['copy_destination']['copy_destination_id']['#title']);
  822. $replacements = array(
  823. '!from' => drupal_render($form['quickbackup']['source_id']),
  824. '!to' => drupal_render($form['quickbackup']['destination']['destination_id']),
  825. '!profile' => drupal_render($form['quickbackup']['profile_id']),
  826. // '!submit' => drupal_render($form['quickbackup']['submit']),
  827. );
  828. $form['quickbackup']['markup'] = array(
  829. '#type' => 'markup',
  830. "#prefix" => '<div class="container-inline backup-migrate-inline">',
  831. "#suffix" => '</div>',
  832. '#markup' => t('Backup my !from to !to using !profile', $replacements),
  833. );
  834. $replacements = array(
  835. '!to' => drupal_render($form['quickbackup']['destination']['copy_destination']['copy_destination_id']),
  836. );
  837. $form['quickbackup']['destination']['copy']["#prefix"] = '<div class="container-inline backup-migrate-inline">';
  838. $form['quickbackup']['destination']['copy']["#suffix"] = $replacements['!to'] . '</div>';
  839. // This is not good translation practice as it relies on the structure of
  840. // English. If I add the pulldown to the label, howerver, the box toggles
  841. // when the pulldown is clicked.
  842. // $form['quickbackup']['destination']['copy']['#title'] = t('Save a copy to');
  843. unset($form['quickbackup']['source_id']);
  844. unset($form['quickbackup']['destination']['destination_id']);
  845. unset($form['quickbackup']['destination']['copy_destination']);
  846. unset($form['quickbackup']['profile_id']);
  847. // unset($form['quickbackup']['submit']);
  848. return drupal_render_children($form);
  849. }
  850. /**
  851. * Theme the quick schedule form.
  852. */
  853. function theme_backup_migrate_ui_quick_schedule_form_inline($form) {
  854. drupal_add_js(drupal_get_path('module', 'backup_migrate') . '/backup_migrate.js');
  855. drupal_add_css(drupal_get_path('module', 'backup_migrate') . '/backup_migrate.css');
  856. $form = $form['form'];
  857. // Remove the titles so that the pulldowns can be displayed inline.
  858. unset($form['settings']['source_id']['#title']);
  859. unset($form['settings']['period']['#title']);
  860. unset($form['settings']['destination']['destination_id']['#title']);
  861. unset($form['settings']['destination']['destination_id']['#description']);
  862. unset($form['settings']['profile_id']['#title']);
  863. unset($form['settings']['destination']['copy_destination']['copy_destination_id']['#title']);
  864. unset($form['settings']['profile_id']['#title']);
  865. unset($form['settings']['keep']['#title']);
  866. unset($form['settings']['keep']['#field_prefix']);
  867. unset($form['settings']['keep']['#field_suffix']);
  868. $replacements = array(
  869. '!from' => drupal_render($form['settings']['source_id']),
  870. '!to' => drupal_render($form['settings']['destination']['destination_id']),
  871. '!frequency' => drupal_render($form['settings']['period']),
  872. '!profile' => drupal_render($form['settings']['profile_id']),
  873. '!keep' => drupal_render($form['settings']['keep']),
  874. );
  875. $form['settings'] = array(
  876. 'inline' => array(
  877. '#type' => 'markup',
  878. "#prefix" => '<div class="container-inline backup-migrate-quickschedule">',
  879. "#suffix" => '</div>',
  880. '#markup' => t('Backup !frequency to !to using !profile keeping !keep', $replacements),
  881. ),
  882. ) + $form['settings'];
  883. $replacements = array(
  884. '!to' => drupal_render($form['settings']['destination']['copy_destination']['copy_destination_id']),
  885. );
  886. $form['settings']['destination']['copy']["#prefix"] = '<div class="container-inline backup-migrate-inline">';
  887. $form['settings']['destination']['copy']["#suffix"] = $replacements['!to'] . '</div>';
  888. unset($form['settings']['period']);
  889. unset($form['settings']['destination']['destination_id']);
  890. unset($form['settings']['destination']['copy_destination']);
  891. unset($form['settings']['profile_id']);
  892. unset($form['settings']['keep']);
  893. return drupal_render_children($form);
  894. }
  895. /**
  896. * Theme the dependent form section.
  897. */
  898. function theme_backup_migrate_dependent($vars) {
  899. $element = $vars['element'];
  900. drupal_add_js(drupal_get_path('module', 'backup_migrate') . '/backup_migrate.js');
  901. drupal_add_css(drupal_get_path('module', 'backup_migrate') . '/backup_migrate.css');
  902. $id = drupal_html_id(implode('-', $element['#array_parents']));
  903. $settings = array(
  904. 'backup_migrate' => array(
  905. 'dependents' => array($id => array('dependent' => $id, 'dependencies' => $element['#dependencies'])),
  906. ),
  907. );
  908. drupal_add_js($settings, 'setting');
  909. return '<div id="edit-' . $id . '" class="backup-migrate-form-dependent">' . drupal_render_children($element) . '</div>';
  910. }
  911. /**
  912. * Theme file list form widget.
  913. */
  914. function theme_backup_migrate_file_list($vars) {
  915. $element = $vars['element'];
  916. // Render the radios so they can be placed in the file list rendering.
  917. $vars['element']['#display_options']['form_elements'] = array();
  918. if (isset($vars['element']['files']) && count($vars['element']['files'])) {
  919. foreach (element_children($vars['element']['files']) as $id) {
  920. $vars['element']['files'][$id]['#title'] = '';
  921. $vars['element']['#display_options']['form_elements'][$id] = render($vars['element']['files'][$id]);
  922. }
  923. }
  924. $out = _backup_migrate_ui_destination_display_file_list($vars['element']['#files'], $vars['element']['#display_options']);
  925. return $out;
  926. }
  927. /**
  928. * Theme the dependent form section.
  929. */
  930. function theme_backup_migrate_group($vars) {
  931. $output = '';
  932. if (!empty($vars['title'])) {
  933. $output .= '<h2 class="backup-migrate-group-title">' . $vars['title'] . '</h2>';
  934. }
  935. if (!empty($vars['description'])) {
  936. $output .= '<div class="backup-migrate-group-description backup-migrate-description description">' . $vars['description'] . '</div>';
  937. }
  938. $output .= '<div class="backup-migrate-group-body">' . $vars['body'] . '</div>';
  939. $output = '<div class="backup-migrate-group">' . $output . '</div>';
  940. return $output;
  941. }
  942. /**
  943. * The backup/export form.
  944. */
  945. function backup_migrate_ui_manual_backup_form($form, &$form_state, $profile) {
  946. drupal_add_js(drupal_get_path('module', 'backup_migrate') . '/backup_migrate.js', array('type' => 'module', 'scope' => 'footer'));
  947. $form = array();
  948. $form += _backup_migrate_get_source_form('db');
  949. $form_state['profile'] = $profile;
  950. $form['machine_name'] = array(
  951. "#type" => "value",
  952. '#default_value' => $profile->get_id(),
  953. );
  954. $form['storage'] = array(
  955. "#type" => "value",
  956. '#default_value' => $profile->storage,
  957. );
  958. $form['destination'] = array(
  959. "#type" => "fieldset",
  960. "#title" => t("Backup Destination"),
  961. "#collapsible" => TRUE,
  962. "#collapsed" => FALSE,
  963. "#tree" => FALSE,
  964. "#description" => t("Choose where the backup file will be saved. Backup files contain sensitive data, so be careful where you save them. Select 'Download' to download the file to your desktop."),
  965. // '#weight' => 70,
  966. );
  967. $form['destination']['destination'] = _backup_migrate_get_destination_pulldown('manual backup', variable_get('backup_migrate_destination_id', 'download'));
  968. $form += _backup_migrate_ui_backup_settings_form($profile);
  969. // Make the source settings dependent on the source selected. Except don't
  970. // because it looks super weird.
  971. // @code
  972. // foreach (element_children($form['sources']) as $source) {
  973. // $form['sources'][$source] = array(
  974. // '#type' => 'backup_migrate_dependent',
  975. // '#dependencies' => array(
  976. // 'source_id' => $source,
  977. // ),
  978. // 'settings' => $form['sources'][$source]
  979. // );
  980. // }
  981. // @endcode
  982. if (user_access('administer backup and migrate')) {
  983. $form['save_settings'] = array(
  984. "#type" => "checkbox",
  985. "#title" => t('Save these settings.'),
  986. "#default_value" => FALSE,
  987. '#weight' => 80,
  988. );
  989. $form['save_options'] = array(
  990. '#type' => 'backup_migrate_dependent',
  991. '#dependencies' => array(
  992. 'save_settings' => TRUE,
  993. ),
  994. '#weight' => 90,
  995. );
  996. $name = array(
  997. '#default_value' => $profile->get('name'),
  998. '#type' => 'textfield',
  999. '#title' => t('Save the settings as'),
  1000. );
  1001. if ($profile->get_id()) {
  1002. $form['save_options']['create_new'] = array(
  1003. '#default_value' => $profile->get('name'),
  1004. '#type' => 'radios',
  1005. '#default_value' => 0,
  1006. '#options' => array(
  1007. 0 => t("Replace the '%profile' profile", array('%profile' => $profile->get('name'))),
  1008. 1 => t('Create new profile'),
  1009. ),
  1010. );
  1011. $name["#title"] = t('Profile name');
  1012. $name["#description"] = t("This will be the name of your new profile if you select 'Create new profile' otherwise it will become the name of the '%profile' profile.", array('%profile' => $profile->get('name')));
  1013. }
  1014. else {
  1015. $name["#title"] = t('Save the settings as');
  1016. $name["#description"] = t('Pick a name for the settings. Your settings will be saved as a profile and will appear in the <a href="!url">Profiles Tab</a>.', array('!url' => url(BACKUP_MIGRATE_MENU_PATH . '/profile')));
  1017. $name["#default_value"] = t('Untitled Profile');
  1018. }
  1019. $form['save_options']['name'] = $name;
  1020. $form['save_options'][] = array(
  1021. '#type' => 'submit',
  1022. '#value' => t('Save Without Backing Up'),
  1023. );
  1024. }
  1025. $form['#validate'][] = 'backup_migrate_ui_manual_quick_backup_form_validate';
  1026. $form['#submit'][] = 'backup_migrate_ui_manual_backup_form_submit';
  1027. $form['submit'] = array(
  1028. '#type' => 'submit',
  1029. '#value' => t('Backup now'),
  1030. '#weight' => 100,
  1031. );
  1032. return _backup_migrate_ui_action_form($form, $form_state, 'backup');
  1033. }
  1034. /**
  1035. * Submits the form.
  1036. *
  1037. * Save the values as defaults if desired and output the backup file.
  1038. */
  1039. function backup_migrate_ui_manual_backup_form_submit($form, &$form_state) {
  1040. // Create a profile with the given settings.
  1041. $profile = $form_state['profile'];
  1042. $profile->from_array($form_state['values']);
  1043. // Save the settings profile if the save box is checked.
  1044. if ($form_state['values']['save_settings'] && user_access('administer backup and migrate')) {
  1045. if (@$form_state['values']['create_new']) {
  1046. // Reset the id and storage so a new item will be saved.
  1047. $profile->set_id(NULL);
  1048. $profile->storage = BACKUP_MIGRATE_STORAGE_NONE;
  1049. }
  1050. $profile->save();
  1051. variable_set("backup_migrate_profile_id", $profile->get_id());
  1052. variable_set("backup_migrate_destination_id", $form_state['values']['destination_id']);
  1053. }
  1054. // Perform the actual backup if that is what was selected.
  1055. if ($form_state['values']['op'] !== t('Save Without Backing Up') && user_access('perform backup')) {
  1056. backup_migrate_ui_manual_backup_perform($profile);
  1057. }
  1058. $form_state['redirect'] = BACKUP_MIGRATE_MENU_PATH . "/export/advanced";
  1059. }
  1060. /**
  1061. * Alters a B&M action (backup/restore) form to allow for filters to add steps.
  1062. */
  1063. function _backup_migrate_ui_action_form($form, &$form_state, $op = 'backup') {
  1064. $form['operation'] = array('#type' => 'value', '#value' => $op);
  1065. // If the form has been submitted at least once then see if there are
  1066. // additional form elements to add.
  1067. if (!empty($form_state['storage']['values']) && !empty($form_state['values']['settings'])) {
  1068. $page = _backup_migrate_filter_before_action_form($form_state['values']['settings'], $op);
  1069. if (!empty($page)) {
  1070. $page['#validate'] = $form['#validate'];
  1071. $page['#submit'] = $form['#submit'];
  1072. $page['submit'] = array(
  1073. '#type' => 'submit',
  1074. '#value' => isset($form_state['values']['op']) ? check_plain($form_state['values']['op']) : t('Continue'),
  1075. '#weight' => 1,
  1076. );
  1077. $form = $page;
  1078. }
  1079. }
  1080. // Add some pre and post processing functions to validate and submit.
  1081. array_unshift($form['#validate'], 'backup_migrate_ui_action_form_pre_validate');
  1082. array_push($form['#validate'], 'backup_migrate_ui_action_form_post_validate');
  1083. array_unshift($form['#submit'], 'backup_migrate_ui_action_form_pre_submit');
  1084. return $form;
  1085. }
  1086. /**
  1087. * Allows filters, sources and destinations to present additional form elements.
  1088. *
  1089. * The additional form elements will be shown to the user before an action is
  1090. * submitted.
  1091. */
  1092. function backup_migrate_ui_action_form_pre_validate($form, &$form_state) {
  1093. if (!empty($form_state['storage']['values'])) {
  1094. $form_state['values'] += $form_state['storage']['values'];
  1095. }
  1096. }
  1097. /**
  1098. * Allows filters, sources and destinations to present additional form elements.
  1099. *
  1100. * The additional form elements will be shown to the user before an action is
  1101. * submitted.
  1102. */
  1103. function backup_migrate_ui_action_form_post_validate($form, &$form_state) {
  1104. backup_migrate_include('filters');
  1105. if (!empty($form_state['values']['settings'])) {
  1106. // See if there are extra form items to be processed.
  1107. $form = _backup_migrate_filter_before_action_form($form_state['values']['settings'], $form_state['values']['operation']);
  1108. if ($form) {
  1109. // First time through the form simply rebuild the form to show the
  1110. // additional fields.
  1111. if (empty($form_state['storage']['values'])) {
  1112. $form_state['storage']['values'] = $form_state['values'];
  1113. $form_state['rebuild'] = TRUE;
  1114. }
  1115. else {
  1116. backup_migrate_filters_before_action_form_validate($form_state['values']['settings'], $form_state['values']['operation'], $form, $form_state);
  1117. }
  1118. }
  1119. }
  1120. }
  1121. /**
  1122. * Allows filters, sources and destinations to present additional form elements.
  1123. *
  1124. * The additional form elements will be shown to the user before an action is
  1125. * submitted.
  1126. */
  1127. function backup_migrate_ui_action_form_pre_submit($form, &$form_state) {
  1128. if (!empty($form_state['storage']['values']) && !empty($form_state['values']['settings'])) {
  1129. // Check if there are any form elements to submit.
  1130. backup_migrate_filters_before_action_form_submit($form_state['values']['settings'], $form_state['values']['operation'], $form, $form_state);
  1131. // Clear the storage so the form can continue as normal.
  1132. unset($form_state['storage']);
  1133. }
  1134. }
  1135. /**
  1136. * Retrieves form items to be filled out before an action can continue.
  1137. *
  1138. * @return array
  1139. * An array of form items.
  1140. */
  1141. function _backup_migrate_filter_before_action_form($settings, $op) {
  1142. $form = array();
  1143. $form = backup_migrate_filters_before_action_form($settings, $op);
  1144. return $form;
  1145. }
  1146. /**
  1147. * Perform an actual manual backup and tell the user of the progress.
  1148. */
  1149. function backup_migrate_ui_manual_backup_perform($settings) {
  1150. // Peform the actual backup.
  1151. backup_migrate_perform_backup($settings);
  1152. }
  1153. /**
  1154. * The restore/import upload page.
  1155. */
  1156. function backup_migrate_ui_manual_restore() {
  1157. return drupal_get_form('backup_migrate_ui_manual_restore_form');
  1158. }
  1159. /**
  1160. * The restore/import upload form.
  1161. */
  1162. function backup_migrate_ui_manual_restore_form() {
  1163. backup_migrate_include('filters', 'destinations', 'sources');
  1164. drupal_set_message(t('Restoring will delete some or all of your data and cannot be undone. <strong>Always test your backups on a non-production server!</strong>'), 'warning', FALSE);
  1165. $form = array();
  1166. $from_default = (isset($_GET['page']) || isset($_GET['sort'])) ? 'saved' : 'upload';
  1167. $form['backup_migrate_restore_from_upload'] = array(
  1168. "#type" => "radio",
  1169. "#title" => t('Restore from an uploaded file'),
  1170. '#return_value' => 'upload',
  1171. '#parents' => array('from'),
  1172. '#default_value' => $from_default,
  1173. );
  1174. $form['backup_migrate_restore_upload'] = array(
  1175. '#type' => 'backup_migrate_dependent',
  1176. '#dependencies' => array(
  1177. 'from' => 'upload',
  1178. ),
  1179. );
  1180. $form['backup_migrate_restore_upload']['backup_migrate_restore_upload'] = array(
  1181. '#title' => t('Upload a Backup File'),
  1182. '#type' => 'file',
  1183. '#description' => t("Upload a backup file created by Backup and Migrate. For other database or file backups please use another tool for import. Max file size: %size", array("%size" => format_size(file_upload_max_size()))),
  1184. );
  1185. $form['backup_migrate_restore_from_saved'] = array(
  1186. "#type" => "radio",
  1187. "#title" => t('Restore from a saved backup'),
  1188. '#return_value' => 'saved',
  1189. '#parents' => array('from'),
  1190. '#default_value' => $from_default,
  1191. );
  1192. $form['backup_migrate_restore_saved'] = array(
  1193. '#type' => 'backup_migrate_dependent',
  1194. '#dependencies' => array(
  1195. 'from' => 'saved',
  1196. ),
  1197. );
  1198. list($files, $fetch_time, $remote) = backup_migrate_get_saved_backups();
  1199. $keys = array_keys($files);
  1200. $default = reset($keys);
  1201. $form['backup_migrate_restore_saved']['file'] = array(
  1202. '#type' => 'backup_migrate_file_list',
  1203. '#display_options' => array(
  1204. 'limit' => 5,
  1205. 'pager' => FALSE,
  1206. 'more' => BACKUP_MIGRATE_MENU_PATH . '/backups',
  1207. 'operations' => FALSE,
  1208. ),
  1209. '#default_value' => $default,
  1210. '#files' => $files,
  1211. );
  1212. $sources = _backup_migrate_get_source_form_item_options();
  1213. if (count($sources) > 1) {
  1214. $form['source_id'] = array(
  1215. "#type" => "select",
  1216. "#title" => t("Restore to"),
  1217. "#options" => $sources,
  1218. "#description" => t("Choose where to restore to. Any database destinations you have created and any databases specified in your settings.php can be restored to."),
  1219. "#default_value" => 'db',
  1220. );
  1221. }
  1222. else {
  1223. $form['source_id'] = array(
  1224. "#type" => "value",
  1225. "#value" => 'db',
  1226. );
  1227. }
  1228. $form = array_merge_recursive($form, backup_migrate_filters_settings_form(backup_migrate_filters_settings_default('restore'), 'restore'));
  1229. // Add the advanced fieldset if there are any fields in it.
  1230. if (isset($form['advanced'])) {
  1231. $form['advanced']['#type'] = 'fieldset';
  1232. $form['advanced']['#title'] = t('Advanced Options');
  1233. $form['advanced']['#collapsed'] = TRUE;
  1234. $form['advanced']['#collapsible'] = TRUE;
  1235. }
  1236. $form['submit'] = array(
  1237. '#type' => 'submit',
  1238. '#value' => t('Restore now'),
  1239. );
  1240. if (user_access('access backup files')) {
  1241. $form[] = array(
  1242. '#type' => 'markup',
  1243. '#markup' => t('<p>Or you can restore one of the files in your <a href="!url">saved backup destinations.</a></p>', array("!url" => url(BACKUP_MIGRATE_MENU_PATH . '/backups'))),
  1244. );
  1245. }
  1246. $form['#attributes'] = array('enctype' => 'multipart/form-data');
  1247. return $form;
  1248. }
  1249. /**
  1250. * The restore submit. Do the restore.
  1251. */
  1252. function backup_migrate_ui_manual_restore_form_submit($form, &$form_state) {
  1253. backup_migrate_include('destinations');
  1254. $validators = array('file_validate_extensions' => array('gz zip sql mysql bz bz2'));
  1255. $values = $form_state['values'];
  1256. if ($values['from'] == 'upload' && $file = file_save_upload('backup_migrate_restore_upload', $validators)) {
  1257. backup_migrate_perform_restore('upload', $file->uri, $values);
  1258. }
  1259. elseif ($values['from'] == 'saved' && $file = $values['file']) {
  1260. list($destination, $file_id) = explode('/', $file, 2);
  1261. $file =
  1262. backup_migrate_perform_restore($destination, $file_id, $form_state['values']);
  1263. }
  1264. $form_state['redirect'] = BACKUP_MIGRATE_MENU_PATH . '/restore';
  1265. }
  1266. /**
  1267. * Convert an item to an 'exportable'.
  1268. */
  1269. function backup_migrate_ui_export_form($form, &$form_state, $item) {
  1270. if ($item && function_exists('ctools_var_export')) {
  1271. $code = ctools_var_export($item);
  1272. $form = ctools_export_form($form_state, $code);
  1273. return $form;
  1274. }
  1275. return array();
  1276. }
  1277. /**
  1278. * List the previously created backups from across multiple destinations.
  1279. */
  1280. function backup_migrate_ui_saved_backups() {
  1281. backup_migrate_include('profiles', 'destinations', 'sources');
  1282. drupal_add_css(drupal_get_path('module', 'backup_migrate') . '/backup_migrate.css');
  1283. $remote = FALSE;
  1284. $now = $fetch_time = time();
  1285. $refresh = isset($_GET['refresh']);
  1286. $out = '';
  1287. list($files, $fetch_time, $remote) = backup_migrate_get_saved_backups($refresh);
  1288. // Redirect if refreshing instead of displaying.
  1289. if ($refresh) {
  1290. drupal_goto($_GET['q']);
  1291. }
  1292. $fetch = $out = '';
  1293. // Get the fetch link.
  1294. if ($fetch_time < $now) {
  1295. $fetch = '<div class="description">' . t('This listing was fetched !time ago. !refresh', array(
  1296. '!time' => format_interval($now - $fetch_time, 1),
  1297. '!refresh' => l(t('fetch now'), $_GET['q'], array(
  1298. 'query' => array('refresh' => 'true'),
  1299. )),
  1300. )) . '</div>';
  1301. }
  1302. // Render the list.
  1303. $out .= $fetch;
  1304. $out .= _backup_migrate_ui_destination_display_file_list($files, array('limit' => 20, 'pager' => TRUE));
  1305. $out .= $fetch;
  1306. // Add a remote backups warning if there are no remote backups.
  1307. if (!$remote && user_access('administer backup and migrate')) {
  1308. drupal_set_message(
  1309. t('You do not have any offsite backups. For the highest level of protection, <a href="!addurl">create an offsite destination</a> or <a href="!nsurl">set up NodeSquirrel</a> to save your backups to.',
  1310. array(
  1311. '!addurl' => url(BACKUP_MIGRATE_MENU_PATH . '/settings/destination/add'),
  1312. '!nsurl' => url(BACKUP_MIGRATE_MENU_PATH . '/nodesquirrel'),
  1313. )), 'warning', FALSE);
  1314. }
  1315. return $out;
  1316. }
  1317. /**
  1318. * List the previously created backups from across multiple destinations.
  1319. */
  1320. function backup_migrate_get_saved_backups($refresh = FALSE) {
  1321. backup_migrate_include('profiles', 'destinations', 'sources');
  1322. drupal_add_css(drupal_get_path('module', 'backup_migrate') . '/backup_migrate.css');
  1323. $remote = FALSE;
  1324. $now = $fetch_time = time();
  1325. $out = '';
  1326. $files = $sort = array();
  1327. foreach (backup_migrate_get_destinations('list files') as $destination) {
  1328. $dest_id = $destination->get_id();
  1329. $dest_files = $destination->list_files();
  1330. if ($dest_files) {
  1331. $location = l($destination->get_name(), BACKUP_MIGRATE_MENU_PATH . '/settings/destination/list/files/' . $dest_id);
  1332. if ($destination->get('remote')) {
  1333. $location .= $destination->get('remote') ? ' <strong class="backup-migrate-tag">(' . t('offsite') . ')</strong>' : '';
  1334. $remote = TRUE;
  1335. }
  1336. // Refresh the destination.
  1337. if ($refresh) {
  1338. $destination->file_cache_clear();
  1339. }
  1340. // Get the file fetch time.
  1341. if ($destination->cache_files && $destination->fetch_time) {
  1342. $fetch_time = min($destination->fetch_time, $fetch_time);
  1343. }
  1344. // Add the files from this destination to the full list.
  1345. foreach ($dest_files as $id => $file) {
  1346. $dest_files[$id]->file_info['bam_other_safe'][t('Location:')] = $location;
  1347. $sort[$dest_id . '/' . $id] = $file->info('filetime');
  1348. $files[$dest_id . '/' . $id] = $file;
  1349. }
  1350. }
  1351. }
  1352. array_multisort($sort, SORT_DESC, $files);
  1353. return array($files, $fetch_time, $remote);
  1354. }
  1355. /**
  1356. * Helper function for byte conversion.
  1357. */
  1358. function backup_migrate_to_bytes($from) {
  1359. $number = substr($from, 0, -1);
  1360. switch (strtoupper(substr($from, -1))) {
  1361. case "M":
  1362. return $number * pow(1024, 2);
  1363. break;
  1364. case "G":
  1365. return $number * pow(1024, 3);
  1366. break;
  1367. default:
  1368. return $number * pow(1024, 2);
  1369. }
  1370. }
  1371. /**
  1372. * Perform a backup with the given settings.
  1373. */
  1374. function backup_migrate_perform_backup(&$settings) {
  1375. backup_migrate_include('destinations', 'files', 'filters');
  1376. timer_start('backup_migrate_backup');
  1377. _backup_migrate_set_timeout();
  1378. // Conditionally increase the memory limit
  1379. // Get the current php memory limit.
  1380. $php_memory_limit = ini_get('memory_limit');
  1381. // Convert the php memory limit to bytes.
  1382. $php_memory_limit_bytes = backup_migrate_to_bytes($php_memory_limit);
  1383. // Get the current Backup Migrate memory limit.
  1384. // Hint: To further increase the memory limit used by Backup Migrate add one
  1385. // of the following line to the settings.php file:
  1386. // * $conf['backup_migrate_memory_limit'] = '512M';
  1387. // * $conf['backup_migrate_memory_limit'] = '1G';
  1388. $backup_migrate_memory_limit = variable_get('backup_migrate_memory_limit', '256M');
  1389. // Convert the Backup Migrate memory limit to bytes.
  1390. $backup_migrate_memory_limit_bytes = backup_migrate_to_bytes($backup_migrate_memory_limit);
  1391. // If not in 'safe mode', increase the memory limit.
  1392. if (!ini_get('safe_mode') && $php_memory_limit_bytes < $backup_migrate_memory_limit_bytes) {
  1393. // Ensure ini_set() is not disabled.
  1394. if (!strpos(ini_get('disable_functions'), 'ini_set')) {
  1395. // Convert bytes to megabytes, add the 'M' unit.
  1396. $backup_migrate_memory_limit = round($backup_migrate_memory_limit_bytes / 1024 / 1024) . 'M';
  1397. // Update the php memory limit.
  1398. ini_set('memory_limit', $backup_migrate_memory_limit);
  1399. }
  1400. }
  1401. if (variable_get('backup_migrate_verbose')) {
  1402. _backup_migrate_message("Memory limit: %limit", array('%limit' => ini_get('memory_limit')), 'success');
  1403. }
  1404. // Confirm the destinations are valid.
  1405. $valid = backup_migrate_destination_confirm_destination($settings);
  1406. if (!$valid) {
  1407. backup_migrate_backup_fail("Could not run backup because a there was no valid destination to save to.", array(), $settings);
  1408. return FALSE;
  1409. }
  1410. // Create the temporary file object to be backed up into.
  1411. $filename = _backup_migrate_construct_filename($settings);
  1412. $file = new backup_file(array('filename' => $filename));
  1413. if (!$file) {
  1414. backup_migrate_backup_fail("Could not run backup because a temporary file could not be created.", array(), $settings);
  1415. return FALSE;
  1416. }
  1417. // Register shutdown callback to deal with timeouts.
  1418. register_shutdown_function('backup_migrate_shutdown', $settings);
  1419. if (variable_get('backup_migrate_verbose')) {
  1420. watchdog('backup_migrate', "Starting backup: %file", array('%file' => $file->filename()));
  1421. }
  1422. $file = backup_migrate_filters_backup($file, $settings);
  1423. if (!$file) {
  1424. if (_backup_migrate_check_timeout()) {
  1425. backup_migrate_backup_fail('Could not complete the backup because the script timed out. Try increasing your PHP <a href="!url">max_execution_time setting</a>.', array('!url' => 'http://www.php.net/manual/en/info.configuration.php#ini.max-execution-time'), $settings);
  1426. }
  1427. else {
  1428. backup_migrate_backup_fail("Could not complete the backup.", array(), $settings);
  1429. }
  1430. return FALSE;
  1431. }
  1432. if (variable_get('backup_migrate_verbose')) {
  1433. watchdog('backup_migrate', "Preparing to save backup: %file", array('%file' => $file->filename()));
  1434. }
  1435. $file = backup_migrate_destination_save_file($file, $settings);
  1436. if (!$file) {
  1437. backup_migrate_backup_fail("Could not run backup because the file could not be saved to the destination.", array(), $settings);
  1438. return FALSE;
  1439. }
  1440. if (variable_get('backup_migrate_verbose')) {
  1441. watchdog('backup_migrate', "Backup succeeded: %file", array('%file' => $file->filename()));
  1442. }
  1443. // Backup succeeded.
  1444. $time = timer_stop('backup_migrate_backup');
  1445. $message = '%source backed up successfully to %file (%size) in destination %dest in !time. !action';
  1446. $params = array(
  1447. '%file' => $filename,
  1448. '%dest' => $settings->get_destination_name(),
  1449. '%source' => $settings->get_source_name(),
  1450. '%size' => format_size($file->filesize()),
  1451. '!time' => backup_migrate_format_elapsed_time($time['time']),
  1452. '!action' => !empty($settings->performed_action) ? $settings->performed_action : '',
  1453. );
  1454. if (($destination = $settings->get_destination()) && ($links = $destination->get_file_links($file->file_id()))) {
  1455. $params['!links'] = implode(", ", $links);
  1456. }
  1457. backup_migrate_backup_succeed($message, $params, $settings);
  1458. return $file;
  1459. }
  1460. /**
  1461. * Formats an elapsed time given in milleseconds.
  1462. */
  1463. function backup_migrate_format_elapsed_time($time) {
  1464. $ms = t('!time ms', array('!time' => $time));
  1465. if ($time < 1000) {
  1466. return $ms;
  1467. }
  1468. return '<abbr title="' . $ms . '">' . format_interval($time / 1000) . '</abbr>';
  1469. }
  1470. /**
  1471. * Restore from a file in the given destination.
  1472. */
  1473. function backup_migrate_perform_restore($destination_id, $file, $settings = array()) {
  1474. backup_migrate_include('files', 'filters');
  1475. timer_start('backup_migrate_restore');
  1476. _backup_migrate_set_timeout();
  1477. // Make the settings into a default profile.
  1478. if (!is_object($settings)) {
  1479. $settings = backup_migrate_crud_create_item('profile', $settings);
  1480. $settings->source_id = empty($settings->source_id) ? 'db' : $settings->source_id;
  1481. }
  1482. // Register shutdown callback.
  1483. register_shutdown_function('backup_migrate_shutdown', $settings);
  1484. if (!is_object($file)) {
  1485. // Load the file from the destination.
  1486. $file = backup_migrate_destination_get_file($destination_id, $file);
  1487. if (!$file) {
  1488. _backup_migrate_message("Could not restore because the file could not be loaded from the destination.", array(), 'error');
  1489. backup_migrate_cleanup();
  1490. return FALSE;
  1491. }
  1492. }
  1493. // Filter the file and perform the restore.
  1494. $file = backup_migrate_filters_restore($file, $settings);
  1495. if (!$file) {
  1496. if (_backup_migrate_check_timeout()) {
  1497. backup_migrate_restore_fail('Could not perform the restore because the script timed out. Try increasing your PHP <a href="!url">max_execution_time setting</a>.', array('!url' => 'http://www.php.net/manual/en/info.configuration.php#ini.max-execution-time'), 'error');
  1498. }
  1499. else {
  1500. backup_migrate_restore_fail("Could not perform the restore.", array(), 'error');
  1501. }
  1502. backup_migrate_cleanup();
  1503. return FALSE;
  1504. }
  1505. $time = timer_stop('backup_migrate_restore');
  1506. if ($file) {
  1507. $destination = backup_migrate_get_destination($destination_id);
  1508. $message = '%source restored from %dest file %file in !time. !action';
  1509. $params = array(
  1510. '%file' => $file->filename(),
  1511. '%source' => $settings->get_source_name(),
  1512. '%dest' => $destination->get_name(),
  1513. '!time' => backup_migrate_format_elapsed_time($time['time']),
  1514. '!action' => !empty($settings->performed_action) ? $settings->performed_action : '',
  1515. );
  1516. if ($destination && $destination->op('list files')) {
  1517. $path = $destination->get_settings_path();
  1518. $params['!links'] = t('<a href="!restoreurl">Restore again</a>', array('!restoreurl' => url($path . '/list/restorefile/' . $destination->get_id() . '/' . $file->file_id())));
  1519. }
  1520. backup_migrate_restore_succeed($message, $params, $settings);
  1521. }
  1522. // Delete any temp files we've created.
  1523. backup_migrate_cleanup();
  1524. // No errors. Return the file.
  1525. return $file;
  1526. }
  1527. /**
  1528. * Clean up when a backup operation fails.
  1529. */
  1530. function backup_migrate_backup_fail($message, $params, $settings) {
  1531. backup_migrate_include('files', 'filters');
  1532. _backup_migrate_message($message, $params, 'error');
  1533. backup_migrate_cleanup();
  1534. backup_migrate_filters_invoke_all('backup_fail', $settings, $message, $params);
  1535. return FALSE;
  1536. }
  1537. /**
  1538. * Clean up when a backup operation succeeds.
  1539. */
  1540. function backup_migrate_backup_succeed($message, $params, $settings) {
  1541. backup_migrate_include('filters', 'files');
  1542. _backup_migrate_message($message, $params, 'success');
  1543. backup_migrate_cleanup();
  1544. backup_migrate_filters_invoke_all('backup_succeed', $settings, $message, $params);
  1545. return FALSE;
  1546. }
  1547. /**
  1548. * Clean up when a restore operation fails.
  1549. */
  1550. function backup_migrate_restore_fail($message, $params, $settings) {
  1551. backup_migrate_include('files', 'filters');
  1552. _backup_migrate_message($message, $params, 'error');
  1553. backup_migrate_cleanup();
  1554. backup_migrate_filters_invoke_all('restore_fail', $settings, $message, $params);
  1555. return FALSE;
  1556. }
  1557. /**
  1558. * Clean up when a restore operation succeeds.
  1559. */
  1560. function backup_migrate_restore_succeed($message, $params, $settings) {
  1561. backup_migrate_include('filters', 'files');
  1562. _backup_migrate_message($message, $params, 'success');
  1563. backup_migrate_cleanup();
  1564. backup_migrate_filters_invoke_all('restore_succeed', $settings, $message, $params);
  1565. return FALSE;
  1566. }
  1567. /**
  1568. * Cleans up after a success or failure.
  1569. */
  1570. function backup_migrate_cleanup() {
  1571. // Check that the cleanup function exists. If it doesn't then we probably
  1572. // didn't create any files to be cleaned up.
  1573. if (function_exists('_backup_migrate_temp_files_delete')) {
  1574. _backup_migrate_temp_files_delete();
  1575. }
  1576. }
  1577. /**
  1578. * Shutdown callback.
  1579. *
  1580. * Called when the script terminates even if the script timed out.
  1581. */
  1582. function backup_migrate_shutdown($settings) {
  1583. // If we ran out of time, set an error so the user knows what happened.
  1584. if (_backup_migrate_check_timeout()) {
  1585. backup_migrate_cleanup();
  1586. backup_migrate_backup_fail('The operation timed out. Try increasing your PHP <a href="!url">max_execution_time setting</a>.', array('!url' => 'http://www.php.net/manual/en/info.configuration.php#ini.max-execution-time'), $settings);
  1587. // The session will have already been written and closed, so we need to
  1588. // write any changes directly.
  1589. _drupal_session_write(session_id(), session_encode());
  1590. // Add a redirect or we'll just get whitescreened.
  1591. drupal_goto(BACKUP_MIGRATE_MENU_PATH);
  1592. }
  1593. }
  1594. /**
  1595. * Action to backup the drupal site.
  1596. *
  1597. * Requires actions.module.
  1598. *
  1599. * function action_backup_migrate_backup($op, $edit = array()) {
  1600. * switch ($op) {
  1601. * case 'do':
  1602. * _backup_migrate_backup_with_defaults();
  1603. * watchdog('action', 'Backed up database');
  1604. * break;.
  1605. * * Case 'metadata':
  1606. * return array(
  1607. * 'description' => t('Backup the database with the default settings'),
  1608. * 'type' => t('Backup and Migrate'),
  1609. * 'batchable' => TRUE,
  1610. * 'configurable' => FALSE,
  1611. * );
  1612. * * // Return an HTML config form for the action.
  1613. * * case 'form':
  1614. * return '';
  1615. * * // Validate the HTML form.
  1616. * * case 'validate':
  1617. * return TRUE;
  1618. * * // Process the HTML form to store configuration.
  1619. * * case 'submit':
  1620. * return '';
  1621. * }
  1622. * }.
  1623. */
  1624. /**
  1625. * Implements hook_action_info().
  1626. */
  1627. // function backup_migrate_action_info() {
  1628. // return array(
  1629. // 'backup_migrate_action_backup' => array(
  1630. // 'label' => t('Backup the database'),
  1631. // 'description' => t('Backup the database with the default settings.'),
  1632. // ),
  1633. // );
  1634. // }
  1635. /**
  1636. * Action callback.
  1637. */
  1638. function backup_migrate_action_backup() {
  1639. _backup_migrate_backup_with_defaults();
  1640. }
  1641. /**
  1642. * Backup the database with the default settings.
  1643. */
  1644. function _backup_migrate_backup_with_defaults($destination_id = "manual") {
  1645. backup_migrate_include('files', 'profiles');
  1646. $settings = _backup_migrate_profile_saved_default_profile();
  1647. $settings->destination_id = $destination_id;
  1648. $settings->source_id = 'db';
  1649. backup_migrate_perform_backup($settings);
  1650. }
  1651. /**
  1652. * Helper function to set a Drupal message and watchdog message.
  1653. *
  1654. * Depends on whether the module is being run interactively.
  1655. */
  1656. function _backup_migrate_message($message, $replace = array(), $type = 'status') {
  1657. // Only set a message if there is a callback handler to handle the message.
  1658. if (($callback = _backup_migrate_message_callback()) && function_exists($callback)) {
  1659. $callback($message, $replace, $type);
  1660. }
  1661. // Store the message in case it's needed (for the status notification filter
  1662. // for example).
  1663. _backup_migrate_messages($message, $replace, $type);
  1664. }
  1665. /**
  1666. * Helper function to set a Drupal message and watchdog message.
  1667. *
  1668. * Depends on whether the module is being run interactively.
  1669. */
  1670. function _backup_migrate_messages($message = NULL, $replace = array(), $type = 'status') {
  1671. static $messages = array();
  1672. if ($message) {
  1673. $messages[] = array(
  1674. 'message' => $message,
  1675. 'replace' => $replace,
  1676. 'type' => $type,
  1677. );
  1678. }
  1679. return $messages;
  1680. }
  1681. /**
  1682. * Sends a message to the browser.
  1683. *
  1684. * The normal type of message handling for interactive use.
  1685. */
  1686. function _backup_migrate_message_browser($message, $replace, $type) {
  1687. // Log the message as well for admins.
  1688. _backup_migrate_message_log($message, $replace, $type);
  1689. // If there are links, we can display them in the browser.
  1690. if (!empty($replace['!links'])) {
  1691. $message .= " (!links)";
  1692. }
  1693. // Use drupal_set_message to display to the user.
  1694. drupal_set_message(t($message, $replace), str_replace('success', 'status', $type), FALSE);
  1695. }
  1696. /**
  1697. * Logs a message if we are in a non-interactive mode such as a cron run.
  1698. */
  1699. function _backup_migrate_message_log($message, $replace, $type) {
  1700. // We only want to log the errors or successful completions.
  1701. if (in_array($type, array('error', 'success'))) {
  1702. watchdog('backup_migrate', $message, $replace, $type == 'error' ? WATCHDOG_ERROR : WATCHDOG_NOTICE);
  1703. }
  1704. }
  1705. /**
  1706. * Sets or retrieves a message handler.
  1707. */
  1708. function _backup_migrate_message_callback($callback = NULL) {
  1709. static $current_callback = '_backup_migrate_message_log';
  1710. if ($callback !== NULL) {
  1711. $current_callback = $callback;
  1712. }
  1713. return $current_callback;
  1714. }
  1715. /**
  1716. * Increase the execution timeout, and also the database connection timeout.
  1717. */
  1718. function _backup_migrate_set_timeout() {
  1719. // If not in 'safe mode', increase the maximum execution time:
  1720. $backup_max_time = variable_get('backup_migrate_backup_max_time', 1200);
  1721. if (!ini_get('safe_mode') && ini_get('max_execution_time') < $backup_max_time) {
  1722. drupal_set_time_limit($backup_max_time);
  1723. }
  1724. // Also increase the time mysql keeps the connection open.
  1725. if (Database::getConnection() instanceof DatabaseConnection_mysql) {
  1726. $mysql_wait_timeout = db_query('SELECT @@session.wait_timeout')->fetchField();
  1727. if ($mysql_wait_timeout < $backup_max_time) {
  1728. // This does not work with placeholders, so sanitize by casting to int.
  1729. db_query('SET SESSION wait_timeout = ' . (int) $backup_max_time);
  1730. }
  1731. }
  1732. }
  1733. /**
  1734. * Determines if time for a page execution has run out.
  1735. */
  1736. function _backup_migrate_check_timeout() {
  1737. static $timeout;
  1738. // Max execution of 0 means unlimited.
  1739. if (ini_get('max_execution_time') == 0) {
  1740. return FALSE;
  1741. }
  1742. // Figure out when we should stop execution.
  1743. if (!$timeout) {
  1744. $backup_max_time = max(ini_get('max_execution_time'), variable_get('backup_migrate_backup_max_time', 1200));
  1745. $timeout = (!empty($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time()) + $backup_max_time - variable_get('backup_migrate_timeout_buffer', 5);
  1746. }
  1747. return (time() > $timeout);
  1748. }
  1749. /**
  1750. * Converts an associated array to an ini format string.
  1751. */
  1752. function _backup_migrate_array_to_ini($data, $prefix = '') {
  1753. $content = '';
  1754. foreach ($data as $key => $val) {
  1755. if ($prefix) {
  1756. $key = $prefix . '[' . $key . ']';
  1757. }
  1758. if (is_array($val)) {
  1759. $content .= _backup_migrate_array_to_ini($val, $key);
  1760. }
  1761. else {
  1762. $content .= $key . ' = "' . $val . "\"\n";
  1763. }
  1764. }
  1765. return $content;
  1766. }
  1767. /**
  1768. * Execute a command line command.
  1769. *
  1770. * @return bool
  1771. * Returns FALSE if the function failed.
  1772. */
  1773. function backup_migrate_exec($command, $args = array()) {
  1774. if (!function_exists('exec') || ini_get('safe_mode')) {
  1775. return FALSE;
  1776. }
  1777. // Escape the arguments.
  1778. foreach ($args as $key => $arg) {
  1779. $args[$key] = escapeshellarg($arg);
  1780. }
  1781. $command = strtr($command, $args);
  1782. $output = $result = NULL;
  1783. // Run the command.
  1784. exec($command, $output, $result);
  1785. return $result == 0;
  1786. }
  1787. /**
  1788. * Format a size, treating 1GB as 1000 MB rather than 1024 MB.
  1789. *
  1790. * This is a more standard and expected version for storage (as opposed to
  1791. * memory).
  1792. */
  1793. function backup_migrate_format_size($size, $langcode = LANGUAGE_NONE) {
  1794. $precision = 2;
  1795. $multiply = pow(10, $precision);
  1796. if ($size == 0) {
  1797. return t('0 bytes', array(), array('langcode' => $langcode));
  1798. }
  1799. if ($size < 1024) {
  1800. return format_plural($size, '1 byte', '@count bytes', array(), array('langcode' => $langcode));
  1801. }
  1802. else {
  1803. $size = ceil($size * $multiply / 1024);
  1804. $string = '@size KB';
  1805. if ($size >= (1024 * $multiply)) {
  1806. $size = ceil($size / 1024);
  1807. $string = '@size MB';
  1808. }
  1809. if ($size >= 1000 * $multiply) {
  1810. $size = ceil($size / 1000);
  1811. $string = '@size GB';
  1812. }
  1813. if ($size >= 1000 * $multiply) {
  1814. $size = ceil($size / 1000);
  1815. $string = '@size TB';
  1816. }
  1817. return t($string, array('@size' => round($size / $multiply, $precision)), array('langcode' => $langcode));
  1818. }
  1819. }
  1820. /**
  1821. * Implements hook_mail().
  1822. */
  1823. function backup_migrate_mail($key, &$message, $params) {
  1824. if ($key == 'backup_succeed') {
  1825. $message['id'] = 'backup_migrate_backup_succeed';
  1826. $message['subject'] = t('!site backup succeeded', array('!site' => variable_get('site_name', 'Drupal site')));
  1827. $message['body'][] = $params['body'];
  1828. }
  1829. elseif ($key == 'backup_fail') {
  1830. $message['id'] = 'backup_migrate_backup_fail';
  1831. $message['subject'] = t('!site backup failed', array('!site' => variable_get('site_name', 'Drupal site')));
  1832. $message['body'][] = $params['body'];
  1833. }
  1834. }
  1835. /**
  1836. * Identify the current memory usage.
  1837. *
  1838. * Uses the same formula as the Devel module.
  1839. *
  1840. * @return int
  1841. * The current memory usage, in megabytes.
  1842. */
  1843. function backup_migrate_get_peak_memory_usage() {
  1844. return round(memory_get_peak_usage(TRUE) / 1024 / 1024, 2);
  1845. }
  1846. /**
  1847. * Returns the "name" element from an array.
  1848. *
  1849. * Callback for array_map() within
  1850. * backup_migrate_destination_db_mysql::_restore_db_from_file() and
  1851. * backup_migrate_source_db_mysql::_restore_db_from_file().
  1852. */
  1853. function backup_migrate_array_name_value($a) {
  1854. return $a["name"];
  1855. }