backup_migrate.install 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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' => 32,
  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' => 32,
  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' => 32,
  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' => 32,
  117. 'default' => 'db',
  118. 'not null' => TRUE
  119. ),
  120. 'destination_id' => array(
  121. 'type' => 'varchar',
  122. 'length' => 32,
  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' => 32,
  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. require_once DRUPAL_ROOT . '/'. drupal_get_path('module', 'backup_migrate') .'/includes/crud.inc';
  175. require_once DRUPAL_ROOT . '/'. drupal_get_path('module', 'backup_migrate') .'/backup_migrate.module';
  176. require_once DRUPAL_ROOT . '/'. drupal_get_path('module', 'backup_migrate') .'/includes/profiles.inc';
  177. require_once DRUPAL_ROOT . '/'. drupal_get_path('module', 'backup_migrate') .'/includes/db.inc';
  178. require_once DRUPAL_ROOT . '/'. drupal_get_path('module', 'backup_migrate') .'/includes/files.inc';
  179. if (variable_get("backup_migrate_file_name", NULL)) {
  180. $settings = array(
  181. 'profile_id' => 'default',
  182. 'filename' => variable_get("backup_migrate_file_name", _backup_migrate_default_filename()),
  183. 'append_timestamp' => variable_get("backup_migrate_append_timestamp", FALSE) ? 1 : 0,
  184. 'timestamp_format' => variable_get("backup_migrate_timestamp_format", 'Y-m-d\TH-i-s'),
  185. 'filters' => array(
  186. 'compression' => variable_get("backup_migrate_compression", "none"),
  187. 'exclude_tables' => variable_get("backup_migrate_exclude_tables", array()),
  188. 'nodata_tables' => variable_get("backup_migrate_nodata_tables", array()),
  189. ),
  190. 'name' => t('Default Settings'),
  191. );
  192. $profile = backup_migrate_crud_create_item('profile', $settings);
  193. $profile->save();
  194. variable_set("backup_migrate_profile_id", 'default');
  195. // Set up the default schedules.
  196. if (variable_get("backup_migrate_schedule_backup_period", 0)) {
  197. require_once DRUPAL_ROOT . '/'. drupal_get_path('module', 'backup_migrate') .'/includes/schedules.inc';
  198. $schedule = array(
  199. 'name' => t('Default Schedule'),
  200. 'profile_id' => $profile->get_id(),
  201. 'enabled' => 1,
  202. 'destination_id' => 'scheduled',
  203. 'period' => array('number' => variable_get("backup_migrate_schedule_backup_period", 0), 'type' => 'hours'),
  204. 'keep' => variable_get("backup_migrate_schedule_backup_keep", 0),
  205. );
  206. $schedule = backup_migrate_crud_create_item('schedule', $schedule);
  207. $schedule->save();
  208. }
  209. }
  210. }
  211. /**
  212. * Remove variables on uninstall.
  213. */
  214. function backup_migrate_uninstall() {
  215. db_query("DELETE FROM {variable} WHERE name LIKE 'backup_migrate_%'");
  216. cache_clear_all('variables', 'cache');
  217. }
  218. /**
  219. * Update from 1.x to 2.x.
  220. */
  221. function backup_migrate_update_2000() {
  222. _backup_migrate_setup_database_defaults();
  223. return array();
  224. }
  225. /**
  226. * Adding filter field for dev release of 2009-01-28
  227. */
  228. function backup_migrate_update_2001() {
  229. $ret = array();
  230. $schema = drupal_get_schema_unprocessed('backup_migrate', 'backup_migrate_profiles');
  231. // Add the filters field to the db.
  232. if (!db_field_exists('backup_migrate_profiles', 'filters')) {
  233. db_add_field('backup_migrate_profiles', 'filters', array('description' => t('The filter settings for the profile.'),'type' => 'text', 'not null' => TRUE));
  234. }
  235. // Add the source field
  236. if (!db_field_exists('backup_migrate_profiles', 'source_id')) {
  237. 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' => 32, 'default' => 'db', 'not null' => TRUE));
  238. }
  239. // Remove the compression field.
  240. if (db_field_exists('backup_migrate_profiles', 'compression')) {
  241. db_drop_field('backup_migrate_profiles', 'compression');
  242. }
  243. return $ret;
  244. }
  245. /**
  246. * Clearing the cache because there was a menu structure change in the dev of 2009-05-31
  247. */
  248. function backup_migrate_update_2002() {
  249. // Cache should clear automatically. Nothing to do here.
  250. return array();
  251. }
  252. /**
  253. * Allowing non-int profile ids in schedules 2009-05-31
  254. */
  255. function backup_migrate_update_2003() {
  256. $ret = array();
  257. $spec = array(
  258. 'type' => 'varchar',
  259. 'length' => 32,
  260. 'not null' => TRUE,
  261. 'default' => '0',
  262. 'description' => 'The primary identifier for a profile.',
  263. );
  264. db_change_field('backup_migrate_schedules', 'profile_id', 'profile_id', $spec);
  265. return $ret;
  266. }
  267. /**
  268. * Allowing non-int profile ids 2009-07-01
  269. */
  270. function backup_migrate_update_2004() {
  271. $ret = array();
  272. $spec = array(
  273. 'type' => 'varchar',
  274. 'length' => 32,
  275. 'not null' => TRUE,
  276. 'default' => '0',
  277. );
  278. $spec['description'] = 'The primary identifier for a destination.';
  279. db_change_field('backup_migrate_destinations', 'destination_id', 'destination_id', $spec);
  280. $spec['description'] = 'The primary identifier for a profile.';
  281. db_change_field('backup_migrate_profiles', 'profile_id', 'profile_id', $spec);
  282. $spec['description'] = 'The primary identifier for a schedule.';
  283. db_change_field('backup_migrate_schedules', 'schedule_id', 'schedule_id', $spec);
  284. // Drop the user/pass fields as they weren't being used.
  285. if (db_field_exists('backup_migrate_destinations', 'username')) {
  286. db_drop_field('backup_migrate_destinations', 'username');
  287. }
  288. if (db_field_exists('backup_migrate_destinations', 'password')) {
  289. db_drop_field('backup_migrate_destinations', 'password');
  290. }
  291. return $ret;
  292. }
  293. /**
  294. * Change the default database id to something friendlier 2009-08-08
  295. */
  296. function backup_migrate_update_2005() {
  297. require_once './'. drupal_get_path('module', 'backup_migrate') .'/includes/crud.inc';
  298. require_once './'. drupal_get_path('module', 'backup_migrate') .'/includes/profiles.inc';
  299. $ret = array();
  300. // Change the destination ids of the defined database sources mostly to make using them with drush friendlier.
  301. // Change the db_url:default id to simply 'db'
  302. $ret[] = db_query("UPDATE {backup_migrate_profiles} SET source_id = 'db' WHERE source_id = 'db_url:default'");
  303. $ret[] = db_query("UPDATE {backup_migrate_schedules} SET destination_id = 'db' WHERE destination_id = 'db_url:default'");
  304. // Change the defined db keys from db_url:key to db:key.
  305. $ret[] = db_query("UPDATE {backup_migrate_profiles} SET source_id = REPLACE(source_id, 'db_url:', 'db:')");
  306. $ret[] = db_query("UPDATE {backup_migrate_schedules} SET destination_id = REPLACE(destination_id, 'db_url:', 'db:')");
  307. // Add the source field to the schedule
  308. if (!db_field_exists('backup_migrate_schedules', 'source_id')) {
  309. db_add_field('backup_migrate_schedules', 'source_id', array('description' => t('The db source to backup from.'), 'type' => 'varchar', 'length' => 32, 'default' => 'db', 'not null' => TRUE));
  310. }
  311. // Copy source data from profiles to schedules.
  312. $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));
  313. foreach ($result as $schedule) {
  314. if (!$schedule['source_id']) {
  315. $schedule['source_id'] = 'db';
  316. }
  317. $ret[] = db_query("UPDATE {backup_migrate_schedules} SET source_id = '". $schedule['source_id'] ."' WHERE schedule_id = '". $schedule['profile_id'] ."'");
  318. }
  319. if (db_field_exists('backup_migrate_profiles', 'source_id')) {
  320. db_drop_field('backup_migrate_profiles', 'source_id');
  321. }
  322. // Copy the no-data and exclude tables settings into the 'filter' field.
  323. $result = db_query('SELECT * FROM {backup_migrate_profiles}', array(), array('fetch' => PDO::FETCH_ASSOC));
  324. foreach ($result as $item) {
  325. if (isset($item['nodata_tables']) && isset($item['exclude_tables'])) {
  326. $profile = backup_migrate_get_profile($item['profile_id']);
  327. $profile->filters['nodata_tables'] = unserialize($item['nodata_tables']);
  328. $profile->filters['exclude_tables'] = unserialize($item['exclude_tables']);
  329. $profile->save();
  330. }
  331. }
  332. if (db_field_exists('backup_migrate_profiles', 'nodata_tables')) {
  333. db_drop_field('backup_migrate_profiles', 'nodata_tables');
  334. }
  335. if (db_field_exists('backup_migrate_profiles', 'exclude_tables')) {
  336. db_drop_field('backup_migrate_profiles', 'exclude_tables');
  337. }
  338. return $ret;
  339. }
  340. /**
  341. * Move the backup and migrate directory to the private directory.
  342. */
  343. function backup_migrate_update_7200() {
  344. $from = 'public://backup_migrate';
  345. $to = 'private://backup_migrate';
  346. if (drupal_realpath($from) && !drupal_realpath($to)) {
  347. if (!rename($from, $to)) {
  348. 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');
  349. }
  350. }
  351. }
  352. /**
  353. * Change the filename field to support 255 characters.
  354. */
  355. function backup_migrate_update_7202() {
  356. $ret = array();
  357. db_change_field('backup_migrate_profiles', 'filename', 'filename', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE));
  358. return $ret;
  359. }
  360. /**
  361. * Update the schedule last run times to use variables instead of saving with the schedule.
  362. */
  363. function backup_migrate_update_7203() {
  364. $result = db_query('SELECT * FROM {backup_migrate_schedules}', array(), array('fetch' => PDO::FETCH_ASSOC));
  365. foreach ($result as $item) {
  366. if (isset($item['last_run'])) {
  367. variable_set('backup_migrate_schedule_last_run_' . $item['schedule_id'], $item['last_run']);
  368. }
  369. }
  370. if (db_field_exists('backup_migrate_schedules', 'last_run')) {
  371. db_drop_field('backup_migrate_schedules', 'last_run');
  372. }
  373. }