sources.filesource.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <?php
  2. /**
  3. * @file
  4. * A destination type for saving locally to the server.
  5. */
  6. /**
  7. * A destination type for saving locally to the server.
  8. *
  9. * @ingroup backup_migrate_destinations
  10. */
  11. class backup_migrate_destination_filesource extends backup_migrate_source {
  12. public $supported_ops = array('restore', 'configure', 'delete', 'source');
  13. /**
  14. *
  15. */
  16. public function type_name() {
  17. return t("Files Directory");
  18. }
  19. /**
  20. * Declares the current files directory as a backup source..
  21. */
  22. public function sources() {
  23. $out = array();
  24. $out['files'] = backup_migrate_create_destination('filesource', array(
  25. 'machine_name' => 'files',
  26. 'location' => 'public://',
  27. 'name' => t('Public Files Directory'),
  28. 'show_in_list' => FALSE,
  29. ));
  30. if (variable_get('file_private_path', FALSE)) {
  31. $out['files_private'] = backup_migrate_create_destination('filesource', array(
  32. 'machine_name' => 'files',
  33. 'location' => 'private://',
  34. 'name' => t('Private Files Directory'),
  35. 'show_in_list' => FALSE,
  36. ));
  37. }
  38. return $out;
  39. }
  40. /**
  41. * Gets the form for the settings for the files destination.
  42. */
  43. public function edit_form() {
  44. $form = parent::edit_form();
  45. $form['location'] = array(
  46. "#type" => "textfield",
  47. "#title" => t("Directory path"),
  48. "#default_value" => $this->get_location(),
  49. "#required" => TRUE,
  50. "#description" => t('Enter the path to the directory to back up. Use a relative path to pick a path relative to your Drupal root directory. The web server must be able to read from this path.'),
  51. );
  52. return $form;
  53. }
  54. /**
  55. * Returns a list of backup filetypes.
  56. */
  57. public function file_types() {
  58. return array(
  59. "tar" => array(
  60. "extension" => "tar",
  61. "filemime" => "application/x-tar",
  62. "backup" => TRUE,
  63. "restore" => TRUE,
  64. ),
  65. );
  66. }
  67. /**
  68. * Gets the form for the settings for this destination.
  69. *
  70. * Return the default directories whose data can be ignored. These directories
  71. * contain info which can be easily reproducted. Also exclude the backup and
  72. * migrate folder to prevent exponential bloat.
  73. */
  74. public function backup_settings_default() {
  75. return array(
  76. 'exclude_filepaths' => "backup_migrate\nstyles\ncss\njs\nctools\nless\nlanguages\nadvagg_css\nadvagg_js",
  77. );
  78. }
  79. /**
  80. * Get the form for the backup settings for this destination.
  81. */
  82. public function backup_settings_form($settings) {
  83. $form['exclude_filepaths'] = array(
  84. "#type" => "textarea",
  85. "#multiple" => TRUE,
  86. "#title" => t("Exclude the following files or directories"),
  87. "#default_value" => isset($settings['exclude_filepaths']) ? $settings['exclude_filepaths'] : '',
  88. "#description" => t("A list of files or directories to be excluded from backups. Add one path per line relative to the directory being backed up."),
  89. );
  90. return $form;
  91. }
  92. /**
  93. * Backup from this source.
  94. */
  95. public function backup_to_file($file, $settings) {
  96. if ($out = $this->_backup_to_file_cli($file, $settings)) {
  97. return $out;
  98. }
  99. else {
  100. return $this->_backup_to_file_php($file, $settings);
  101. }
  102. }
  103. /**
  104. * Backup from this source.
  105. */
  106. public function _backup_to_file_php($file, $settings) {
  107. if ($this->check_libs()) {
  108. $excluded = $this->get_excluded_paths($settings);
  109. $files = $this->get_files_to_backup($this->get_realpath(), $settings, $excluded);
  110. if ($files) {
  111. $file->push_type('tar');
  112. $gz = new Archive_Tar($file->filepath(), FALSE);
  113. $gz->addModify($files, '', $this->get_realpath());
  114. return $file;
  115. }
  116. backup_migrate_backup_fail('No files available.', array(), $settings);
  117. return FALSE;
  118. }
  119. return FALSE;
  120. }
  121. /**
  122. * Backup from this source.
  123. */
  124. public function _backup_to_file_cli($file, $settings) {
  125. if (!empty($settings->filters['use_cli']) && function_exists('backup_migrate_exec') && function_exists('escapeshellarg')) {
  126. $excluded = $this->get_excluded_paths($settings);
  127. $exclude = array();
  128. foreach ($excluded as $path) {
  129. $exclude[] = '--exclude=' . escapeshellarg($path);
  130. }
  131. $exclude = implode(' ', $exclude);
  132. // Create a symlink in a temp directory so we can rename the file in the
  133. // archive.
  134. $temp = backup_migrate_temp_directory();
  135. $file->push_type('tar');
  136. backup_migrate_exec("tar --dereference -C %input -rf %output $exclude .", array(
  137. '%output' => $file->filepath(),
  138. '%input' => $this->get_realpath(),
  139. '%temp' => $temp,
  140. ));
  141. return $file;
  142. }
  143. return FALSE;
  144. }
  145. /**
  146. * Restore to this source.
  147. */
  148. public function restore_from_file($file, &$settings) {
  149. if ($out = $this->_restore_from_file_cli($file, $settings)) {
  150. return $out;
  151. }
  152. else {
  153. return $this->_restore_from_file_php($file, $settings);
  154. }
  155. }
  156. /**
  157. * Restore to this source.
  158. */
  159. public function _restore_from_file_php($file, &$settings) {
  160. if ($this->check_libs()) {
  161. $from = $file->pop_type();
  162. $temp = backup_migrate_temp_directory();
  163. $tar = new Archive_Tar($from->filepath());
  164. $tar->extractModify($temp, $file->name);
  165. // Older B&M Files format included a base 'files' directory.
  166. if (file_exists($temp . '/files')) {
  167. $temp = $temp . '/files';
  168. }
  169. if (file_exists($temp . '/' . $file->name . '/files')) {
  170. $temp = $temp . '/files';
  171. }
  172. // Move the files from the temp directory.
  173. _backup_migrate_move_files($temp, $this->get_realpath());
  174. return $file;
  175. }
  176. return FALSE;
  177. }
  178. /**
  179. * Restore to this source.
  180. */
  181. public function _restore_from_file_cli($file, &$settings) {
  182. if (!empty($settings->filters['use_cli']) && function_exists('backup_migrate_exec')) {
  183. $temp = backup_migrate_temp_directory();
  184. backup_migrate_exec("tar -C %temp -xf %input", array('%input' => $file->filepath(), '%temp' => $temp));
  185. // Older B&M Files format included a base 'files' directory.
  186. if (file_exists($temp . '/files')) {
  187. $temp = $temp . '/files';
  188. }
  189. if (file_exists($temp . '/' . $file->name . '/files')) {
  190. $temp = $temp . '/files';
  191. }
  192. // Move the files from the temp directory.
  193. backup_migrate_exec("mv -rf %temp/* %output", array('%output' => $this->get_realpath(), '%temp' => $temp));
  194. return $file;
  195. }
  196. return FALSE;
  197. }
  198. /**
  199. * Gets a list of files to backup from the given set if dirs.
  200. *
  201. * Exclude any that match the array $exclude.
  202. */
  203. public function get_files_to_backup($dir, $settings, $exclude = array()) {
  204. $out = $errors = array();
  205. if (!file_exists($dir)) {
  206. backup_migrate_backup_fail('Directory %dir does not exist.', array('%dir' => $dir), $settings);
  207. return FALSE;
  208. }
  209. if ($handle = @opendir($dir)) {
  210. while (($file = readdir($handle)) !== FALSE) {
  211. if ($file != '.' && $file != '..' && !in_array($file, $exclude)) {
  212. $real = realpath($dir . '/' . $file);
  213. // If the path is not excluded.
  214. if (!in_array($real, $exclude)) {
  215. if (is_dir($real)) {
  216. $subdir = $this->get_files_to_backup($real, $settings, $exclude);
  217. // If there was an error reading the subdirectory then abort the
  218. // backup.
  219. if ($subdir === FALSE) {
  220. closedir($handle);
  221. return FALSE;
  222. }
  223. // If the directory is empty, add an empty directory.
  224. if (count($subdir) == 0) {
  225. $out[] = $real;
  226. }
  227. $out = array_merge($out, $subdir);
  228. }
  229. else {
  230. if (is_readable($real)) {
  231. $out[] = $real;
  232. }
  233. else {
  234. $errors[] = $dir . '/' . $file;
  235. }
  236. }
  237. }
  238. }
  239. }
  240. closedir($handle);
  241. }
  242. else {
  243. backup_migrate_backup_fail('Could not open directory %dir', array('%dir' => $dir), $settings);
  244. return FALSE;
  245. }
  246. // Alert the user to any errors there might have been.
  247. if ($errors) {
  248. if (count($errors < 5)) {
  249. $filesmsg = t('The following files: !files', array('!files' => theme('item_list', array('items' => $errors))));
  250. }
  251. else {
  252. $filesmsg = t('!count files', array('!count' => count($errors)));
  253. }
  254. if (empty($settings->filters['ignore_errors'])) {
  255. backup_migrate_backup_fail('The backup could not be completed because !files could not be read. If you want to skip unreadable files use the \'Ignore Errors\' setting under \'Advanced Options\' in \'Advanced Backup\' or in your schedule settings profile.', array('!files' => $filesmsg), 'error');
  256. $out = FALSE;
  257. }
  258. else {
  259. backup_migrate_backup_fail('!files could not be read and were skipped', array('!files' => $filesmsg), 'error');
  260. }
  261. }
  262. return $out;
  263. }
  264. /**
  265. * Breaks the excluded paths string into a usable list of paths.
  266. */
  267. public function get_excluded_paths($settings) {
  268. $base_dir = $this->get_realpath() . '/';
  269. $paths = empty($settings->filters['exclude_filepaths']) ? '' : $settings->filters['exclude_filepaths'];
  270. $out = explode("\n", $paths);
  271. foreach ($out as $key => $val) {
  272. $path = trim($val, "/ \t\r\n");
  273. // If the path specified is a stream url or absolute path add the
  274. // normalized version.
  275. if ($real = drupal_realpath($path)) {
  276. $out[$key] = $real;
  277. }
  278. // If the path is a relative path add it.
  279. elseif ($real = drupal_realpath($base_dir . $path)) {
  280. $out[$key] = $real;
  281. }
  282. // Otherwise add it as is even though it probably won't match any files.
  283. else {
  284. $out[$key] = $path;
  285. }
  286. }
  287. return $out;
  288. }
  289. /**
  290. * Checks that the required libraries are installed.
  291. */
  292. public function check_libs() {
  293. $result = TRUE;
  294. // Drupal 7 has Archive_Tar built in so there should be no need to include
  295. // anything here.
  296. return $result;
  297. }
  298. /**
  299. * Get the file location.
  300. */
  301. public function get_realpath() {
  302. return drupal_realpath($this->get_location());
  303. }
  304. }