backup_migrate.install 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. <?php
  2. /**
  3. * @file
  4. * Install hooks for Backup and Migrate.
  5. */
  6. /**
  7. * Implementation of hook_requirements().
  8. */
  9. function backup_migrate_requirements($phase) {
  10. $requirements = array();
  11. return $requirements;
  12. }
  13. /**
  14. * Implementation of hook_schema().
  15. */
  16. function backup_migrate_schema() {
  17. $schema['backup_migrate_profiles'] = array(
  18. 'fields' => array(
  19. 'profile_id' => array(
  20. 'type' => 'varchar',
  21. 'length' => 255,
  22. 'not null' => TRUE,
  23. 'default' => '0',
  24. 'description' => 'The primary identifier for a profile.',
  25. ),
  26. 'name' => array(
  27. 'description' => 'The name of the profile.',
  28. 'type' => 'varchar',
  29. 'length' => 255,
  30. 'not null' => TRUE
  31. ),
  32. 'filename' => array(
  33. 'description' => 'The name of the profile.',
  34. 'type' => 'varchar',
  35. 'length' => 255,
  36. 'not null' => TRUE
  37. ),
  38. 'append_timestamp' => array(
  39. 'description' => 'Append a timestamp to the filename.',
  40. 'type' => 'int',
  41. 'size' => 'tiny',
  42. 'unsigned' => TRUE,
  43. 'not null' => TRUE,
  44. 'default' => 0
  45. ),
  46. 'timestamp_format' => array(
  47. 'description' => 'The format of the timestamp.',
  48. 'type' => 'varchar',
  49. 'length' => 14,
  50. 'not null' => TRUE
  51. ),
  52. 'filters' => array(
  53. 'description' => 'The filter settings for the profile.',
  54. 'type' => 'text',
  55. 'not null' => TRUE,
  56. 'serialize' => TRUE,
  57. 'serialized default' => 'a:0:{}',
  58. ),
  59. ),
  60. 'primary key' => array('profile_id'),
  61. );
  62. $schema['backup_migrate_destinations'] = array(
  63. 'fields' => array(
  64. 'destination_id' => array(
  65. 'type' => 'varchar',
  66. 'length' => 255,
  67. 'not null' => TRUE,
  68. 'default' => '0',
  69. 'description' => 'The primary identifier for a profile.',
  70. ),
  71. 'name' => array(
  72. 'description' => 'The name of the profile.',
  73. 'type' => 'varchar',
  74. 'length' => 255,
  75. 'not null' => TRUE
  76. ),
  77. 'type' => array(
  78. 'description' => 'The type of the destination.',
  79. 'type' => 'varchar',
  80. 'length' => 32,
  81. 'not null' => TRUE
  82. ),
  83. 'location' => array(
  84. 'description' => 'The the location string of the destination.',
  85. 'type' => 'text',
  86. 'not null' => TRUE
  87. ),
  88. 'settings' => array(
  89. 'description' => 'Other settings for the destination.',
  90. 'type' => 'text',
  91. 'not null' => TRUE,
  92. 'serialize' => TRUE,
  93. 'serialized default' => 'a:0:{}',
  94. ),
  95. ),
  96. 'primary key' => array('destination_id'),
  97. );
  98. $schema['backup_migrate_schedules'] = array(
  99. 'fields' => array(
  100. 'schedule_id' => array(
  101. 'type' => 'varchar',
  102. 'length' => 255,
  103. 'not null' => TRUE,
  104. 'default' => '0',
  105. 'description' => 'The primary identifier for a profile.',
  106. ),
  107. 'name' => array(
  108. 'description' => 'The name of the profile.',
  109. 'type' => 'varchar',
  110. 'length' => 255,
  111. 'not null' => TRUE
  112. ),
  113. 'source_id' => array(
  114. 'description' => 'The {backup_migrate_destination}.destination_id of the source to backup from.',
  115. 'type' => 'varchar',
  116. 'length' => 255,
  117. 'default' => 'db',
  118. 'not null' => TRUE
  119. ),
  120. 'destination_id' => array(
  121. 'type' => 'varchar',
  122. 'length' => 255,
  123. 'not null' => TRUE,
  124. 'default' => '0',
  125. 'description' => 'The {backup_migrate_destination}.destination_id of the destination to back up to.',
  126. ),
  127. 'profile_id' => array(
  128. 'type' => 'varchar',
  129. 'length' => 255,
  130. 'not null' => TRUE,
  131. 'default' => '0',
  132. 'description' => 'The primary identifier for a profile.',
  133. ),
  134. 'keep' => array(
  135. 'type' => 'int',
  136. 'not null' => TRUE,
  137. 'default' => 0,
  138. 'description' => 'The number of backups to keep.',
  139. ),
  140. 'period' => array(
  141. 'type' => 'int',
  142. 'not null' => TRUE,
  143. 'default' => 0,
  144. 'description' => 'The number of seconds between backups.',
  145. ),
  146. 'enabled' => array(
  147. 'description' => 'Whether the schedule is enabled.',
  148. 'type' => 'int',
  149. 'size' => 'tiny',
  150. 'unsigned' => TRUE,
  151. 'not null' => TRUE,
  152. 'default' => 0
  153. ),
  154. 'cron' => array(
  155. 'description' => 'Whether the schedule should be run during cron.',
  156. 'type' => 'int',
  157. 'size' => 'tiny',
  158. 'unsigned' => TRUE,
  159. 'not null' => TRUE,
  160. 'default' => 0
  161. ),
  162. ),
  163. 'primary key' => array('schedule_id'),
  164. );
  165. return $schema;
  166. }
  167. /**
  168. * Implementation of hook_install().
  169. */
  170. function backup_migrate_install() {
  171. _backup_migrate_setup_database_defaults();
  172. }
  173. function _backup_migrate_setup_database_defaults() {
  174. if (variable_get("backup_migrate_file_name", NULL)) {
  175. require_once DRUPAL_ROOT . '/'. drupal_get_path('module', 'backup_migrate') .'/includes/crud.inc';
  176. require_once DRUPAL_ROOT . '/'. drupal_get_path('module', 'backup_migrate') .'/backup_migrate.module';
  177. require_once DRUPAL_ROOT . '/'. drupal_get_path('module', 'backup_migrate') .'/includes/profiles.inc';
  178. require_once DRUPAL_ROOT . '/'. drupal_get_path('module', 'backup_migrate') .'/includes/files.inc';
  179. $settings = array(
  180. 'profile_id' => 'default',
  181. 'filename' => variable_get("backup_migrate_file_name", _backup_migrate_default_filename()),
  182. 'append_timestamp' => variable_get("backup_migrate_append_timestamp", FALSE) ? 1 : 0,
  183. 'timestamp_format' => variable_get("backup_migrate_timestamp_format", 'Y-m-d\TH-i-s'),
  184. 'filters' => array(
  185. 'compression' => variable_get("backup_migrate_compression", "none"),
  186. 'exclude_tables' => variable_get("backup_migrate_exclude_tables", array()),
  187. 'nodata_tables' => variable_get("backup_migrate_nodata_tables", array()),
  188. ),
  189. 'name' => t('Default Settings'),
  190. );
  191. $profile = backup_migrate_crud_create_item('profile', $settings);
  192. $profile->save();
  193. variable_set("backup_migrate_profile_id", 'default');
  194. // Set up the default schedules.
  195. if (variable_get("backup_migrate_schedule_backup_period", 0)) {
  196. require_once DRUPAL_ROOT . '/'. drupal_get_path('module', 'backup_migrate') .'/includes/schedules.inc';
  197. $schedule = array(
  198. 'name' => t('Default Schedule'),
  199. 'profile_id' => $profile->get_id(),
  200. 'enabled' => 1,
  201. 'destination_id' => 'scheduled',
  202. 'period' => array('number' => variable_get("backup_migrate_schedule_backup_period", 0), 'type' => 'hours'),
  203. 'keep' => variable_get("backup_migrate_schedule_backup_keep", 0),
  204. );
  205. $schedule = backup_migrate_crud_create_item('schedule', $schedule);
  206. $schedule->save();
  207. }
  208. }
  209. }
  210. /**
  211. * Remove variables on uninstall.
  212. */
  213. function backup_migrate_uninstall() {
  214. db_query("DELETE FROM {variable} WHERE name LIKE 'backup_migrate_%'");
  215. cache_clear_all('variables', 'cache');
  216. }
  217. /**
  218. * Update from 1.x to 2.x.
  219. */
  220. function backup_migrate_update_2000() {
  221. _backup_migrate_setup_database_defaults();
  222. return array();
  223. }
  224. /**
  225. * Adding filter field for dev release of 2009-01-28
  226. */
  227. function backup_migrate_update_2001() {
  228. $ret = array();
  229. $schema = drupal_get_schema_unprocessed('backup_migrate', 'backup_migrate_profiles');
  230. // Add the filters field to the db.
  231. if (!db_field_exists('backup_migrate_profiles', 'filters')) {
  232. db_add_field('backup_migrate_profiles', 'filters', array('description' => t('The filter settings for the profile.'),'type' => 'text', 'not null' => TRUE));
  233. }
  234. // Add the source field
  235. if (!db_field_exists('backup_migrate_profiles', 'source_id')) {
  236. db_add_field('backup_migrate_profiles', 'source_id', array('description' => t('The {backup_migrate_destination}.destination_id of the source to backup from.'), 'type' => 'varchar', 'length' => 255, 'default' => 'db', 'not null' => TRUE));
  237. }
  238. // Remove the compression field.
  239. if (db_field_exists('backup_migrate_profiles', 'compression')) {
  240. db_drop_field('backup_migrate_profiles', 'compression');
  241. }
  242. return $ret;
  243. }
  244. /**
  245. * Clearing the cache because there was a menu structure change in the dev of 2009-05-31
  246. */
  247. function backup_migrate_update_2002() {
  248. // Cache should clear automatically. Nothing to do here.
  249. return array();
  250. }
  251. /**
  252. * Allowing non-int profile ids in schedules 2009-05-31
  253. */
  254. function backup_migrate_update_2003() {
  255. $ret = array();
  256. $spec = array(
  257. 'type' => 'varchar',
  258. 'length' => 255,
  259. 'not null' => TRUE,
  260. 'default' => '0',
  261. 'description' => 'The primary identifier for a profile.',
  262. );
  263. db_change_field('backup_migrate_schedules', 'profile_id', 'profile_id', $spec);
  264. return $ret;
  265. }
  266. /**
  267. * Allowing non-int profile ids 2009-07-01
  268. */
  269. function backup_migrate_update_2004() {
  270. $ret = array();
  271. $spec = array(
  272. 'type' => 'varchar',
  273. 'length' => 255,
  274. 'not null' => TRUE,
  275. 'default' => '0',
  276. );
  277. $spec['description'] = 'The primary identifier for a destination.';
  278. db_change_field('backup_migrate_destinations', 'destination_id', 'destination_id', $spec);
  279. $spec['description'] = 'The primary identifier for a profile.';
  280. db_change_field('backup_migrate_profiles', 'profile_id', 'profile_id', $spec);
  281. $spec['description'] = 'The primary identifier for a schedule.';
  282. db_change_field('backup_migrate_schedules', 'schedule_id', 'schedule_id', $spec);
  283. // Drop the user/pass fields as they weren't being used.
  284. if (db_field_exists('backup_migrate_destinations', 'username')) {
  285. db_drop_field('backup_migrate_destinations', 'username');
  286. }
  287. if (db_field_exists('backup_migrate_destinations', 'password')) {
  288. db_drop_field('backup_migrate_destinations', 'password');
  289. }
  290. return $ret;
  291. }
  292. /**
  293. * Change the default database id to something friendlier 2009-08-08
  294. */
  295. function backup_migrate_update_2005() {
  296. require_once './'. drupal_get_path('module', 'backup_migrate') .'/includes/crud.inc';
  297. require_once './'. drupal_get_path('module', 'backup_migrate') .'/includes/profiles.inc';
  298. $ret = array();
  299. // Change the destination ids of the defined database sources mostly to make using them with drush friendlier.
  300. // Change the db_url:default id to simply 'db'
  301. $ret[] = db_query("UPDATE {backup_migrate_profiles} SET source_id = 'db' WHERE source_id = 'db_url:default'");
  302. $ret[] = db_query("UPDATE {backup_migrate_schedules} SET destination_id = 'db' WHERE destination_id = 'db_url:default'");
  303. // Change the defined db keys from db_url:key to db:key.
  304. $ret[] = db_query("UPDATE {backup_migrate_profiles} SET source_id = REPLACE(source_id, 'db_url:', 'db:')");
  305. $ret[] = db_query("UPDATE {backup_migrate_schedules} SET destination_id = REPLACE(destination_id, 'db_url:', 'db:')");
  306. // Add the source field to the schedule
  307. if (!db_field_exists('backup_migrate_schedules', 'source_id')) {
  308. db_add_field('backup_migrate_schedules', 'source_id', array('description' => t('The db source to backup from.'), 'type' => 'varchar', 'length' => 255, 'default' => 'db', 'not null' => TRUE));
  309. }
  310. // Copy source data from profiles to schedules.
  311. $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));
  312. foreach ($result as $schedule) {
  313. if (!$schedule['source_id']) {
  314. $schedule['source_id'] = 'db';
  315. }
  316. $ret[] = db_query("UPDATE {backup_migrate_schedules} SET source_id = '". $schedule['source_id'] ."' WHERE schedule_id = '". $schedule['profile_id'] ."'");
  317. }
  318. if (db_field_exists('backup_migrate_profiles', 'source_id')) {
  319. db_drop_field('backup_migrate_profiles', 'source_id');
  320. }
  321. // Copy the no-data and exclude tables settings into the 'filter' field.
  322. $result = db_query('SELECT * FROM {backup_migrate_profiles}', array(), array('fetch' => PDO::FETCH_ASSOC));
  323. foreach ($result as $item) {
  324. if (isset($item['nodata_tables']) && isset($item['exclude_tables'])) {
  325. $profile = backup_migrate_get_profile($item['profile_id']);
  326. $profile->filters['nodata_tables'] = unserialize($item['nodata_tables']);
  327. $profile->filters['exclude_tables'] = unserialize($item['exclude_tables']);
  328. $profile->save();
  329. }
  330. }
  331. if (db_field_exists('backup_migrate_profiles', 'nodata_tables')) {
  332. db_drop_field('backup_migrate_profiles', 'nodata_tables');
  333. }
  334. if (db_field_exists('backup_migrate_profiles', 'exclude_tables')) {
  335. db_drop_field('backup_migrate_profiles', 'exclude_tables');
  336. }
  337. return $ret;
  338. }
  339. /**
  340. * Move the backup and migrate directory to the private directory.
  341. */
  342. function backup_migrate_update_7200() {
  343. $from = 'public://backup_migrate';
  344. $to = 'private://backup_migrate';
  345. if (drupal_realpath($from) && !drupal_realpath($to)) {
  346. if (!rename($from, $to)) {
  347. 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');
  348. }
  349. }
  350. }
  351. /**
  352. * Change the filename field to support 255 characters.
  353. */
  354. function backup_migrate_update_7202() {
  355. $ret = array();
  356. db_change_field('backup_migrate_profiles', 'filename', 'filename', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE));
  357. return $ret;
  358. }
  359. /**
  360. * Update the schedule last run times to use variables instead of saving with the schedule.
  361. */
  362. function backup_migrate_update_7203() {
  363. $result = db_query('SELECT * FROM {backup_migrate_schedules}', array(), array('fetch' => PDO::FETCH_ASSOC));
  364. foreach ($result as $item) {
  365. if (isset($item['last_run'])) {
  366. variable_set('backup_migrate_schedule_last_run_' . $item['schedule_id'], $item['last_run']);
  367. }
  368. }
  369. if (db_field_exists('backup_migrate_schedules', 'last_run')) {
  370. db_drop_field('backup_migrate_schedules', 'last_run');
  371. }
  372. }
  373. /**
  374. * Disable the NodeSquirrel module if it's installed.
  375. */
  376. function backup_migrate_update_7204() {
  377. $ret = NULL;
  378. if (module_exists('nodesquirrel')) {
  379. module_disable(array('nodesquirrel'));
  380. $ret = t("The NodeSquirrel module was disabled. NodeSquirrel support is now built into Backup and Migrate.");
  381. }
  382. return $ret;
  383. }
  384. /**
  385. * Increase the length of all ids to 255.
  386. */
  387. function backup_migrate_update_7205() {
  388. $schema['backup_migrate_profiles'] = array(
  389. 'fields' => array(
  390. 'profile_id' => array(
  391. 'type' => 'varchar',
  392. 'length' => 255,
  393. 'not null' => TRUE,
  394. 'default' => '0',
  395. 'description' => t('The primary identifier for a profile.'),
  396. ),
  397. 'filename' => array(
  398. 'description' => t('The base pattern (including unreplaced tokens) for the name of the backup file.'),
  399. 'type' => 'varchar',
  400. 'length' => 255,
  401. 'not null' => TRUE
  402. ),
  403. ),
  404. );
  405. $schema['backup_migrate_destinations'] = array(
  406. 'fields' => array(
  407. 'destination_id' => array(
  408. 'type' => 'varchar',
  409. 'length' => 255,
  410. 'not null' => TRUE,
  411. 'default' => '0',
  412. 'description' => t('The primary identifier for a profile.'),
  413. ),
  414. ),
  415. );
  416. $schema['backup_migrate_schedules'] = array(
  417. 'fields' => array(
  418. 'schedule_id' => array(
  419. 'type' => 'varchar',
  420. 'length' => 255,
  421. 'not null' => TRUE,
  422. 'default' => '0',
  423. 'description' => t('The primary identifier for a profile.'),
  424. ),
  425. 'source_id' => array(
  426. 'description' => t('The {backup_migrate_destination}.destination_id of the source to backup from.'),
  427. 'type' => 'varchar',
  428. 'length' => 255,
  429. 'default' => 'db',
  430. 'not null' => TRUE
  431. ),
  432. 'destination_id' => array(
  433. 'type' => 'varchar',
  434. 'length' => 255,
  435. 'not null' => TRUE,
  436. 'default' => '0',
  437. 'description' => t('The {backup_migrate_destination}.destination_id of the destination to back up to.'),
  438. ),
  439. 'profile_id' => array(
  440. 'type' => 'varchar',
  441. 'length' => 255,
  442. 'not null' => TRUE,
  443. 'default' => '0',
  444. 'description' => t('The primary identifier for a profile.'),
  445. ),
  446. ),
  447. );
  448. $spec = array(
  449. 'type' => 'varchar',
  450. 'length' => 255,
  451. 'not null' => TRUE,
  452. 'default' => '',
  453. );
  454. $schema = backup_migrate_schema();
  455. // Update the length of each of the ids
  456. foreach ($schema as $table => $ids) {
  457. foreach ($ids['fields'] as $field => $spec) {
  458. db_change_field($table, $field, $field, $spec);
  459. }
  460. }
  461. }