backup_migrate.install 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  1. <?php
  2. /**
  3. * @file
  4. * Install hooks for Backup and Migrate.
  5. */
  6. /**
  7. * Implements hook_requirements().
  8. */
  9. function backup_migrate_requirements($phase) {
  10. $requirements = array();
  11. // Ensure translations don't break during installation.
  12. $t = get_t();
  13. if ($phase == 'runtime') {
  14. // Get a list of all destinations, make sure none of them are publicly
  15. // accessible.
  16. // @todo Expand the API to add methods to specifically check this.
  17. require_once dirname(__FILE__) . '/includes/destinations.inc';
  18. foreach (backup_migrate_get_destinations() as $dest_name => $destination) {
  19. if (method_exists($destination, 'get_display_location')) {
  20. $dest_path = $destination->get_display_location();
  21. if (!empty($dest_path) && file_valid_uri($dest_path)) {
  22. $scheme = file_uri_scheme($dest_path);
  23. // Support public and private storage and raw server paths.
  24. if ($scheme === 'private' || $scheme === 'public' || substr($dest_path, 0, 1) == '/') {
  25. // Check if the path exists.
  26. $path_exists = file_prepare_directory($dest_path, FILE_CREATE_DIRECTORY);
  27. if ($path_exists) {
  28. $real_path = drupal_realpath($dest_path);
  29. // See if the private path is somewhere inside the main Drupal
  30. // directory structure.
  31. if (strpos($real_path, DRUPAL_ROOT) === 0) {
  32. // Extract the relative path from the Drupal root path, and
  33. // then add the base URL, theoretically creating a fully
  34. // qualified URL to the storage directory.
  35. $url = substr($real_path, strlen(DRUPAL_ROOT) + 1);
  36. $url = url($url, array('absolute' => TRUE));
  37. $result = drupal_http_request($url);
  38. // If the HTTP request comes back as a status 200 that means
  39. // there is a directory listing of some sort; directory paths
  40. // should return a 503 error.
  41. if (!empty($result->code) && $result->code == 200) {
  42. // Get the human readable information for this destination.
  43. $dest_spec = $destination->get_list_row();
  44. // Display a warning message.
  45. $requirements['bmdest_' . $dest_name] = array(
  46. 'severity' => REQUIREMENT_ERROR,
  47. 'title' => 'Backup Migrate',
  48. 'value' => $t('Backup destination "%dest" is publicly accessible!', array('%dest' => $dest_spec['name'])),
  49. 'description' => $t('The backup destination, "%dest", stores its files in the "%path" directory. This directory is publicly available from the web server and urgently needs to be secured! Please see the Drupal manual on <a href="@manual">configuring the private directory path</a> on how to fix this problem.',
  50. array(
  51. '%dest' => $dest_spec['name'],
  52. '%path' => $real_path,
  53. '@manual' => 'https://www.drupal.org/docs/7/core/modules/file/overview',
  54. )),
  55. );
  56. }
  57. // Check an individual file.
  58. else {
  59. $files = scandir($real_path);
  60. if (!empty($files)) {
  61. foreach ($files as $file) {
  62. // Skip the base field pointers.
  63. if ($file == '.' || $file == '..') {
  64. continue;
  65. }
  66. $result = drupal_http_request($url . '/' . $file);
  67. // If the HTTP request comes back as a status 200 that
  68. // means the file is accessible.
  69. if (!empty($result->code) && $result->code == 200) {
  70. // Get the human readable information for this
  71. // destination.
  72. $dest_spec = $destination->get_list_row();
  73. // Display a warning message.
  74. $requirements['bmdest_' . $dest_name] = array(
  75. 'severity' => REQUIREMENT_ERROR,
  76. 'title' => 'Backup Migrate',
  77. 'value' => $t('Files in "%dest" are publicly accessible!', array('%dest' => $dest_spec['name'])),
  78. 'description' => $t('The backup destination, "%dest", stores its files in the "%path" directory. These file(s) are publicly available from the web server and urgently need to be secured! Please see the Drupal manual on <a href="@manual">configuring the private directory path</a> on how to fix this problem.',
  79. array(
  80. '%dest' => $dest_spec['name'],
  81. '%path' => $real_path,
  82. '@manual' => 'https://www.drupal.org/docs/7/core/modules/file/overview',
  83. )),
  84. );
  85. }
  86. // Only need to check one file.
  87. break;
  88. }
  89. }
  90. }
  91. }
  92. }
  93. }
  94. }
  95. }
  96. }
  97. // Leave a note if there were no problems.
  98. // @todo No point in displaying this until the API has been expanded.
  99. // @code
  100. // if (empty($requirements)) {
  101. // $requirements['bmdest_' . $dest_name] = array(
  102. // 'severity' => REQUIREMENT_INFO,
  103. // 'title' => 'Backup Migrate',
  104. // 'value' => $t('Backup destinations are safe'),
  105. // 'description' => $t('The backup destinations were all checked and none of them were exposing files to the public. This is a good thing.'),
  106. // );
  107. // }
  108. // @endcode
  109. if (variable_get('backup_migrate_disable_cron', FALSE)) {
  110. $requirements['bm_disable_cron'] = array(
  111. 'severity' => REQUIREMENT_INFO,
  112. 'title' => 'Backup Migrate',
  113. 'value' => $t('Cron tasks are disabled'),
  114. 'description' => $t('The cron tasks have been disabled, so scheduled backups will not run. See the Backup & Migrate module\'s README.txt file for further details.'),
  115. );
  116. }
  117. }
  118. return $requirements;
  119. }
  120. /**
  121. * Implements hook_schema().
  122. */
  123. function backup_migrate_schema() {
  124. $schema['backup_migrate_profiles'] = array(
  125. 'export' => array(
  126. 'key' => 'machine_name',
  127. 'key name' => 'Profile ID',
  128. 'admin_title' => 'name',
  129. 'primary key' => 'profile_id',
  130. // Exports will be defined as $preset.
  131. 'identifier' => 'item',
  132. // Function hook name.
  133. 'default hook' => 'exportables_backup_migrate_profiles',
  134. 'api' => array(
  135. 'owner' => 'backup_migrate',
  136. // Base name for api include files.
  137. 'api' => 'backup_migrate_exportables',
  138. 'minimum_version' => 1,
  139. 'current_version' => 1,
  140. ),
  141. ),
  142. 'fields' => array(
  143. 'profile_id' => array(
  144. 'type' => 'serial',
  145. 'unsigned' => TRUE,
  146. 'not null' => TRUE,
  147. 'description' => 'Primary ID field for the table. Not used for anything except internal lookups.',
  148. // Do not export database-only keys.
  149. 'no export' => TRUE,
  150. ),
  151. 'machine_name' => array(
  152. 'type' => 'varchar',
  153. 'length' => 255,
  154. 'not null' => TRUE,
  155. 'default' => '0',
  156. 'description' => 'The primary identifier for a profile.',
  157. ),
  158. 'name' => array(
  159. 'description' => 'The name of the profile.',
  160. 'type' => 'varchar',
  161. 'length' => 255,
  162. 'not null' => TRUE,
  163. ),
  164. 'filename' => array(
  165. 'description' => 'The name of the profile.',
  166. 'type' => 'varchar',
  167. 'length' => 255,
  168. 'not null' => TRUE,
  169. ),
  170. 'append_timestamp' => array(
  171. 'description' => 'Append a timestamp to the filename.',
  172. 'type' => 'int',
  173. 'size' => 'tiny',
  174. 'unsigned' => TRUE,
  175. 'not null' => TRUE,
  176. 'default' => 0,
  177. ),
  178. 'timestamp_format' => array(
  179. 'description' => 'The format of the timestamp.',
  180. 'type' => 'varchar',
  181. 'length' => 14,
  182. 'not null' => TRUE,
  183. ),
  184. 'filters' => array(
  185. 'description' => 'The filter settings for the profile.',
  186. 'type' => 'text',
  187. 'size' => 'medium',
  188. 'not null' => TRUE,
  189. 'serialize' => TRUE,
  190. 'serialized default' => 'a:0:{}',
  191. ),
  192. ),
  193. 'primary key' => array('profile_id'),
  194. );
  195. $schema['backup_migrate_destinations'] = array(
  196. 'export' => array(
  197. 'key' => 'machine_name',
  198. 'key name' => 'Destination ID',
  199. 'admin_title' => 'name',
  200. 'primary key' => 'destination_id',
  201. // Exports will be defined as $preset.
  202. 'identifier' => 'item',
  203. // Function hook name.
  204. 'default hook' => 'exportables_backup_migrate_destinations',
  205. 'api' => array(
  206. 'owner' => 'backup_migrate',
  207. // Base name for api include files.
  208. 'api' => 'backup_migrate_exportables',
  209. 'minimum_version' => 1,
  210. 'current_version' => 1,
  211. ),
  212. ),
  213. 'fields' => array(
  214. 'destination_id' => array(
  215. 'type' => 'serial',
  216. 'unsigned' => TRUE,
  217. 'not null' => TRUE,
  218. 'description' => 'Primary ID field for the table. Not used for anything except internal lookups.',
  219. // Do not export database-only keys.
  220. 'no export' => TRUE,
  221. ),
  222. 'machine_name' => array(
  223. 'type' => 'varchar',
  224. 'length' => 255,
  225. 'not null' => TRUE,
  226. 'default' => '0',
  227. 'description' => 'The primary identifier for a destination.',
  228. ),
  229. 'name' => array(
  230. 'description' => 'The name of the destination.',
  231. 'type' => 'varchar',
  232. 'length' => 255,
  233. 'not null' => TRUE,
  234. ),
  235. 'subtype' => array(
  236. 'description' => 'The type of the destination.',
  237. 'type' => 'varchar',
  238. 'length' => 32,
  239. 'not null' => TRUE,
  240. ),
  241. 'location' => array(
  242. 'description' => 'The the location string of the destination.',
  243. 'type' => 'text',
  244. 'not null' => TRUE,
  245. ),
  246. 'settings' => array(
  247. 'description' => 'Other settings for the destination.',
  248. 'type' => 'text',
  249. 'not null' => TRUE,
  250. 'serialize' => TRUE,
  251. 'serialized default' => 'a:0:{}',
  252. ),
  253. ),
  254. 'primary key' => array('destination_id'),
  255. );
  256. $schema['backup_migrate_sources'] = array(
  257. 'export' => array(
  258. 'key' => 'machine_name',
  259. 'key name' => 'Source ID',
  260. 'admin_title' => 'name',
  261. 'primary key' => 'source_id',
  262. // Exports will be defined as $preset.
  263. 'identifier' => 'item',
  264. // Function hook name.
  265. 'default hook' => 'exportables_backup_migrate_sources',
  266. 'api' => array(
  267. 'owner' => 'backup_migrate',
  268. // Base name for api include files.
  269. 'api' => 'backup_migrate_exportables',
  270. 'minimum_version' => 1,
  271. 'current_version' => 1,
  272. ),
  273. ),
  274. 'fields' => array(
  275. 'source_id' => array(
  276. 'type' => 'serial',
  277. 'unsigned' => TRUE,
  278. 'not null' => TRUE,
  279. 'description' => 'Primary ID field for the table. Not used for anything except internal lookups.',
  280. // Do not export database-only keys.
  281. 'no export' => TRUE,
  282. ),
  283. 'machine_name' => array(
  284. 'type' => 'varchar',
  285. 'length' => 255,
  286. 'not null' => TRUE,
  287. 'default' => '0',
  288. 'description' => 'The primary identifier for a source.',
  289. ),
  290. 'name' => array(
  291. 'description' => 'The name of the source.',
  292. 'type' => 'varchar',
  293. 'length' => 255,
  294. 'not null' => TRUE,
  295. ),
  296. 'subtype' => array(
  297. 'description' => 'The type of the source.',
  298. 'type' => 'varchar',
  299. 'length' => 32,
  300. 'not null' => TRUE,
  301. ),
  302. 'location' => array(
  303. 'description' => 'The the location string of the source.',
  304. 'type' => 'text',
  305. 'not null' => TRUE,
  306. ),
  307. 'settings' => array(
  308. 'description' => 'Other settings for the source.',
  309. 'type' => 'text',
  310. 'not null' => TRUE,
  311. 'serialize' => TRUE,
  312. 'serialized default' => 'a:0:{}',
  313. ),
  314. ),
  315. 'primary key' => array('source_id'),
  316. );
  317. $schema['backup_migrate_schedules'] = array(
  318. 'export' => array(
  319. 'key' => 'machine_name',
  320. 'key name' => 'Source ID',
  321. 'admin_title' => 'name',
  322. 'primary key' => 'schedule_id',
  323. // Exports will be defined as $preset.
  324. 'identifier' => 'item',
  325. // Function hook name.
  326. 'default hook' => 'exportables_backup_migrate_schedules',
  327. 'api' => array(
  328. 'owner' => 'backup_migrate',
  329. // Base name for api include files.
  330. 'api' => 'backup_migrate_exportables',
  331. 'minimum_version' => 1,
  332. 'current_version' => 1,
  333. ),
  334. ),
  335. 'fields' => array(
  336. 'schedule_id' => array(
  337. 'type' => 'serial',
  338. 'unsigned' => TRUE,
  339. 'not null' => TRUE,
  340. 'description' => 'Primary ID field for the table. Not used for anything except internal lookups.',
  341. // Do not export database-only keys.
  342. 'no export' => TRUE,
  343. ),
  344. 'machine_name' => array(
  345. 'type' => 'varchar',
  346. 'length' => 255,
  347. 'not null' => TRUE,
  348. 'default' => '0',
  349. 'description' => 'The primary identifier for a profile.',
  350. ),
  351. 'name' => array(
  352. 'description' => 'The name of the profile.',
  353. 'type' => 'varchar',
  354. 'length' => 255,
  355. 'not null' => TRUE,
  356. ),
  357. 'source_id' => array(
  358. 'description' => 'The {backup_migrate_destination}.destination_id of the source to backup from.',
  359. 'type' => 'varchar',
  360. 'length' => 255,
  361. 'default' => 'db',
  362. 'not null' => TRUE,
  363. ),
  364. 'destination_id' => array(
  365. 'type' => 'varchar',
  366. 'length' => 255,
  367. 'not null' => TRUE,
  368. 'default' => '0',
  369. 'description' => 'The {backup_migrate_destination}.destination_id of the destination to back up to.',
  370. ),
  371. 'copy_destination_id' => array(
  372. 'type' => 'varchar',
  373. 'length' => 32,
  374. 'not null' => TRUE,
  375. 'default' => '0',
  376. 'description' => 'A second {backup_migrate_destination}.destination_id of the destination to copy the backup to.',
  377. ),
  378. 'profile_id' => array(
  379. 'type' => 'varchar',
  380. 'length' => 255,
  381. 'not null' => TRUE,
  382. 'default' => '0',
  383. 'description' => 'The primary identifier for a profile.',
  384. ),
  385. 'keep' => array(
  386. 'type' => 'int',
  387. 'not null' => TRUE,
  388. 'default' => 0,
  389. 'description' => 'The number of backups to keep.',
  390. ),
  391. 'period' => array(
  392. 'type' => 'int',
  393. 'not null' => TRUE,
  394. 'default' => 0,
  395. 'description' => 'The number of seconds between backups.',
  396. ),
  397. 'enabled' => array(
  398. 'description' => 'Whether the schedule is enabled.',
  399. 'type' => 'int',
  400. 'size' => 'tiny',
  401. 'unsigned' => TRUE,
  402. 'not null' => TRUE,
  403. 'default' => 0,
  404. ),
  405. 'cron' => array(
  406. 'description' => 'Whether the schedule should be run during cron.',
  407. 'type' => 'varchar',
  408. 'length' => 32,
  409. 'not null' => TRUE,
  410. 'default' => 'builtin',
  411. ),
  412. 'cron_schedule' => array(
  413. 'description' => 'The cron schedule to run on.',
  414. 'type' => 'varchar',
  415. 'length' => 255,
  416. 'not null' => TRUE,
  417. 'default' => '0 4 * * *',
  418. ),
  419. ),
  420. 'primary key' => array('schedule_id'),
  421. );
  422. return $schema;
  423. }
  424. /**
  425. * Implements hook_uninstall().
  426. */
  427. function backup_migrate_uninstall() {
  428. variable_del('backup_migrate_backup_max_time');
  429. variable_del('backup_migrate_cleanup_temp_files');
  430. variable_del('backup_migrate_cleanup_time');
  431. variable_del('backup_migrate_copy_destination_id');
  432. variable_del('backup_migrate_data_bytes_per_line');
  433. variable_del('backup_migrate_data_rows_per_line');
  434. variable_del('backup_migrate_data_rows_per_query');
  435. variable_del('backup_migrate_destination_id');
  436. variable_del('backup_migrate_disable_cron');
  437. variable_del('backup_migrate_max_email_size');
  438. variable_del('backup_migrate_memory_limit');
  439. variable_del('backup_migrate_profile_id');
  440. variable_del('backup_migrate_schedule_buffer');
  441. variable_del('backup_migrate_schedule_last_run_');
  442. variable_del('backup_migrate_source_id');
  443. variable_del('backup_migrate_timeout_buffer');
  444. variable_del('backup_migrate_verbose');
  445. variable_del('nodesquirrel_endpoint_urls');
  446. variable_del('nodesquirrel_schedule');
  447. variable_del('nodesquirrel_schedule_enabled');
  448. variable_del('nodesquirrel_schedule_source_id');
  449. variable_del('nodesquirrel_secret_key');
  450. // Remove the dynamic generated 'last run' variables.
  451. $last_ran_schedules = db_select('variable', 'var')
  452. ->fields('var', array('name'))
  453. ->condition('name', 'backup\_migrate\_schedule\_last\_run\_%', 'LIKE')
  454. ->execute()
  455. ->fetchAllAssoc('name', PDO::FETCH_ASSOC);
  456. foreach ($last_ran_schedules as $key => $schedule) {
  457. variable_del($key);
  458. }
  459. }
  460. /**
  461. * Update from 1.x to 2.x.
  462. */
  463. function backup_migrate_update_2000() {
  464. _backup_migrate_setup_database_defaults();
  465. return array();
  466. }
  467. /**
  468. * Adding filter field for dev release of 2009-01-28.
  469. */
  470. function backup_migrate_update_2001() {
  471. $ret = array();
  472. $schema = drupal_get_schema_unprocessed('backup_migrate', 'backup_migrate_profiles');
  473. // Add the filters field to the db.
  474. if (!db_field_exists('backup_migrate_profiles', 'filters')) {
  475. db_add_field('backup_migrate_profiles', 'filters', array(
  476. 'description' => t('The filter settings for the profile.'),
  477. 'type' => 'text',
  478. 'not null' => TRUE,
  479. ));
  480. }
  481. // Add the source field.
  482. if (!db_field_exists('backup_migrate_profiles', 'source_id')) {
  483. db_add_field('backup_migrate_profiles', 'source_id', array(
  484. 'description' => t('The {backup_migrate_destination}.destination_id of the source to backup from.'),
  485. 'type' => 'varchar',
  486. 'length' => 255,
  487. 'default' => 'db',
  488. 'not null' => TRUE,
  489. ));
  490. }
  491. // Remove the compression field.
  492. if (db_field_exists('backup_migrate_profiles', 'compression')) {
  493. db_drop_field('backup_migrate_profiles', 'compression');
  494. }
  495. return $ret;
  496. }
  497. /**
  498. * Clear the cache because there was a menu structure change.
  499. */
  500. function backup_migrate_update_2002() {
  501. // Cache should clear automatically. Nothing to do here.
  502. return array();
  503. }
  504. /**
  505. * Allowing non-int profile ids in schedules 2009-05-31.
  506. */
  507. function backup_migrate_update_2003() {
  508. $ret = array();
  509. $spec = array(
  510. 'type' => 'varchar',
  511. 'length' => 255,
  512. 'not null' => TRUE,
  513. 'default' => '0',
  514. 'description' => 'The primary identifier for a profile.',
  515. );
  516. db_change_field('backup_migrate_schedules', 'profile_id', 'profile_id', $spec);
  517. return $ret;
  518. }
  519. /**
  520. * Allowing non-int profile ids 2009-07-01.
  521. */
  522. function backup_migrate_update_2004() {
  523. $ret = array();
  524. $spec = array(
  525. 'type' => 'varchar',
  526. 'length' => 255,
  527. 'not null' => TRUE,
  528. 'default' => '0',
  529. );
  530. $spec['description'] = 'The primary identifier for a destination.';
  531. db_change_field('backup_migrate_destinations', 'destination_id', 'destination_id', $spec);
  532. $spec['description'] = 'The primary identifier for a profile.';
  533. db_change_field('backup_migrate_profiles', 'profile_id', 'profile_id', $spec);
  534. $spec['description'] = 'The primary identifier for a schedule.';
  535. db_change_field('backup_migrate_schedules', 'schedule_id', 'schedule_id', $spec);
  536. // Drop the user/pass fields as they weren't being used.
  537. if (db_field_exists('backup_migrate_destinations', 'username')) {
  538. db_drop_field('backup_migrate_destinations', 'username');
  539. }
  540. if (db_field_exists('backup_migrate_destinations', 'password')) {
  541. db_drop_field('backup_migrate_destinations', 'password');
  542. }
  543. return $ret;
  544. }
  545. /**
  546. * Change the default database id to something friendlier 2009-08-08.
  547. */
  548. function backup_migrate_update_2005() {
  549. require_once './' . drupal_get_path('module', 'backup_migrate') . '/includes/crud.inc';
  550. require_once './' . drupal_get_path('module', 'backup_migrate') . '/includes/profiles.inc';
  551. $ret = array();
  552. // Change the destination ids of the defined database sources mostly to make
  553. // using them with drush friendlier.
  554. // Change the db_url:default id to simply 'db'.
  555. $ret[] = db_query("UPDATE {backup_migrate_profiles} SET source_id = 'db' WHERE source_id = 'db_url:default'");
  556. $ret[] = db_query("UPDATE {backup_migrate_schedules} SET destination_id = 'db' WHERE destination_id = 'db_url:default'");
  557. // Change the defined db keys from db_url:key to db:key.
  558. $ret[] = db_query("UPDATE {backup_migrate_profiles} SET source_id = REPLACE(source_id, 'db_url:', 'db:')");
  559. $ret[] = db_query("UPDATE {backup_migrate_schedules} SET destination_id = REPLACE(destination_id, 'db_url:', 'db:')");
  560. // Add the source field to the schedule.
  561. if (!db_field_exists('backup_migrate_schedules', 'source_id')) {
  562. db_add_field('backup_migrate_schedules', 'source_id', array(
  563. 'description' => t('The db source to backup from.'),
  564. 'type' => 'varchar',
  565. 'length' => 255,
  566. 'default' => 'db',
  567. 'not null' => TRUE,
  568. ));
  569. }
  570. // Copy source data from profiles to schedules.
  571. $result = db_query('SELECT p.source_id, s.schedule_id FROM {backup_migrate_schedules} s LEFT JOIN {backup_migrate_profiles} p ON s.profile_id = p.profile_id', array(), array('fetch' => PDO::FETCH_ASSOC));
  572. foreach ($result as $schedule) {
  573. if (!$schedule['source_id']) {
  574. $schedule['source_id'] = 'db';
  575. }
  576. $ret[] = db_query("UPDATE {backup_migrate_schedules} SET source_id = '" . $schedule['source_id'] . "' WHERE schedule_id = '" . $schedule['profile_id'] . "'");
  577. }
  578. if (db_field_exists('backup_migrate_profiles', 'source_id')) {
  579. db_drop_field('backup_migrate_profiles', 'source_id');
  580. }
  581. // Copy the no-data and exclude tables settings into the 'filter' field.
  582. $result = db_query('SELECT * FROM {backup_migrate_profiles}', array(), array('fetch' => PDO::FETCH_ASSOC));
  583. foreach ($result as $item) {
  584. if (isset($item['nodata_tables']) && isset($item['exclude_tables'])) {
  585. $profile = backup_migrate_get_profile($item['profile_id']);
  586. $profile->filters['nodata_tables'] = unserialize($item['nodata_tables']);
  587. $profile->filters['exclude_tables'] = unserialize($item['exclude_tables']);
  588. $profile->save();
  589. }
  590. }
  591. if (db_field_exists('backup_migrate_profiles', 'nodata_tables')) {
  592. db_drop_field('backup_migrate_profiles', 'nodata_tables');
  593. }
  594. if (db_field_exists('backup_migrate_profiles', 'exclude_tables')) {
  595. db_drop_field('backup_migrate_profiles', 'exclude_tables');
  596. }
  597. return $ret;
  598. }
  599. /**
  600. * Move the backup and migrate directory to the private directory.
  601. */
  602. function backup_migrate_update_7200() {
  603. $from = 'public://backup_migrate';
  604. $to = 'private://backup_migrate';
  605. if (drupal_realpath($from) && !drupal_realpath($to)) {
  606. if (!rename($from, $to)) {
  607. drupal_set_message(t('Unable to move the backups directory to your private folder, please check file permissions and move the directory %from to %to', array('%from' => drupal_realpath($from), '%to' => drupal_realpath($to))), 'warning');
  608. }
  609. }
  610. }
  611. /**
  612. * Change the filename field to support 255 characters.
  613. */
  614. function backup_migrate_update_7202() {
  615. $ret = array();
  616. db_change_field('backup_migrate_profiles', 'filename', 'filename', array(
  617. 'type' => 'varchar',
  618. 'length' => 255,
  619. 'not null' => TRUE,
  620. ));
  621. return $ret;
  622. }
  623. /**
  624. * Schedule last run times to use variables instead of saving with the schedule.
  625. */
  626. function backup_migrate_update_7203() {
  627. $result = db_query('SELECT * FROM {backup_migrate_schedules}', array(), array('fetch' => PDO::FETCH_ASSOC));
  628. foreach ($result as $item) {
  629. if (isset($item['last_run'])) {
  630. variable_set('backup_migrate_schedule_last_run_' . $item['schedule_id'], $item['last_run']);
  631. }
  632. }
  633. if (db_field_exists('backup_migrate_schedules', 'last_run')) {
  634. db_drop_field('backup_migrate_schedules', 'last_run');
  635. }
  636. }
  637. /**
  638. * Upgrade from Backup & Migrate 7.x-2.x.
  639. *
  640. * - Uninstall the Backup Migrate Files module if it's installed.
  641. * - Uninstall the NodeSquirrel module if it's installed.
  642. * - Upgrade the configurations.
  643. */
  644. function backup_migrate_update_7300() {
  645. if (module_exists('backup_migrate_files')) {
  646. module_disable(array('backup_migrate_files'));
  647. $ret[] = array(
  648. 'success' => TRUE,
  649. 'query' => 'Disabled the Backup and Migrate Files module',
  650. );
  651. }
  652. if (module_exists('nodesquirrel')) {
  653. module_disable(array('nodesquirrel'));
  654. $ret[] = array(
  655. 'success' => TRUE,
  656. 'query' => 'Disabled the NodeSquirrel module',
  657. );
  658. }
  659. // Add sources to the schema.
  660. $schema['backup_migrate_sources'] = array(
  661. 'fields' => array(
  662. 'source_id' => array(
  663. 'type' => 'varchar',
  664. 'length' => 32,
  665. 'not null' => TRUE,
  666. 'default' => '0',
  667. 'description' => t('The primary identifier for a source.'),
  668. ),
  669. 'name' => array(
  670. 'description' => t('The name of the source.'),
  671. 'type' => 'varchar',
  672. 'length' => 255,
  673. 'not null' => TRUE,
  674. ),
  675. 'type' => array(
  676. 'description' => t('The type of the source.'),
  677. 'type' => 'varchar',
  678. 'length' => 32,
  679. 'not null' => TRUE,
  680. ),
  681. 'location' => array(
  682. 'description' => t('The the location string of the source.'),
  683. 'type' => 'text',
  684. 'not null' => TRUE,
  685. ),
  686. 'settings' => array(
  687. 'description' => t('Other settings for the source.'),
  688. 'type' => 'text',
  689. 'not null' => TRUE,
  690. 'serialize' => TRUE,
  691. 'serialized default' => 'a:0:{}',
  692. ),
  693. ),
  694. 'primary key' => array('source_id'),
  695. );
  696. if (!db_table_exists('backup_migrate_sources')) {
  697. db_create_table('backup_migrate_sources', $schema['backup_migrate_sources']);
  698. }
  699. // Move custom destinations to sources.
  700. $result = db_query("SELECT * FROM {backup_migrate_destinations} WHERE type in ('filesource', 'db')", array(), array('fetch' => PDO::FETCH_ASSOC));
  701. foreach ($result as $item) {
  702. $item['source_id'] = $item['destination_id'];
  703. drupal_write_record('backup_migrate_source', $item);
  704. }
  705. // Change 'destination' settings to 'source' settings.
  706. $result = db_query('SELECT * FROM {backup_migrate_profiles}', array(), array('fetch' => PDO::FETCH_ASSOC));
  707. foreach ($result as $item) {
  708. $item['filters'] = unserialize($item['filters']);
  709. $item['filters']['sources'] = $item['filters']['destinations'];
  710. unset($item['filters']['destinations']);
  711. drupal_write_record('backup_migrate_profiles', $item, array('profile_id'));
  712. }
  713. // Clear the plugins caches.
  714. cache_clear_all('*', 'cache', TRUE);
  715. // Rebuild the menus.
  716. variable_set('menu_rebuild_needed', TRUE);
  717. }
  718. /**
  719. * Switch the cron switch to text.
  720. */
  721. function backup_migrate_update_7301() {
  722. db_change_field('backup_migrate_schedules', 'cron', 'cron', array(
  723. 'type' => 'varchar',
  724. 'length' => 32,
  725. 'not null' => TRUE,
  726. 'default' => 'builtin',
  727. ));
  728. db_add_field('backup_migrate_schedules', 'cron_schedule', array(
  729. 'description' => 'The cron schedule to run on.',
  730. 'type' => 'varchar',
  731. 'length' => 255,
  732. 'default' => '0 4 * * *',
  733. 'not null' => TRUE,
  734. ));
  735. }
  736. /**
  737. * Add a second destination to schedules.
  738. */
  739. function backup_migrate_update_7302() {
  740. db_add_field('backup_migrate_schedules', 'copy_destination_id',
  741. array(
  742. 'type' => 'varchar',
  743. 'length' => 32,
  744. 'not null' => TRUE,
  745. 'default' => '0',
  746. 'description' => 'A second {backup_migrate_destination}.destination_id of the destination to copy the backup to.',
  747. )
  748. );
  749. }
  750. /**
  751. * Add a serial id field to all tables to allow them to be ctools exportable.
  752. */
  753. function backup_migrate_update_7303() {
  754. foreach (array(
  755. 'backup_migrate_sources' => 'source_id',
  756. 'backup_migrate_destinations' => 'destination_id',
  757. 'backup_migrate_schedules' => 'schedule_id',
  758. 'backup_migrate_profiles' => 'profile_id',
  759. ) as $table => $id) {
  760. // Take the primary key status from the machine name so it can be renamed
  761. // A serial field must be defined as a key, so make a temporary index.
  762. // See: https://www.drupal.org/node/190027
  763. db_add_index($table, 'temp', array($id));
  764. db_drop_primary_key($table);
  765. // Drop our temporary index.
  766. db_drop_index($table, 'temp');
  767. // Switch the name of the id to 'machine_name' to be more ctools standard.
  768. db_change_field($table, $id, 'machine_name', array(
  769. 'type' => 'varchar',
  770. 'length' => 32,
  771. 'not null' => TRUE,
  772. ));
  773. // Add a serial ID.
  774. db_add_field($table, $id,
  775. array(
  776. 'type' => 'serial',
  777. 'unsigned' => TRUE,
  778. 'not null' => TRUE,
  779. 'description' => 'Primary ID field for the table. Not used for anything except internal lookups.',
  780. // Do not export database-only keys.
  781. 'no export' => TRUE,
  782. ),
  783. array('primary key' => array($id))
  784. );
  785. }
  786. foreach (array('backup_migrate_sources', 'backup_migrate_destinations') as $table) {
  787. db_change_field($table, 'type', 'subtype', array(
  788. 'type' => 'varchar',
  789. 'length' => 32,
  790. 'not null' => TRUE,
  791. ));
  792. }
  793. }
  794. /**
  795. * Update all schedules to use the built in cron if none is specified.
  796. */
  797. function backup_migrate_update_7304() {
  798. db_query("UPDATE {backup_migrate_schedules} SET cron = 'builtin' WHERE cron = '0'");
  799. }
  800. /**
  801. * Fix schema mismatch after upgrade.
  802. */
  803. function backup_migrate_update_7305() {
  804. // Fix the 'machine_name' table fields.
  805. $field_spec = array(
  806. 'type' => 'varchar',
  807. 'length' => 255,
  808. 'not null' => TRUE,
  809. 'default' => '0',
  810. );
  811. foreach (array(
  812. 'backup_migrate_profiles',
  813. 'backup_migrate_destinations',
  814. 'backup_migrate_sources',
  815. 'backup_migrate_schedules',
  816. ) as $table) {
  817. if (!db_field_exists($table, 'machine_name')) {
  818. try {
  819. db_add_field($table, 'machine_name', $field_spec);
  820. }
  821. catch (\Exception $e) {
  822. db_change_field($table, 'machine_name', 'machine_name', $field_spec);
  823. }
  824. }
  825. else {
  826. db_change_field($table, 'machine_name', 'machine_name', $field_spec);
  827. }
  828. }
  829. // Fix the 'cron' table field.
  830. $field_spec = array(
  831. 'type' => 'varchar',
  832. 'length' => 32,
  833. 'not null' => TRUE,
  834. 'default' => 'builtin',
  835. );
  836. if (!db_field_exists('backup_migrate_schedules', 'cron')) {
  837. try {
  838. db_add_field('backup_migrate_schedules', 'cron', $field_spec);
  839. }
  840. catch (\Exception $e) {
  841. db_change_field('backup_migrate_schedules', 'cron', 'cron', $field_spec);
  842. }
  843. }
  844. else {
  845. db_change_field('backup_migrate_schedules', 'cron', 'cron', $field_spec);
  846. }
  847. }
  848. /**
  849. * Leave a message to explain the mixup over the backup option.
  850. */
  851. function backup_migrate_update_7306() {
  852. drupal_set_message(t('Please note that release 7.x-3.4 had a bug which caused all backups to be overwritten instead of having a timestamp added. Please review all backup settings to ensure they work as intended.'), 'warning');
  853. }
  854. /**
  855. * - 'backup_migrate_backup_memory_limit' vs 'backup_migrate_memory_limit'.
  856. */
  857. function backup_migrate_update_7307() {
  858. $limit = variable_get('backup_migrate_backup_memory_limit');
  859. if (!empty($limit)) {
  860. variable_set('backup_migrate_memory_limit', $limit);
  861. variable_del('backup_migrate_backup_memory_limit');
  862. }
  863. }
  864. /**
  865. * Update profiles table filter field to accommodate larger serialized strings.
  866. */
  867. function backup_migrate_update_7308() {
  868. db_change_field('backup_migrate_profiles', 'filters', 'filters', array(
  869. 'description' => 'The filter settings for the profile.',
  870. 'type' => 'text',
  871. 'size' => 'medium',
  872. 'not null' => TRUE,
  873. 'serialize' => TRUE,
  874. 'serialized default' => 'a:0:{}',
  875. ));
  876. }
  877. /**
  878. * NodeSquirrel support has been removed.
  879. */
  880. function backup_migrate_update_7309() {
  881. require_once dirname(__FILE__) . '/includes/destinations.inc';
  882. require_once dirname(__FILE__) . '/includes/schedules.inc';
  883. foreach (backup_migrate_get_schedules() as $schedule) {
  884. // Look for backups which use NodeSquirrel as its destination.
  885. if ($schedule->destination_id == 'nodesquirrel') {
  886. $name = $schedule->name;
  887. // If this schedule had a second destination, swap the destinations.
  888. if (!empty($schedule->copy_destination_id)) {
  889. $destination = backup_migrate_get_destination($schedule->copy_destination_id);
  890. $schedule->destination_id = $schedule->copy_destination_id;
  891. $schedule->copy_destination_id = '';
  892. $schedule->name = $destination->name;
  893. $schedule->save();
  894. drupal_set_message(t('The backup schedule named "%backup" was renamed and now just backups to %destination.', array('%backup' => $name, '%destination' => $destination->name)));
  895. }
  896. // Just delete it.
  897. else {
  898. $schedule->delete();
  899. drupal_set_message(t('The backup schedule named "%backup" as been deleted.', array('%backup' => $name)));
  900. }
  901. }
  902. // Backups which used NodeSquirrel as the second destination will have the
  903. // second destination disabled.
  904. elseif ($schedule->copy_destination_id == 'nodesquirrel') {
  905. $schedule->copy_destination_id = '';
  906. $schedule->save();
  907. drupal_set_message(t('The backup schedule named "%backup" no longer keeps a second backup on NodeSquirrel.', array('%backup' => $schedule->name)));
  908. }
  909. }
  910. // Clear the cache so that the NodeSquirrel plugin is no longer loaded.
  911. cache_clear_all('*', 'cache', TRUE);
  912. // Rebuild the menus so that the NodeSquirrel menu item is removed.
  913. variable_set('menu_rebuild_needed', TRUE);
  914. // @todo Remove the configuration later.
  915. drupal_set_message(t('<a href="!url">NodeSquirrel stopped being usable as a backup destination</a> on October 1st, 2019 and ceased operations entirely on November 1st, 2019. Because of this, the NodeSquirrel functionality has been disabled. Please switch to an alternate destination if necessary. Please note that the NodeSquirrel configuration itself has not been removed.', array('!url' => 'https://pantheon.io/nodesquirrel-service-end-life')));
  916. }
  917. /**
  918. * Disable e-mail destinations.
  919. */
  920. function backup_migrate_update_7310() {
  921. $out = '';
  922. // Disable scheduled e-mail back-ups to allow users to review their
  923. // destinations first.
  924. //
  925. // Which e-mail destinations exist?
  926. $destinations = db_select('backup_migrate_destinations', 'bmd')
  927. ->fields('bmd', array('machine_name'))
  928. ->condition('subtype', 'email', '=')
  929. ->execute()
  930. ->fetchAllAssoc('machine_name', PDO::FETCH_ASSOC);
  931. $destinations = array_keys($destinations);
  932. if (!empty($destinations)) {
  933. // Which schedules contain enabled e-mail destinations?
  934. $or = db_or();
  935. $or->condition('destination_id', $destinations, 'IN');
  936. $or->condition('copy_destination_id', $destinations, 'IN');
  937. $schedules_query = db_select('backup_migrate_schedules', 'bms')
  938. ->fields('bms', array('schedule_id', 'name'))
  939. ->condition($or)
  940. ->condition('enabled', 1, '=');
  941. $schedules = $schedules_query->execute()->fetchAllAssoc('schedule_id', PDO::FETCH_ASSOC);
  942. // Disable the relevant schedules.
  943. if (count($schedules) > 0) {
  944. $email_schedules = array_keys($schedules);
  945. $names = array_column($schedules, 'name');
  946. $replacements = array('@schedules' => '<ul><li>' . implode('</li><li>', $names) . '</li></ul>');
  947. db_update('backup_migrate_schedules')
  948. ->fields(['enabled' => '0'])
  949. ->condition('schedule_id', $email_schedules, 'IN')
  950. ->execute();
  951. $out .= t('Schedules that back up to e-mail destinations have been disabled. Check that you are using the correct e-mail addresses, then re-enable manually. The following schedules have been disabled: @schedules', $replacements);
  952. }
  953. }
  954. if (!empty($out)) {
  955. return $out;
  956. }
  957. else {
  958. return t('No destinations were affected by this change.');
  959. }
  960. }
  961. /**
  962. * Adjust the default performance settings.
  963. */
  964. function backup_migrate_update_7311() {
  965. $settings = array(
  966. 'backup_migrate_data_rows_per_query' => array(
  967. 'old' => 1000,
  968. 'new' => 50000,
  969. ),
  970. 'backup_migrate_data_rows_per_line' => array(
  971. 'old' => 30,
  972. 'new' => 10,
  973. ),
  974. 'backup_migrate_data_bytes_per_line' => array(
  975. 'old' => 2000,
  976. 'new' => 2 * 1024,
  977. ),
  978. );
  979. foreach ($settings as $var_name => $data) {
  980. $stored = variable_get($var_name);
  981. // Nothing was stored in the database.
  982. if (empty($stored)) {
  983. // Don't do anything, the new default will be picked up automatically.
  984. }
  985. // The old value was smaller than or equal to the new value, or was the
  986. // same as the old default, delete it so that the new default can be picked
  987. // up.
  988. elseif (intval($stored) <= $data['new'] || $stored == $data['old']) {
  989. variable_del($var_name);
  990. drupal_set_message(t('!var setting reset to the new default of !val.', array('!var' => $var_name, '!val' => $data['new'])));
  991. }
  992. }
  993. }