backup_migrate.module 67 KB

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