archive.drush.inc

  1. 8.0.x commands/core/archive.drush.inc
  2. 6.x commands/core/archive.drush.inc
  3. 7.x commands/core/archive.drush.inc
  4. 4.x commands/core/archive.drush.inc
  5. 5.x commands/core/archive.drush.inc
  6. master commands/core/archive.drush.inc

An early implementation of Site Archive dump/restore. See http://groups.drupal.org/site-archive-format.

Functions

Namesort descending Description
archive_archive_dump_complete Command argument complete callback.
archive_archive_restore_complete Command argument complete callback.
archive_drush_command
drush_archive_dump Command callback. Generate site archive file.
drush_archive_guess_manifest Try to find docroot and DB dump file in an extracted archive.
drush_archive_restore Command callback. Restore web site(s) from a site archive file.

File

commands/core/archive.drush.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * An early implementation of Site Archive dump/restore. See
  5. * http://groups.drupal.org/site-archive-format.
  6. */
  7. function archive_drush_command() {
  8. $items['archive-dump'] = array(
  9. 'description' => 'Backup your code, files, and database into a single file.',
  10. 'arguments' => array(
  11. 'sites' => 'Optional. Site specifications, delimited by commas. Typically, list subdirectory name(s) under /sites.',
  12. ),
  13. 'options' => array(
  14. 'description' => 'Describe the archive contents.',
  15. 'tags' => 'Add tags to the archive manifest. Delimit multiple by commas.',
  16. 'destination' => 'The full path and filename in which the archive should be stored. If omitted, it will be saved to the drush-backups directory and a filename will be generated.',
  17. 'overwrite' => 'Do not fail if the destination file exists; overwrite it instead.',
  18. 'generator' => 'The generator name to store in the MANIFEST file. The default is "Drush archive-dump".',
  19. 'generatorversion' => 'The generator version number to store in the MANIFEST file. The default is ' . DRUSH_VERSION . '.',
  20. 'pipe' => 'Only print the destination of the archive. Useful for scripts that don\'t pass --destination.',
  21. 'preserve-symlinks' => 'Preserve symbolic links.',
  22. 'no-core' => 'Exclude Drupal core, so the backup only contains the site specific stuff.',
  23. 'tar-options' => 'Options passed thru to the tar command.',
  24. ),
  25. 'examples' => array(
  26. 'drush archive-dump default,example.com,foo.com' => 'Write an archive containing 3 sites in it.',
  27. 'drush archive-dump @sites' => 'Save archive containing all sites in a multi-site.',
  28. 'drush archive-dump default --destination=/backups/mysite.tar' => 'Save archive to custom location.',
  29. 'drush archive-dump --tar-options="--exclude=.git --exclude=sites/default/files"' => 'Omits any .git directories found in the tree as well as sites/default/files.',
  30. 'drush archive-dump --tar-options="--exclude=%files"' => 'Placeholder %files is replaced with the real path for the current site, and that path is excluded.',
  31. ),
  32. 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_SITE,
  33. 'aliases' => array('ard', 'archive-backup', 'arb'),
  34. );
  35. $items['archive-restore'] = array(
  36. 'description' => 'Expand a site archive into a Drupal web site.',
  37. 'arguments' => array(
  38. 'file' => 'The site archive file that should be expanded.',
  39. 'site name' => 'Optional. Which site within the archive you want to restore. Defaults to all.',
  40. ),
  41. 'required-arguments' => 1,
  42. 'options' => array(
  43. 'destination' => 'Specify where the Drupal site should be expanded, including the docroot. Defaults to the current working directory.',
  44. 'db-prefix' => 'An optional table prefix to use during restore.',
  45. 'db-url' => 'A Drupal 6 style database URL indicating the target for database restore. If not provided, the archived settings.php is used.',
  46. 'db-su' => 'Account to use when creating the new database. Optional.',
  47. 'db-su-pw' => 'Password for the "db-su" account. Optional.',
  48. 'overwrite' => 'Allow drush to overwrite any files in the destination.',
  49. ),
  50. 'examples' => array(
  51. 'drush archive-restore ./example.tar.gz' => 'Restore the files and databases for all sites in the archive.',
  52. 'drush archive-restore ./example.tar.gz example.com' => 'Restore the files and database for example.com site.',
  53. 'drush archive-restore ./example.tar.gz --destination=/var/www/example.com/docroot' => 'Restore archive to a custom location.',
  54. 'drush archive-restore ./example.tar.gz --db-url=mysql://root:pass@127.0.0.1/dbname' => 'Restore archive to a new database (and customize settings.php to point there.).',
  55. ),
  56. 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
  57. 'aliases' => array('arr'),
  58. );
  59. return $items;
  60. }
  61. /**
  62. * Command callback. Generate site archive file.
  63. */
  64. function drush_archive_dump($sites_subdirs = '@self') {
  65. $include_platform = !drush_get_option('no-core', FALSE);
  66. $tar = drush_get_tar_executable();
  67. $sites = array();
  68. $aliases = drush_sitealias_resolve_sitespecs(explode(',', $sites_subdirs));
  69. foreach ($aliases as $key => $alias) {
  70. $sites[$key] = $alias;
  71. if (($db_record = sitealias_get_databases_from_record($alias))) {
  72. $sites[$key]['databases'] = $db_record;
  73. }
  74. else {
  75. $sites[$key]['databases'] = array();
  76. drush_log(dt('DB definition not found for !alias', array('!alias' => $key)), 'notice');
  77. }
  78. }
  79. // The user can specify a destination filepath or not. That filepath might
  80. // end with .gz, .tgz, or something else. At the end of this command we will
  81. // gzip a file, and we want it to end up with the user-specified name (if
  82. // any), but gzip renames files and refuses to compress files ending with
  83. // .gz and .tgz, making our lives difficult. Solution:
  84. //
  85. // 1. Create a unique temporary base name to which gzip WILL append .gz.
  86. // 2. If no destination is provided, set $dest_dir to a backup directory and
  87. // $final_destination to be the unique name in that dir.
  88. // 3. If a destination is provided, set $dest_dir to that directory and
  89. // $final_destination to the exact name given.
  90. // 4. Set $destination, the actual working file we will build up, to the
  91. // unqiue name in $dest_dir.
  92. // 5. After gzip'ing $destination, rename $destination.gz to
  93. // $final_destination.
  94. //
  95. // Sheesh.
  96. // Create the unique temporary name.
  97. $prefix = 'none';
  98. if (!empty($sites)) {
  99. $first = current($sites);
  100. if ( !empty($first['databases']['default']['default']['database']) ) {
  101. $prefix = count($sites) > 1 ? 'multiple_sites' : str_replace('/', '-', $first['databases']['default']['default']['database']);
  102. }
  103. }
  104. $date = gmdate('Ymd_His');
  105. $temp_dest_name = "$prefix.$date.tar";
  106. $final_destination = drush_get_option('destination');
  107. if (!$final_destination) {
  108. // No destination provided.
  109. $backup = drush_include_engine('version_control', 'backup');
  110. // TODO: this standard drush pattern leads to a slightly obtuse directory structure.
  111. $dest_dir = $backup->prepare_backup_dir('archive-dump');
  112. if (empty($dest_dir)) {
  113. $dest_dir = drush_tempdir();
  114. }
  115. $final_destination = "$dest_dir/$temp_dest_name.gz";
  116. }
  117. else {
  118. // Use the supplied --destination. If it is relative, resolve it
  119. // relative to the directory in which drush was invoked.
  120. $command_cwd = getcwd();
  121. drush_op('chdir', drush_get_context('DRUSH_OLDCWD', getcwd()));
  122. // This doesn't perform realpath on the basename, but that's okay. This is
  123. // not path-based security. We just use it for checking for perms later.
  124. drush_mkdir(dirname($final_destination));
  125. $dest_dir = realpath(dirname($final_destination));
  126. $final_destination = $dest_dir . '/' . basename($final_destination);
  127. drush_op('chdir', $command_cwd);
  128. }
  129. // $dest_dir is either the backup directory or specified directory. Set our
  130. // working file.
  131. $destination = "$dest_dir/$temp_dest_name";
  132. // Validate the FINAL destination. It should be a file that does not exist
  133. // (unless --overwrite) in a writable directory (and a writable file if
  134. // it exists). We check all this up front to avoid failing after a long
  135. // dump process.
  136. $overwrite = drush_get_option('overwrite');
  137. $dest_dir = dirname($final_destination);
  138. $dt_args = array('!file' => $final_destination, '!dir' => $dest_dir);
  139. if (is_dir($final_destination)) {
  140. drush_set_error('DRUSH_ARCHIVE_DEST_IS_DIR', dt('destination !file must be a file, not a directory.', $dt_args));
  141. return;
  142. }
  143. else if (file_exists($final_destination)) {
  144. if (!$overwrite) {
  145. drush_set_error('DRUSH_ARCHIVE_DEST_EXISTS', dt('destination !file exists; specify --overwrite to overwrite.', $dt_args));
  146. return;
  147. }
  148. else if (!is_writable($final_destination)) {
  149. drush_set_error('DRUSH_ARCHIVE_DEST_FILE_NOT_WRITEABLE', dt('destination !file is not writable.', $dt_args));
  150. return;
  151. }
  152. }
  153. else if (!is_writable($dest_dir)) {
  154. drush_set_error('DRUSH_ARCHIVE_DEST_DIR_NOT_WRITEABLE', dt('destination directory !dir is not writable.', $dt_args));
  155. return;
  156. }
  157. // Get the extra options for tar, if any
  158. $tar_extra_options = drush_sitealias_evaluate_paths_in_options(drush_get_option('tar-options', ''));
  159. // Start adding codebase to the archive.
  160. $docroot_path = realpath(drush_get_context('DRUSH_DRUPAL_ROOT'));
  161. $docroot = basename($docroot_path);
  162. $workdir = dirname($docroot_path);
  163. if ($include_platform) {
  164. $dereference = (drush_get_option('preserve-symlinks', FALSE)) ? '' : '--dereference ';
  165. // Convert destination path to Unix style for tar on MinGW - see http://drupal.org/node/1844224
  166. if (drush_is_mingw()) {
  167. $destination_orig = $destination;
  168. $destination = str_replace('\\', '/', $destination);
  169. $destination = preg_replace('$^([a-zA-Z]):$', '/$1', $destination);
  170. }
  171. // Archive Drupal core, excluding sites dir.
  172. drush_shell_cd_and_exec($workdir, "$tar {$tar_extra_options} --exclude \"{$docroot}/sites\" {$dereference}-cf %s %s", $destination, $docroot);
  173. // Add sites/all to the same archive.
  174. drush_shell_cd_and_exec($workdir, "$tar {$tar_extra_options} {$dereference}-rf %s %s", $destination, "{$docroot}/sites/all");
  175. // Add special files in sites/ to the archive.
  176. $files_to_add = array('sites/README.txt', 'sites/sites.php', 'sites/example.sites.php');
  177. foreach ($files_to_add as $file_to_add) {
  178. if (file_exists($file_to_add)) {
  179. drush_shell_cd_and_exec($workdir, "$tar {$dereference}-rf %s %s", $destination, $docroot . '/' . $file_to_add);
  180. }
  181. }
  182. }
  183. $tmp = drush_tempdir();
  184. $all_dbs = array();
  185. // Dump the default database for each site and add to the archive.
  186. foreach ($sites as $key => $alias) {
  187. if (isset($alias['databases']['default']['default'])) {
  188. $db = $alias['databases']['default']['default'];
  189. // Use a subdirectory name matching the docroot name.
  190. drush_mkdir("{$tmp}/{$docroot}");
  191. // Ensure uniqueness by prefixing key if needed. Remove path delimiters.
  192. $dbname = str_replace(DIRECTORY_SEPARATOR, '-', $db['database']);
  193. $result_file = count($sites) == 1 ? "$tmp/$dbname.sql" : str_replace('@', '', "$tmp/$key-$dbname.sql");
  194. $all_dbs[$key] = array(
  195. 'file' => basename($result_file),
  196. 'driver' => $db['driver'],
  197. );
  198. $table_selection = drush_sql_get_table_selection();
  199. list($dump_exec, $dump_file) = drush_sql_build_dump_command($table_selection, $db, $result_file);
  200. if (!drush_shell_exec($dump_exec)) {
  201. return drush_set_error('DRUSH_SQL_DUMP_FAIL', 'Database dump failed.');
  202. }
  203. drush_shell_cd_and_exec($tmp, "$tar {$tar_extra_options} --dereference -rf %s %s", $destination, basename($result_file));
  204. }
  205. }
  206. // Build a manifest file AND add sites/$subdir to archive as we go.
  207. $platform = array(
  208. 'datestamp' => time(),
  209. 'formatversion' => '1.0',
  210. 'generator' => drush_get_option('generator', 'Drush archive-dump'),
  211. 'generatorversion' => drush_get_option('generatorversion', DRUSH_VERSION),
  212. 'description' => drush_get_option('description', ''),
  213. 'tags' => drush_get_option('tags', ''),
  214. 'archiveformat' => ($include_platform ? 'platform' : 'site'),
  215. );
  216. $contents = drush_export_ini(array('Global' => $platform));
  217. $i=0;
  218. foreach ($sites as $key => $alias) {
  219. if (!$status = drush_invoke_process($alias, 'core-status', array(), array())) {
  220. drush_log(dt('Unable to determine sites directory for !alias', array('!alias' => $key)), 'warning');
  221. }
  222. // Add the site specific directory to archive.
  223. if (!empty($status['object']['%paths']['%site'])) {
  224. drush_shell_cd_and_exec($workdir, "$tar {$tar_extra_options} --dereference -rf %s %s", $destination, "{$docroot}/sites/" . basename($status['object']['%paths']['%site']));
  225. }
  226. $site = array(
  227. 'docroot' => DRUPAL_ROOT,
  228. 'sitedir' => @$status['object']['%paths']['%site'],
  229. 'files-public' => @$status['object']['%paths']['%files'],
  230. 'files-private' => @$status['object']['%paths']['%private'],
  231. );
  232. $site["database-default-file"] = $all_dbs[$key]['file'];
  233. $site["database-default-driver"] = $all_dbs[$key]['driver'];
  234. // The section title is the sites subdirectory name.
  235. $info[basename($site['sitedir'])] = $site;
  236. $contents .= "\n" . drush_export_ini($info);
  237. unset($info);
  238. $i++;
  239. }
  240. file_put_contents("{$tmp}/MANIFEST.ini", $contents);
  241. // Add manifest to archive.
  242. drush_shell_cd_and_exec($tmp, "$tar --dereference -rf %s %s", $destination, 'MANIFEST.ini');
  243. // Ensure that default/default.settings.php is in the archive. This is needed
  244. // by site-install when restoring a site without any DB.
  245. // NOTE: Windows tar file replace operation is broken so we have to check if file already exists.
  246. // Otherwise it will corrupt the archive.
  247. $res = drush_shell_cd_and_exec($workdir, "$tar -tf %s %s", $destination, $docroot . '/sites/default/default.settings.php');
  248. $output = drush_shell_exec_output();
  249. if (!$res || !isset($output[0]) || empty($output[0])) {
  250. drush_shell_cd_and_exec($workdir, "$tar --dereference -vrf %s %s", $destination, $docroot . '/sites/default/default.settings.php');
  251. }
  252. // Switch back to original destination in case it was modified for tar on MinGW.
  253. if (!empty($destination_orig)) {
  254. $destination = $destination_orig;
  255. }
  256. // Compress the archive
  257. drush_shell_exec("gzip --no-name -f %s", $destination);
  258. // gzip appends .gz unless the name already ends in .gz, .tgz, or .taz.
  259. if ("{$destination}.gz" != $final_destination) {
  260. drush_move_dir("{$destination}.gz", $final_destination, $overwrite);
  261. }
  262. drush_log(dt('Archive saved to !dest', array('!dest' => $final_destination)), 'ok');
  263. drush_print_pipe($final_destination);
  264. return $final_destination;
  265. }
  266. /**
  267. * Command argument complete callback.
  268. *
  269. * @return
  270. * List of site names/aliases for archival.
  271. */
  272. function archive_archive_dump_complete() {
  273. return array('values' => array_keys(_drush_sitealias_all_list()));
  274. }
  275. /**
  276. * Command callback. Restore web site(s) from a site archive file.
  277. */
  278. function drush_archive_restore($file, $site_id = NULL) {
  279. $tmp = drush_tempdir();
  280. if (!$files = drush_tarball_extract($file, $tmp)) {
  281. return drush_set_error('DRUSH_ARCHIVE_UNABLE_TO_EXTRACT', dt('Unable to extract site archive tarball to !tmp.', array('!tmp' => $tmp)));
  282. }
  283. $manifest = $tmp . '/MANIFEST.ini';
  284. if (file_exists($manifest)) {
  285. if (!$ini = parse_ini_file($manifest, TRUE)) {
  286. return drush_set_error('DRUSH_ARCHIVE_UNABLE_TO_PARSE_MANIFEST', dt('Unable to parse MANIFEST.ini in the archive.'));
  287. }
  288. }
  289. else {
  290. $ini = drush_archive_guess_manifest($tmp);
  291. }
  292. // Backward compatibility: 'archiveformat' did not exist
  293. // in older versions of archive-dump.
  294. if (!isset( $ini['Global']['archiveformat'])) {
  295. $ini['Global']['archiveformat'] = 'platform';
  296. }
  297. // Grab the first site in the Manifest and move docroot to destination.
  298. $ini_tmp = $ini;
  299. unset($ini_tmp['Global']);
  300. $first = array_shift($ini_tmp);
  301. $docroot = basename($first['docroot']);
  302. $destination = drush_get_option('destination', realpath('.') . "/$docroot");
  303. if ($ini['Global']['archiveformat'] == 'platform') {
  304. // Move the whole platform inplace at once.
  305. if (!drush_move_dir("$tmp/$docroot", $destination, drush_get_option('overwrite'))) {
  306. return drush_set_error('DRUSH_ARCHIVE_UNABLE_TO_RESTORE_FILES', dt('Unable to restore platform to !dest', array('!dest' => $destination)));
  307. }
  308. }
  309. else {
  310. // When no platform is included we do this on a per-site basis.
  311. }
  312. // Loop over sites and restore databases and append to settings.php.
  313. foreach ($ini as $section => $site) {
  314. if ($section != 'Global' && (is_null($site_id) || $section == $site_id) && !empty($site['database-default-file'])) {
  315. $site_destination = $destination . '/' . $site['sitedir'];
  316. // Restore site, in case not already done above.
  317. if ($ini['Global']['archiveformat'] == 'site') {
  318. if (!drush_move_dir("$tmp/$docroot/" . $site['sitedir'], $site_destination, drush_get_option('overwrite'))) {
  319. return drush_set_error('DRUSH_ARCHIVE_UNABLE_TO_RESTORE_FILES', dt('Unable to restore site to !dest', array('!dest' => $site_destination)));
  320. }
  321. }
  322. // Restore database.
  323. $sql_file = $tmp . '/' . $site['database-default-file'];
  324. if ($db_url = drush_get_option('db-url')) {
  325. if (empty($site_id) && count($ini) >= 3) {
  326. // TODO: Use drushrc to provide multiple db-urls for multi-restore?
  327. return drush_set_error('DRUSH_ARCHIVE_UNABLE_MULTI_RESTORE', dt('You must specify a site to restore when the archive contains multiple sites and a db-url is provided.'));
  328. }
  329. $db_spec = drush_convert_db_from_db_url($db_url);
  330. }
  331. else {
  332. $site_specification = $destination . '#' . $section;
  333. if ($return = drush_invoke_process($site_specification, 'sql-conf', array(), array('all' => TRUE), array('integrate' => FALSE, 'override-simulated' => TRUE))) {
  334. $databases = $return['object'];
  335. $db_spec = $databases['default']['default'];
  336. }
  337. else {
  338. return drush_set_error('DRUSH_ARCHIVE_UNABLE_DISCOVER_DB', dt('Unable to get database details from db-url option or settings.php', array('!key' => $key)));
  339. }
  340. }
  341. drush_sql_empty_db($db_spec);
  342. _drush_sql_query(NULL, $db_spec, $sql_file);
  343. // Append new DB info to settings.php.
  344. if ($db_url) {
  345. $settingsfile = $destination . '/' . $site['sitedir'] . '/settings.php';
  346. //If settings.php doesn't exist in the archive, create it from default.settings.php.
  347. if (!file_exists($settingsfile)) {
  348. drush_op('copy', $destination . '/sites/default/default.settings.php', $settingsfile);
  349. }
  350. // Need to do something here or else we can't write.
  351. chmod($settingsfile, 0664);
  352. file_put_contents($settingsfile, "\n// Appended by drush archive-restore command.\n", FILE_APPEND);
  353. if (drush_drupal_major_version($destination) >= 7) {
  354. file_put_contents($settingsfile, "\n" . '$databases = ' . var_export(drush_sitealias_convert_db_from_db_url($db_url), TRUE) . ";\n", FILE_APPEND);
  355. }
  356. else {
  357. file_put_contents($settingsfile, "\n" . '$db_url = \'' . $db_url . "';\n", FILE_APPEND);
  358. }
  359. drush_log(dt('Drush appended the new database configuration at settings.php. Optionally remove the old configuration manually.'), 'ok');
  360. }
  361. }
  362. }
  363. drush_log(dt('Archive restored to !dest', array('!dest' => $destination)), 'ok');
  364. return $destination;
  365. }
  366. /**
  367. * Command argument complete callback.
  368. *
  369. * @return
  370. * Strong glob of files to complete on.
  371. */
  372. function archive_archive_restore_complete() {
  373. return array(
  374. 'files' => array(
  375. 'directories' => array(
  376. 'pattern' => '*',
  377. 'flags' => GLOB_ONLYDIR,
  378. ),
  379. 'tar' => array(
  380. 'pattern' => '*.tar.gz',
  381. ),
  382. ),
  383. );
  384. }
  385. /**
  386. * Try to find docroot and DB dump file in an extracted archive.
  387. *
  388. * @param string $path The location of the extracted archive.
  389. * @return array The manifest data.
  390. */
  391. function drush_archive_guess_manifest($path) {
  392. $db_file = drush_scan_directory($path, '/\.sql$/', array('.', '..', 'CVS'), 0, 0);
  393. if (file_exists($path . '/index.php')) {
  394. $docroot = './';
  395. }
  396. else {
  397. $directories = glob($path . '/*' , GLOB_ONLYDIR);
  398. $docroot = reset($directories);
  399. }
  400. $ini = array(
  401. 'Global' => array(
  402. // Very crude detection of a platform...
  403. 'archiveformat' => (drush_drupal_version($docroot) ? 'platform' : 'site'),
  404. ),
  405. 'default' => array(
  406. 'docroot' => $docroot,
  407. 'sitedir' => 'sites/default',
  408. 'database-default-file' => key($db_file),
  409. ),
  410. );
  411. return $ini;
  412. }